🎉 基础功能跑通

This commit is contained in:
李寻欢 2023-04-17 08:27:26 +08:00
commit 138f15d371
8 changed files with 167 additions and 0 deletions

View File

@ -0,0 +1,22 @@
name: BuildImage
on:
push:
tags:
- '*'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: checkout code
uses: actions/checkout@v3
- name: setup go
uses: https://github.com/actions/setup-go@v4
with:
go-version: '>=1.20.1'
- name: compile
run: |
docker build -t ${{ github.repository }}:${{ github.ref }} .

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
.idea
vendor
logs
*.exe
*.pprof
cache
log
dist

16
Dockerfile Normal file
View File

@ -0,0 +1,16 @@
FROM golang:alpine as builder
WORKDIR /builder
COPY . .
RUN apk add upx
RUN go mod download
RUN go build -o app
RUN upx -9 app
RUN ls -lh && chmod +x ./app
FROM code.hyxc1.com/open/alpine:3.16.0 as runner
LABEL org.opencontainers.image.authors="lxh@cxh.cn"
WORKDIR /app
COPY --from=builder /builder/app ./app
CMD ./app

12
go.mod Normal file
View File

@ -0,0 +1,12 @@
module game-db-robot
go 1.20
require (
github.com/bits-and-blooms/bitset v1.2.2 // indirect
github.com/bits-and-blooms/bloom/v3 v3.2.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/lonelyevil/kook v0.0.33 // indirect
github.com/lonelyevil/kook/log_adapter/plog v0.0.31 // indirect
github.com/phuslu/log v1.0.80 // indirect
)

15
go.sum Normal file
View File

@ -0,0 +1,15 @@
github.com/bits-and-blooms/bitset v1.2.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
github.com/bits-and-blooms/bitset v1.2.2 h1:J5gbX05GpMdBjCvQ9MteIg2KKDExr7DrgK+Yc15FvIk=
github.com/bits-and-blooms/bitset v1.2.2/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
github.com/bits-and-blooms/bloom/v3 v3.2.0 h1:N+g3GTQ0TVbghahYyzwkQbMZR+IwIwFFC8dpIChtN0U=
github.com/bits-and-blooms/bloom/v3 v3.2.0/go.mod h1:MC8muvBzzPOFsrcdND/A7kU7kMhkqb9KI70JlZCP+C8=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/lonelyevil/kook v0.0.29/go.mod h1:WjHC7AmbmNjInT/U/etBVOmAw7T6EqdCwApceRGs1sk=
github.com/lonelyevil/kook v0.0.33 h1:+rXofbtiwJhZmQ9WSuuQT4oNtsumItdy56zNLrB6Z5U=
github.com/lonelyevil/kook v0.0.33/go.mod h1:WjHC7AmbmNjInT/U/etBVOmAw7T6EqdCwApceRGs1sk=
github.com/lonelyevil/kook/log_adapter/plog v0.0.31 h1:L3lpDpxXcD6nEbT00gj2rMfa2TNScbUbnyAwXSIgmKc=
github.com/lonelyevil/kook/log_adapter/plog v0.0.31/go.mod h1:J/ClWHIMo9hALhK88Zd+zZ5jC6nKfBx8XDWicO+QRZc=
github.com/phuslu/log v1.0.80 h1:spAqKcba2lTTpxp6uBFLSVZrtirHiD4B8UepJdXR0H4=
github.com/phuslu/log v1.0.80/go.mod h1:kzJN3LRifrepxThMjufQwS7S35yFAB+jAV1qgA7eBW4=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=

View File

@ -0,0 +1,13 @@
package handler
import (
"fmt"
"github.com/lonelyevil/kook"
)
// GuildMemberAdd
// @description: 用户加入服务器
// @param ctx
func GuildMemberAdd(ctx *kook.GuildMemberAddContext) {
fmt.Printf("有新用户加入服务器: %s\n", ctx.Extra.UserID)
}

50
handler/kmarkdown.go Normal file
View File

@ -0,0 +1,50 @@
package handler
import (
"fmt"
"github.com/lonelyevil/kook"
"regexp"
)
// KMarkdownHandler
// @description:
func KMarkdownHandler(ctx *kook.KmarkdownMessageContext) {
if ctx.Common.Type != kook.MessageTypeKMarkdown || ctx.Extra.Author.Bot {
return
}
pattern := `\(met\)[^()]*\(met\) `
re := regexp.MustCompile(pattern)
content := re.ReplaceAllString(ctx.Common.Content, "")
fmt.Printf("收到新消息: %s(%s) -> %s\n", ctx.Extra.Author.Username, ctx.Extra.Author.Nickname, content)
// 判断是否为at机器人不是的话就不处理
isAt := false
for _, part := range ctx.Extra.Kmarkdown.MentionPart {
if part.ID == "2259002513" {
isAt = true
break
}
}
if !isAt {
return
}
reply := "机器人功能开发中~"
if content == "ping" {
reply = "pong"
}
resp, err := ctx.Session.MessageCreate(&kook.MessageCreate{
MessageCreateBase: kook.MessageCreateBase{
TargetID: ctx.Common.TargetID,
Content: reply,
Quote: ctx.Common.MsgID,
Type: kook.MessageTypeKMarkdown,
},
})
if err != nil {
fmt.Printf("消息回复失败: %v", err)
fmt.Printf("接口返回信息: %+v", resp)
}
}

31
main.go Normal file
View File

@ -0,0 +1,31 @@
package main
import (
"fmt"
"game-db-robot/handler"
"github.com/lonelyevil/kook"
"github.com/lonelyevil/kook/log_adapter/plog"
"github.com/phuslu/log"
"os"
"os/signal"
"syscall"
)
// main
// @description: 启动入口
func main() {
l := log.Logger{
Level: log.InfoLevel,
Writer: &log.ConsoleWriter{},
}
s := kook.New("1/MTY4MzU=/s8MDdc+Yz9qArpCDFycktA==", plog.NewLogger(&l))
s.AddHandler(handler.KMarkdownHandler) // KMarkdown消息处理
s.AddHandler(handler.GuildMemberAdd) // 用户加入服务器
s.Open()
// Wait here until CTRL-C or other term signal is received.
fmt.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, os.Interrupt, syscall.SIGTERM)
<-sc
s.Close()
}