Compare commits
10 Commits
feat/kick
...
b023de86ee
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b023de86ee | ||
|
|
5528766c13 | ||
|
|
46cf57764c | ||
|
|
8f7bb8feba | ||
|
|
c6473fd68a | ||
|
|
d4f070f9ec | ||
|
|
ac593ddbc8 | ||
|
|
7bd7e940ba | ||
|
|
c278b3f516 | ||
|
|
730aea3e3b |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +1,4 @@
|
||||
.env
|
||||
run.sh
|
||||
mcbot
|
||||
*.sqlite
|
||||
|
||||
22
.vscode/launch.json
vendored
Normal file
22
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
{
|
||||
// 使用 IntelliSense 了解相关属性。
|
||||
// 悬停以查看现有属性的描述。
|
||||
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Attach to Process",
|
||||
"type": "go",
|
||||
"request": "attach",
|
||||
"mode": "local",
|
||||
"processId": "${command:pickGoProcess}"
|
||||
},
|
||||
{
|
||||
"name": "Launch Package",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "${workspaceFolder}"
|
||||
}
|
||||
]
|
||||
}
|
||||
13
Dockerfile
13
Dockerfile
@@ -1,11 +1,12 @@
|
||||
FROM git.vaala.cloud/vaalacat/golang:1.20 AS builder
|
||||
|
||||
WORKDIR $GOPATH/src/mcbot
|
||||
COPY . .
|
||||
RUN mkdir /app && \
|
||||
CGO_ENABLED=0 GOPROXY=https://goproxy.cn,direct go build -o mcbot main.go && \
|
||||
cp mcbot /app/
|
||||
# WORKDIR $GOPATH/src/mcbot
|
||||
# COPY . .
|
||||
# RUN mkdir /app && \
|
||||
# CGO_ENABLED=0 GOPROXY=https://goproxy.cn,direct go build -o mcbot main.go && \
|
||||
# cp mcbot /app/
|
||||
|
||||
FROM git.vaala.cloud/vaalacat/alpine
|
||||
COPY --from=builder /app/mcbot /app/mcbot
|
||||
# COPY --from=builder /app/mcbot /app/mcbot
|
||||
COPY mcbot /app/mcbot
|
||||
ENTRYPOINT [ "/app/mcbot" ]
|
||||
45
conf/env.go
45
conf/env.go
@@ -1,51 +1,30 @@
|
||||
package conf
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/ilyakaznacheev/cleanenv"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type botSettings struct {
|
||||
HTTPProxy string
|
||||
BotToken string
|
||||
MCServer string
|
||||
MCBotName string
|
||||
GroupID int64
|
||||
HTTPProxy string `env:"HTTP_PROXY"`
|
||||
BotToken string `env:"BOT_TOKEN"`
|
||||
MCServer string `env:"MC_SERVER"`
|
||||
MCBotName string `env:"MC_BOT_NAME"`
|
||||
GroupID int64 `env:"GROUP_ID"`
|
||||
DBPath string `env:"DB_PATH"`
|
||||
BotAPI string `env:"TG_BOT_API"`
|
||||
AdminID []int64 `env:"ADMIN_ID"`
|
||||
}
|
||||
|
||||
var (
|
||||
botSettingsInstance *botSettings
|
||||
botSettingsInstance botSettings
|
||||
)
|
||||
|
||||
func init() {
|
||||
godotenv.Load()
|
||||
http_proxy := os.Getenv("HTTP_PROXY")
|
||||
bot_token := os.Getenv("BOT_TOKEN")
|
||||
mc_server := os.Getenv("MC_SERVER")
|
||||
mc_bot_name := os.Getenv("MC_BOT_NAME")
|
||||
group_id_str := os.Getenv("GROUP_ID")
|
||||
|
||||
if http_proxy == "" || bot_token == "" || mc_server == "" || mc_bot_name == "" || group_id_str == "" {
|
||||
logrus.Panic("请检查环境变量是否设置正确")
|
||||
}
|
||||
|
||||
group_id, err := strconv.ParseInt(group_id_str, 10, 64)
|
||||
if err != nil {
|
||||
logrus.Panic("请检查环境变量是否设置正确")
|
||||
}
|
||||
|
||||
botSettingsInstance = &botSettings{
|
||||
HTTPProxy: http_proxy,
|
||||
BotToken: bot_token,
|
||||
MCServer: mc_server,
|
||||
MCBotName: mc_bot_name,
|
||||
GroupID: group_id,
|
||||
}
|
||||
cleanenv.ReadEnv(&botSettingsInstance)
|
||||
}
|
||||
|
||||
func GetBotSettings() *botSettings {
|
||||
return botSettingsInstance
|
||||
return &botSettingsInstance
|
||||
}
|
||||
|
||||
39
defs/callback.go
Normal file
39
defs/callback.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package defs
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type Command struct {
|
||||
Command string `json:"Command"`
|
||||
Argstr string `json:"Argstr"`
|
||||
}
|
||||
|
||||
func (c *Command) ToJSON() string {
|
||||
ans, _ := json.Marshal(c)
|
||||
return string(ans)
|
||||
}
|
||||
|
||||
const (
|
||||
CMD_UNKNOWN = "unknown"
|
||||
CMD_APPROVE = "approve"
|
||||
CMD_REJECT = "reject"
|
||||
)
|
||||
|
||||
func NewApproveCommand(mcName string) *Command {
|
||||
return &Command{
|
||||
Command: CMD_APPROVE,
|
||||
Argstr: mcName,
|
||||
}
|
||||
}
|
||||
|
||||
func NewRejectCommand(mcName string) *Command {
|
||||
return &Command{
|
||||
Command: CMD_REJECT,
|
||||
Argstr: mcName,
|
||||
}
|
||||
}
|
||||
|
||||
func NewCommandFromJSON(jsonStr string) (*Command, error) {
|
||||
var cmd Command
|
||||
err := json.Unmarshal([]byte(jsonStr), &cmd)
|
||||
return &cmd, err
|
||||
}
|
||||
32
go.mod
32
go.mod
@@ -3,14 +3,36 @@ module tg-mc
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/Tnze/go-mc v1.19.4-pre1
|
||||
github.com/Tnze/go-mc v1.19.4
|
||||
github.com/glebarez/sqlite v1.9.0
|
||||
github.com/go-co-op/gocron v1.32.1
|
||||
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
|
||||
github.com/ilyakaznacheev/cleanenv v1.5.0
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/sirupsen/logrus v1.9.1
|
||||
github.com/samber/lo v1.38.1
|
||||
github.com/sirupsen/logrus v1.9.3
|
||||
gorm.io/gorm v1.25.4
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/google/uuid v1.3.0 // indirect
|
||||
github.com/iancoleman/strcase v0.2.0 // indirect
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/glebarez/go-sqlite v1.21.2 // indirect
|
||||
github.com/google/uuid v1.3.1 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/robfig/cron/v3 v3.0.1 // indirect
|
||||
go.uber.org/atomic v1.11.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 // indirect
|
||||
golang.org/x/sys v0.11.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
modernc.org/libc v1.24.1 // indirect
|
||||
modernc.org/mathutil v1.6.0 // indirect
|
||||
modernc.org/memory v1.7.1 // indirect
|
||||
modernc.org/sqlite v1.25.0 // indirect
|
||||
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect
|
||||
)
|
||||
|
||||
replace github.com/Tnze/go-mc => /Users/vaala/Workdir/Code/go-mc
|
||||
|
||||
112
go.sum
112
go.sum
@@ -1,25 +1,127 @@
|
||||
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
|
||||
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/Tnze/go-mc v1.19.4-pre1 h1:0yBCeK9EGICdIzxSPbvD88HqcNRho8PRgKtpAC02W0E=
|
||||
github.com/Tnze/go-mc v1.19.4-pre1/go.mod h1:c1znJQglgqa1Jjs3Dr29woN/msguiJrlNtWXhKedh2U=
|
||||
github.com/Tnze/go-mc v1.19.4 h1:9qtxH+xRJWswOYnlf/dsFY4EI2f5jsFhtqTYOObaGIE=
|
||||
github.com/Tnze/go-mc v1.19.4/go.mod h1:c1znJQglgqa1Jjs3Dr29woN/msguiJrlNtWXhKedh2U=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/glebarez/go-sqlite v1.21.1 h1:7MZyUPh2XTrHS7xNEHQbrhfMZuPSzhkm2A1qgg0y5NY=
|
||||
github.com/glebarez/go-sqlite v1.21.1/go.mod h1:ISs8MF6yk5cL4n/43rSOmVMGJJjHYr7L2MbZZ5Q4E2E=
|
||||
github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9gAXWo=
|
||||
github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k=
|
||||
github.com/glebarez/sqlite v1.8.0 h1:02X12E2I/4C1n+v90yTqrjRa8yuo7c3KeHI3FRznCvc=
|
||||
github.com/glebarez/sqlite v1.8.0/go.mod h1:bpET16h1za2KOOMb8+jCp6UBP/iahDpfPQqSaYLTLx8=
|
||||
github.com/glebarez/sqlite v1.9.0 h1:Aj6bPA12ZEx5GbSF6XADmCkYXlljPNUY+Zf1EQxynXs=
|
||||
github.com/glebarez/sqlite v1.9.0/go.mod h1:YBYCoyupOao60lzp1MVBLEjZfgkq0tdB1voAQ09K9zw=
|
||||
github.com/go-co-op/gocron v1.28.3 h1:swTsge6u/1Ei51b9VLMz/YTzEzWpbsk5SiR7m5fklTI=
|
||||
github.com/go-co-op/gocron v1.28.3/go.mod h1:39f6KNSGVOU1LO/ZOoZfcSxwlsJDQOKSu8erN0SH48Y=
|
||||
github.com/go-co-op/gocron v1.32.1 h1:h+StA6Qzlv+ImlCaLfA26rLN9eS/l4sO7oWmPUbRVIY=
|
||||
github.com/go-co-op/gocron v1.32.1/go.mod h1:UGz2oYvVS6PsqlwuOdo5L1Djsg/cQjxJ6T5ntkhp9Bg=
|
||||
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
|
||||
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
|
||||
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
|
||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0=
|
||||
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
|
||||
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
|
||||
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/ilyakaznacheev/cleanenv v1.4.2 h1:nRqiriLMAC7tz7GzjzUTBHfzdzw6SQ7XvTagkFqe/zU=
|
||||
github.com/ilyakaznacheev/cleanenv v1.4.2/go.mod h1:i0owW+HDxeGKE0/JPREJOdSCPIyOnmh6C0xhWAkF/xA=
|
||||
github.com/ilyakaznacheev/cleanenv v1.5.0 h1:0VNZXggJE2OYdXE87bfSSwGxeiGt9moSR2lOrsHHvr4=
|
||||
github.com/ilyakaznacheev/cleanenv v1.5.0/go.mod h1:a5aDzaJrLCQZsazHol1w8InnDcOX0OColm64SlIi6gk=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
|
||||
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
|
||||
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
|
||||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
||||
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
||||
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
|
||||
github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg=
|
||||
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
|
||||
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
|
||||
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
|
||||
github.com/sirupsen/logrus v1.9.1 h1:Ou41VVR3nMWWmTiEUnj0OlsgOSCUFgsPAOl6jRIcVtQ=
|
||||
github.com/sirupsen/logrus v1.9.1/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
|
||||
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
|
||||
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
|
||||
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
|
||||
golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug=
|
||||
golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
|
||||
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 h1:m64FZMko/V45gv0bNmrNYoDEq8U5YUhetc9cBWKS1TQ=
|
||||
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63/go.mod h1:0v4NqG35kSWCMzLaMeX+IQrlSnVE/bqGSyC2cz/9Le8=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
|
||||
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
|
||||
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gorm.io/gorm v1.25.1 h1:nsSALe5Pr+cM3V1qwwQ7rOkw+6UeLrX5O4v3llhHa64=
|
||||
gorm.io/gorm v1.25.1/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
|
||||
gorm.io/gorm v1.25.4 h1:iyNd8fNAe8W9dvtlgeRI5zSVZPsq3OpcTu37cYcpCmw=
|
||||
gorm.io/gorm v1.25.4/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
|
||||
modernc.org/libc v1.22.3 h1:D/g6O5ftAfavceqlLOFwaZuA5KYafKwmr30A6iSqoyY=
|
||||
modernc.org/libc v1.22.3/go.mod h1:MQrloYP209xa2zHome2a8HLiLm6k0UT8CoHpV74tOFw=
|
||||
modernc.org/libc v1.24.1 h1:uvJSeCKL/AgzBo2yYIPPTy82v21KgGnizcGYfBHaNuM=
|
||||
modernc.org/libc v1.24.1/go.mod h1:FmfO1RLrU3MHJfyi9eYYmZBfi/R+tqZ6+hQ3yQQUkak=
|
||||
modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ=
|
||||
modernc.org/mathutil v1.5.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
|
||||
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
|
||||
modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo=
|
||||
modernc.org/memory v1.5.0 h1:N+/8c5rE6EqugZwHii4IFsaJ7MUhoWX07J5tC/iI5Ds=
|
||||
modernc.org/memory v1.5.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU=
|
||||
modernc.org/memory v1.7.1 h1:9J+2/GKTlV503mk3yv8QJ6oEpRCUrRy0ad8TXEPoV8M=
|
||||
modernc.org/memory v1.7.1/go.mod h1:NO4NVCQy0N7ln+T9ngWqOQfi7ley4vpwvARR+Hjw95E=
|
||||
modernc.org/sqlite v1.21.1 h1:GyDFqNnESLOhwwDRaHGdp2jKLDzpyT/rNLglX3ZkMSU=
|
||||
modernc.org/sqlite v1.21.1/go.mod h1:XwQ0wZPIh1iKb5mkvCJ3szzbhk+tykC8ZWqTRTgYRwI=
|
||||
modernc.org/sqlite v1.25.0 h1:AFweiwPNd/b3BoKnBOfFm+Y260guGMF+0UFk0savqeA=
|
||||
modernc.org/sqlite v1.25.0/go.mod h1:FL3pVXie73rg3Rii6V/u5BoHlSoyeZeIgKZEgHARyCU=
|
||||
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 h1:slmdOY3vp8a7KQbHkL+FLbvbkgMqmXojpFUO/jENuqQ=
|
||||
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3/go.mod h1:oVgVk4OWVDi43qWBEyGhXgYxt7+ED4iYNpTngSLX2Iw=
|
||||
|
||||
50
models/user.go
Normal file
50
models/user.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"tg-mc/utils/database"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
gorm.Model
|
||||
TGID int64 `gorm:"unique"`
|
||||
MCName string
|
||||
Status int // 0: pending, 1: normal, 2: banned
|
||||
}
|
||||
|
||||
func init() {
|
||||
if err := database.GetDB().AutoMigrate(&User{}); err != nil {
|
||||
logrus.Panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (u *User) TableName() string {
|
||||
return "users"
|
||||
}
|
||||
|
||||
func GetUserByTGID(tgID int64) (user User, err error) {
|
||||
err = database.GetDB().Where(
|
||||
&User{TGID: tgID},
|
||||
).First(&user).Error
|
||||
return
|
||||
}
|
||||
|
||||
func GetUserByMCName(mcName string) (user User, err error) {
|
||||
err = database.GetDB().Where(
|
||||
&User{MCName: mcName},
|
||||
).First(&user).Error
|
||||
return
|
||||
}
|
||||
|
||||
func CreateUser(u *User) (err error) {
|
||||
err = database.GetDB().Create(&u).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (u *User) Delete(tgID int64) error {
|
||||
return database.GetDB().Where(
|
||||
&User{TGID: tgID},
|
||||
).Unscoped().Delete(&u).Error
|
||||
}
|
||||
@@ -14,5 +14,5 @@ func Run() {
|
||||
}
|
||||
}
|
||||
}()
|
||||
tgbot.Run(mc.SendMsg)
|
||||
tgbot.Run(mc.SendMsg, mc.SendCommand)
|
||||
}
|
||||
|
||||
52
services/mc/auth.go
Normal file
52
services/mc/auth.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package mc
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"tg-mc/models"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Auth interface {
|
||||
IsAuthed(u models.User, expireMode bool) bool
|
||||
Auth(u models.User)
|
||||
Reject(u models.User)
|
||||
}
|
||||
|
||||
type Authcator struct {
|
||||
UserMap *sync.Map
|
||||
}
|
||||
|
||||
var authcator *Authcator
|
||||
|
||||
func GetAuthcator() Auth {
|
||||
if authcator == nil {
|
||||
authcator = &Authcator{
|
||||
UserMap: &sync.Map{},
|
||||
}
|
||||
}
|
||||
return authcator
|
||||
}
|
||||
|
||||
func (a *Authcator) IsAuthed(u models.User, expireMode bool) bool {
|
||||
// if u.MCName != "VaalaCat" {
|
||||
// return true
|
||||
// }
|
||||
if approveTime, ok := a.UserMap.Load(u.MCName); ok {
|
||||
if !expireMode {
|
||||
return true
|
||||
} else if time.Since(approveTime.(time.Time)) < 30*time.Second {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (a *Authcator) Auth(u models.User) {
|
||||
a.UserMap.Store(u.MCName, time.Now())
|
||||
}
|
||||
|
||||
func (a *Authcator) Reject(u models.User) {
|
||||
a.UserMap.Delete(u.MCName)
|
||||
}
|
||||
16
services/mc/defs.go
Normal file
16
services/mc/defs.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package mc
|
||||
|
||||
const (
|
||||
ErrNotJoined = "not joined"
|
||||
)
|
||||
|
||||
const (
|
||||
EventPlayerJoined = "multiplayer.player.joined"
|
||||
EventPlayerLeft = "multiplayer.player.left"
|
||||
)
|
||||
|
||||
const (
|
||||
StatusPending = iota
|
||||
StatusNormal
|
||||
StatusBanned
|
||||
)
|
||||
122
services/mc/helper.go
Normal file
122
services/mc/helper.go
Normal file
@@ -0,0 +1,122 @@
|
||||
package mc
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/defs"
|
||||
"tg-mc/models"
|
||||
su "tg-mc/services/utils"
|
||||
"tg-mc/utils"
|
||||
"time"
|
||||
|
||||
"github.com/Tnze/go-mc/chat"
|
||||
"github.com/Tnze/go-mc/data/packetid"
|
||||
"github.com/Tnze/go-mc/net/packet"
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func GetJoinedPlayer(m chat.Message) (userName string, err error) {
|
||||
if m.Translate != "multiplayer.player.joined" && len(m.With) == 0 {
|
||||
return "", errors.New(ErrNotJoined)
|
||||
}
|
||||
userName = m.With[0].Text
|
||||
return
|
||||
}
|
||||
|
||||
func GetLeftPlayer(m chat.Message) (userName string, err error) {
|
||||
if m.Translate != "multiplayer.player.left" && len(m.With) == 0 {
|
||||
return "", errors.New(ErrNotJoined)
|
||||
}
|
||||
userName = m.With[0].Text
|
||||
return
|
||||
}
|
||||
|
||||
func HandleJoinGame(userName string, mention bool, expireMode bool) {
|
||||
|
||||
u, err := models.GetUserByMCName(userName)
|
||||
if err != nil {
|
||||
logrus.Error("get user name error: ", err)
|
||||
}
|
||||
|
||||
switch u.Status {
|
||||
case StatusNormal:
|
||||
if !GetAuthcator().IsAuthed(u, expireMode) {
|
||||
m := tgbotapi.NewMessage(u.TGID, fmt.Sprintf("MC用户:%v 尝试登录,请手动允许,每次授权持续30秒", userName))
|
||||
m.ReplyMarkup = tgbotapi.NewInlineKeyboardMarkup(
|
||||
tgbotapi.NewInlineKeyboardRow(
|
||||
tgbotapi.NewInlineKeyboardButtonData("批准", defs.NewApproveCommand(u.MCName).ToJSON()),
|
||||
tgbotapi.NewInlineKeyboardButtonData("拒绝", defs.NewRejectCommand(u.MCName).ToJSON())),
|
||||
)
|
||||
conf.Bot.Send(m)
|
||||
KickPlayer(userName)
|
||||
return
|
||||
}
|
||||
if mention {
|
||||
SendMsgToPlayer("欢迎回来!", userName)
|
||||
}
|
||||
case StatusPending:
|
||||
SendMsgToPlayer("你还没有绑定 Telegram 哦, 5秒后你将会被踢出。请在群组中发送 /bind <你的 MC 用户名> 进行绑定。", userName)
|
||||
time.Sleep(5 * time.Second)
|
||||
KickPlayer(userName)
|
||||
m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("用户:%v 没有绑定,尝试登录已被T出", userName))
|
||||
conf.Bot.Send(m)
|
||||
case StatusBanned:
|
||||
SendMsgToPlayer("你已被封禁,如有疑问请联系管理员。", userName)
|
||||
m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("用户:%v 已被封禁,尝试登录已被T出", userName))
|
||||
conf.Bot.Send(m)
|
||||
default:
|
||||
SendMsgToPlayer("未知错误,请联系管理员,你将被踢出", userName)
|
||||
time.Sleep(3 * time.Second)
|
||||
KickPlayer(userName)
|
||||
m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("用户:%v 登录失败,错误未知,已被T出", userName))
|
||||
conf.Bot.Send(m)
|
||||
}
|
||||
}
|
||||
|
||||
func SendCommand(cmd string) error {
|
||||
var salt int64
|
||||
if err := binary.Read(rand.Reader, binary.BigEndian, &salt); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err := conf.Client.Conn.WritePacket(packet.Marshal(
|
||||
packetid.ServerboundChatCommand,
|
||||
packet.String(cmd),
|
||||
packet.Long(time.Now().UnixMilli()),
|
||||
packet.Long(salt),
|
||||
packet.VarInt(0), // signature
|
||||
packet.VarInt(0),
|
||||
packet.NewFixedBitSet(20),
|
||||
))
|
||||
return err
|
||||
}
|
||||
|
||||
func KickPlayer(userName string) error {
|
||||
err := SendCommand("kick " + userName)
|
||||
return err
|
||||
}
|
||||
|
||||
func CronKick() {
|
||||
utils.CronStart(func() {
|
||||
users := su.GetAlivePlayerList()
|
||||
for _, u := range users {
|
||||
HandleJoinGame(u, false, false)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func isBotMsg(msg chat.Message) bool {
|
||||
return msg.Translate == "commands.message.display.outgoing"
|
||||
}
|
||||
|
||||
func HandleLeftGame(userName string) {
|
||||
u, err := models.GetUserByMCName(userName)
|
||||
if err != nil {
|
||||
logrus.Error("get user name error: ", err)
|
||||
}
|
||||
GetAuthcator().Reject(u)
|
||||
}
|
||||
@@ -13,7 +13,6 @@ import (
|
||||
"github.com/Tnze/go-mc/bot/playerlist"
|
||||
"github.com/Tnze/go-mc/bot/screen"
|
||||
"github.com/Tnze/go-mc/chat"
|
||||
"github.com/Tnze/go-mc/data/item"
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -37,7 +36,6 @@ func Run() error {
|
||||
})
|
||||
conf.ScreenManager = screen.NewManager(client, screen.EventsListener{
|
||||
Open: nil,
|
||||
SetSlot: onScreenSlotChange,
|
||||
Close: nil,
|
||||
})
|
||||
|
||||
@@ -54,27 +52,59 @@ func Run() error {
|
||||
return client.HandleGame()
|
||||
}
|
||||
|
||||
func SendMsg(msg string) error {
|
||||
if err := conf.ChatHandler.SendMessage(msg); err != nil {
|
||||
return err
|
||||
func SendMsg(msg string) {
|
||||
go func() {
|
||||
err := conf.ChatHandler.SendMessage(msg)
|
||||
if err != nil {
|
||||
logrus.Error("send msg error: ", err)
|
||||
}
|
||||
return nil
|
||||
}()
|
||||
}
|
||||
|
||||
func SendMsgToPlayer(msg string, playerName string) {
|
||||
go func() {
|
||||
err := SendCommand(fmt.Sprintf("tell %s %s", playerName, msg))
|
||||
if err != nil {
|
||||
logrus.Error("send msg to player error: ", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func onSystemMsg(msg chat.Message, overlay bool) error {
|
||||
go func() {
|
||||
log.Printf("System: %v", msg)
|
||||
m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("%v", msg))
|
||||
conf.Bot.Send(m)
|
||||
log.Printf("System: %v", msg.String())
|
||||
switch msg.Translate {
|
||||
case EventPlayerJoined:
|
||||
userName, err := GetJoinedPlayer(msg)
|
||||
if err != nil {
|
||||
logrus.Error("user join error ", err)
|
||||
break
|
||||
}
|
||||
go HandleJoinGame(userName, true, true)
|
||||
case EventPlayerLeft:
|
||||
userName, err := GetLeftPlayer(msg)
|
||||
if err != nil {
|
||||
logrus.Error("user left error ", err)
|
||||
break
|
||||
}
|
||||
go HandleLeftGame(userName)
|
||||
default:
|
||||
break
|
||||
}
|
||||
// m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("%v", msg))
|
||||
// conf.Bot.Send(m)
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func onPlayerMsg(msg chat.Message, validated bool) error {
|
||||
go func() {
|
||||
if msg.Translate == "commands.message.display.outgoing" {
|
||||
return
|
||||
}
|
||||
log.Printf("Player: %s", msg)
|
||||
s := strings.Split(msg.String(), " ")
|
||||
if len(s) > 1 {
|
||||
if len(s) > 1 && !isBotMsg(msg) {
|
||||
if s[0] != fmt.Sprintf("<%v>", conf.GetBotSettings().MCBotName) {
|
||||
m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("%v", msg))
|
||||
_, err := conf.Bot.Send(m)
|
||||
@@ -110,30 +140,14 @@ func onDeath() error {
|
||||
}
|
||||
|
||||
func onGameStart() error {
|
||||
go func() {
|
||||
log.Println("Game start")
|
||||
// SendMsgToPlayer("Hello", "test")
|
||||
go CronKick()
|
||||
}()
|
||||
return nil // if err isn't nil, HandleGame() will return it.
|
||||
}
|
||||
|
||||
func onScreenSlotChange(id, index int) error {
|
||||
if id == -2 {
|
||||
log.Printf("Slot: inventory: %v", conf.ScreenManager.Inventory.Slots[index])
|
||||
} else if id == -1 && index == -1 {
|
||||
log.Printf("Slot: cursor: %v", conf.ScreenManager.Cursor)
|
||||
} else {
|
||||
container, ok := conf.ScreenManager.Screens[id]
|
||||
if ok {
|
||||
// Currently, only inventory container is supported
|
||||
switch container.(type) {
|
||||
case *screen.Inventory:
|
||||
slot := container.(*screen.Inventory).Slots[index]
|
||||
itemInfo := item.ByID[item.ID(slot.ID)]
|
||||
log.Printf("Slot: Screen[%d].Slot[%d]: [%v] * %d | NBT: %v", id, index, itemInfo, slot.Count, slot.NBT)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func onHealthChange(health float32, foodLevel int32, foodSaturation float32) error {
|
||||
log.Printf("Health: %.2f, FoodLevel: %d, FoodSaturation: %.2f", health, foodLevel, foodSaturation)
|
||||
return nil
|
||||
|
||||
28
services/tgbot/approve.go
Normal file
28
services/tgbot/approve.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/defs"
|
||||
"tg-mc/models"
|
||||
"tg-mc/services/mc"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func ApproveHandler(update tgbotapi.Update, cmd defs.Command) {
|
||||
u, err := models.GetUserByTGID(update.CallbackQuery.From.ID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
mc.GetAuthcator().Auth(u)
|
||||
callback := tgbotapi.NewCallback(update.CallbackQuery.ID, "已授权")
|
||||
if _, err := conf.Bot.Request(callback); err != nil {
|
||||
logrus.Panic(err)
|
||||
}
|
||||
conf.Bot.Send(tgbotapi.NewDeleteMessage(update.CallbackQuery.Message.Chat.ID,
|
||||
update.CallbackQuery.Message.MessageID))
|
||||
conf.Bot.Send(tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID,
|
||||
fmt.Sprintf("已授权☑️: %s 登录MC", u.MCName)))
|
||||
}
|
||||
29
services/tgbot/bind.go
Normal file
29
services/tgbot/bind.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/models"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func BindHandler(m *tgbotapi.Message, i interface{}) {
|
||||
logrus.Infof("id is %d", m.Chat.ID)
|
||||
err := models.CreateUser(&models.User{
|
||||
TGID: m.From.ID,
|
||||
MCName: m.CommandArguments(),
|
||||
Status: 1,
|
||||
})
|
||||
if err != nil {
|
||||
m := tgbotapi.NewMessage(m.Chat.ID, "绑定失败, err: "+err.Error())
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
|
||||
msg := tgbotapi.NewMessage(m.Chat.ID,
|
||||
fmt.Sprintf("绑定成功,你的MCID是%v", m.CommandArguments()))
|
||||
conf.Bot.Send(msg)
|
||||
return
|
||||
}
|
||||
@@ -1,58 +1,88 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/services/utils"
|
||||
"tg-mc/defs"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func Run(sendFunc func(string) error) {
|
||||
var funcHandlers = map[string]func(*tgbotapi.Message, interface{}){
|
||||
"talk": TalkHandler,
|
||||
"list": ListHandler,
|
||||
"bind": BindHandler,
|
||||
"unbind": UnbindHandler,
|
||||
"get": GetHandler,
|
||||
"set": SetHandler,
|
||||
"kick": KickHandler,
|
||||
"ban": BanHandler,
|
||||
}
|
||||
|
||||
var callBackHandlers = map[string]func(tgbotapi.Update, defs.Command){
|
||||
defs.CMD_APPROVE: ApproveHandler,
|
||||
defs.CMD_REJECT: RejectHandler,
|
||||
}
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
|
||||
api := conf.GetBotSettings().BotAPI
|
||||
if len(api) == 0 {
|
||||
api = tgbotapi.APIEndpoint
|
||||
}
|
||||
|
||||
HttpProxy := conf.GetBotSettings().HTTPProxy
|
||||
proxyUrl, err := url.Parse(HttpProxy)
|
||||
if err != nil {
|
||||
log.Panic(err, "HTTP_PROXY environment variable is not set correctly")
|
||||
}
|
||||
|
||||
if len(HttpProxy) != 0 {
|
||||
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}}
|
||||
conf.Bot, err = tgbotapi.NewBotAPIWithClient(
|
||||
conf.GetBotSettings().BotToken,
|
||||
tgbotapi.APIEndpoint,
|
||||
api,
|
||||
client)
|
||||
} else {
|
||||
conf.Bot, err = tgbotapi.NewBotAPIWithAPIEndpoint(conf.GetBotSettings().BotToken, api)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
conf.Bot.Debug = false
|
||||
|
||||
log.Printf("Authorized on account %s", conf.Bot.Self.UserName)
|
||||
}
|
||||
|
||||
func Run(sendFunc func(string), cmdFunc func(string) error) {
|
||||
u := tgbotapi.NewUpdate(0)
|
||||
u.Timeout = 60
|
||||
updates := conf.Bot.GetUpdatesChan(u)
|
||||
|
||||
for update := range updates {
|
||||
if update.Message != nil {
|
||||
logrus.Infof("[%s] %s", update.Message.From.UserName, update.Message.Text)
|
||||
if update.Message.Command() == "talk" {
|
||||
logrus.Infof("id is %d", update.Message.Chat.ID)
|
||||
m := fmt.Sprintf("%v: %v", update.Message.From.UserName, update.Message.CommandArguments())
|
||||
err := sendFunc(m)
|
||||
logrus.WithError(err).Error("send message error")
|
||||
if update.CallbackQuery != nil {
|
||||
go func(update tgbotapi.Update) {
|
||||
logrus.Infof("[%s] %s", update.CallbackQuery.From.UserName, update.CallbackQuery.Data)
|
||||
cmd, err := defs.NewCommandFromJSON(update.CallbackQuery.Data)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
return
|
||||
}
|
||||
if update.Message.Command() == "list" {
|
||||
logrus.Infof("id is %d", update.Message.Chat.ID)
|
||||
m := tgbotapi.NewMessage(update.Message.Chat.ID, utils.GetAlivePlayer())
|
||||
conf.Bot.Send(m)
|
||||
if handler, ok := callBackHandlers[cmd.Command]; ok {
|
||||
handler(update, *cmd)
|
||||
}
|
||||
}(update)
|
||||
} else if update.Message != nil {
|
||||
go func(m *tgbotapi.Message) {
|
||||
logrus.Infof("[%s] %s", m.From.UserName, m.Text)
|
||||
if handler, ok := funcHandlers[m.Command()]; ok {
|
||||
handler(m, sendFunc)
|
||||
}
|
||||
}(update.Message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
71
services/tgbot/get.go
Normal file
71
services/tgbot/get.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/models"
|
||||
"tg-mc/services/utils"
|
||||
commonUtils "tg-mc/utils"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func GetHandler(msg *tgbotapi.Message, i interface{}) {
|
||||
if !utils.IsAdmin(msg) &&
|
||||
len(msg.CommandArguments()) != 0 {
|
||||
tm := tgbotapi.NewMessage(msg.Chat.ID, "您不是管理员,没有该权限")
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
} else if utils.IsAdmin(msg) &&
|
||||
len(msg.CommandArguments()) != 0 {
|
||||
a := commonUtils.GetArgs(msg.CommandArguments())
|
||||
if len(a) != 2 {
|
||||
tm := tgbotapi.NewMessage(msg.Chat.ID, "参数错误,样例:\n```\n/get <tgid|username> <value>\n```")
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
}
|
||||
if a[0] == "tgid" {
|
||||
tgid, err := strconv.ParseInt(a[1], 10, 64)
|
||||
if err != nil {
|
||||
conf.Bot.Send(tgbotapi.NewMessage(msg.Chat.ID, "ID错误,应该为int64"))
|
||||
return
|
||||
}
|
||||
u, err := models.GetUserByTGID(tgid)
|
||||
if err != nil {
|
||||
tm := tgbotapi.NewMessage(msg.Chat.ID, fmt.Sprintf("查询出错,err:\n```\n%+v\n```", err))
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
}
|
||||
tm := tgbotapi.NewMessage(msg.Chat.ID, fmt.Sprintf("用户信息:\n```\n%+v\n```", u))
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
}
|
||||
if a[0] == "username" {
|
||||
u, err := models.GetUserByMCName(a[1])
|
||||
if err != nil {
|
||||
tm := tgbotapi.NewMessage(msg.Chat.ID, fmt.Sprintf("查询出错,err:\n```\n%+v\n```", err))
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
}
|
||||
tm := tgbotapi.NewMessage(msg.Chat.ID, fmt.Sprintf("用户信息:\n```\n%+v\n```", u))
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
}
|
||||
return
|
||||
}
|
||||
logrus.Infof("id is %d", msg.Chat.ID)
|
||||
u, err := models.GetUserByTGID(msg.From.ID)
|
||||
if err != nil {
|
||||
m := tgbotapi.NewMessage(msg.Chat.ID, "你还没有绑定")
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
m := tgbotapi.NewMessage(msg.Chat.ID, fmt.Sprintf("你的MCID是%v", u.MCName))
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
30
services/tgbot/kick.go
Normal file
30
services/tgbot/kick.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/models"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
)
|
||||
|
||||
func KickHandler(m *tgbotapi.Message, i interface{}) {
|
||||
f, ok := i.(func(string) error)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
u, err := models.GetUserByTGID(m.From.ID)
|
||||
if err != nil {
|
||||
conf.Bot.Send(tgbotapi.NewMessage(m.Chat.ID, "您还没有绑定账号,请先绑定"))
|
||||
return
|
||||
}
|
||||
|
||||
err = f(fmt.Sprintf("kick %s", u.MCName))
|
||||
if err != nil {
|
||||
conf.Bot.Send(tgbotapi.NewMessage(m.Chat.ID, err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
conf.Bot.Send(tgbotapi.NewMessage(m.Chat.ID, fmt.Sprintf("已踢出用户 %s", u.MCName)))
|
||||
}
|
||||
15
services/tgbot/list.go
Normal file
15
services/tgbot/list.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"tg-mc/conf"
|
||||
"tg-mc/services/utils"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func ListHandler(m *tgbotapi.Message, i interface{}) {
|
||||
logrus.Infof("id is %d", m.Chat.ID)
|
||||
msg := tgbotapi.NewMessage(m.Chat.ID, utils.GetAlivePlayer())
|
||||
conf.Bot.Send(msg)
|
||||
}
|
||||
26
services/tgbot/reject.go
Normal file
26
services/tgbot/reject.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/defs"
|
||||
"tg-mc/models"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func RejectHandler(update tgbotapi.Update, cmd defs.Command) {
|
||||
u, err := models.GetUserByTGID(update.CallbackQuery.From.ID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
callback := tgbotapi.NewCallback(update.CallbackQuery.ID, "已拒绝")
|
||||
if _, err := conf.Bot.Request(callback); err != nil {
|
||||
logrus.Panic(err)
|
||||
}
|
||||
conf.Bot.Send(tgbotapi.NewDeleteMessage(update.CallbackQuery.Message.Chat.ID,
|
||||
update.CallbackQuery.Message.MessageID))
|
||||
conf.Bot.Send(tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID,
|
||||
fmt.Sprintf("已拒绝❌: %s 登录MC", u.MCName)))
|
||||
}
|
||||
71
services/tgbot/set.go
Normal file
71
services/tgbot/set.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/models"
|
||||
"tg-mc/services/utils"
|
||||
commonUtils "tg-mc/utils"
|
||||
"time"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func SetHandler(m *tgbotapi.Message, i interface{}) {
|
||||
var a []string
|
||||
if t, ok := i.([]string); ok {
|
||||
a = t
|
||||
} else {
|
||||
a = commonUtils.GetArgs(m.CommandArguments())
|
||||
}
|
||||
|
||||
if !utils.IsAdmin(m) {
|
||||
tm := tgbotapi.NewMessage(m.Chat.ID, "您不是管理员,没有该权限")
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
}
|
||||
if len(a) != 3 {
|
||||
tm := tgbotapi.NewMessage(m.Chat.ID, "参数错误,样例:\n```\n/set username tgid status\n```")
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
}
|
||||
tgid, err := strconv.ParseInt(a[1], 10, 64)
|
||||
if err != nil {
|
||||
conf.Bot.Send(tgbotapi.NewMessage(m.Chat.ID, "ID错误,应该为int64"))
|
||||
return
|
||||
}
|
||||
status, err := strconv.ParseInt(a[2], 10, 64)
|
||||
if err != nil || !lo.Contains([]int64{0, 1, 2}, status) {
|
||||
conf.Bot.Send(tgbotapi.NewMessage(m.Chat.ID, "Status错误,应该为0(Pending),1(Normal),2(Banned)"))
|
||||
return
|
||||
}
|
||||
if err := models.CreateUser(&models.User{
|
||||
TGID: tgid,
|
||||
MCName: a[0],
|
||||
Status: int(status),
|
||||
}); err != nil {
|
||||
tm := tgbotapi.NewMessage(m.Chat.ID, fmt.Sprintf("创建用户错误,err:\n```\n%+v\n```", err))
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
}
|
||||
conf.Bot.Send(tgbotapi.NewMessage(m.Chat.ID, "设置用户成功"))
|
||||
}
|
||||
|
||||
func BanHandler(m *tgbotapi.Message, i interface{}) {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
randomNumber := rand.Intn(11)
|
||||
for {
|
||||
_, err := models.GetUserByTGID(int64(randomNumber))
|
||||
if err != nil {
|
||||
break
|
||||
} else {
|
||||
randomNumber = rand.Intn(11)
|
||||
}
|
||||
}
|
||||
SetHandler(m, []string{m.CommandArguments(), fmt.Sprintf("-%d", randomNumber), "2"})
|
||||
}
|
||||
15
services/tgbot/talk.go
Normal file
15
services/tgbot/talk.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func TalkHandler(m *tgbotapi.Message, i interface{}) {
|
||||
sendFunc := i.(func(string))
|
||||
logrus.Infof("id is %d", m.Chat.ID)
|
||||
msg := fmt.Sprintf("%v: %v", m.From.UserName, m.CommandArguments())
|
||||
sendFunc(msg)
|
||||
}
|
||||
28
services/tgbot/unbind.go
Normal file
28
services/tgbot/unbind.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"tg-mc/conf"
|
||||
"tg-mc/models"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func UnbindHandler(msg *tgbotapi.Message, i interface{}) {
|
||||
logrus.Infof("id is %d", msg.Chat.ID)
|
||||
u, err := models.GetUserByTGID(msg.From.ID)
|
||||
if err != nil {
|
||||
m := tgbotapi.NewMessage(msg.Chat.ID, "你还没有绑定")
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
err = u.Delete(msg.From.ID)
|
||||
if err != nil {
|
||||
m := tgbotapi.NewMessage(msg.Chat.ID, "解绑失败")
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
m := tgbotapi.NewMessage(msg.Chat.ID, "解绑成功")
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
@@ -9,3 +9,14 @@ func GetAlivePlayer() string {
|
||||
}
|
||||
return ans
|
||||
}
|
||||
|
||||
func GetAlivePlayerList() []string {
|
||||
ans := []string{}
|
||||
for _, v := range conf.PlayerList.PlayerInfos {
|
||||
if v.Name == conf.GetBotSettings().MCBotName {
|
||||
continue
|
||||
}
|
||||
ans = append(ans, v.Name)
|
||||
}
|
||||
return ans
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"tg-mc/conf"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func SendMsg(msg string) error {
|
||||
@@ -11,3 +12,8 @@ func SendMsg(msg string) error {
|
||||
_, err := conf.Bot.Send(msgT)
|
||||
return err
|
||||
}
|
||||
|
||||
func IsAdmin(m *tgbotapi.Message) bool {
|
||||
return lo.Contains(conf.GetBotSettings().AdminID, m.From.ID) ||
|
||||
lo.Contains(conf.GetBotSettings().AdminID, m.Chat.ID)
|
||||
}
|
||||
|
||||
7
utils/args.go
Normal file
7
utils/args.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package utils
|
||||
|
||||
import "strings"
|
||||
|
||||
func GetArgs(i string) []string {
|
||||
return strings.Split(i, " ")
|
||||
}
|
||||
16
utils/cron.go
Normal file
16
utils/cron.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/go-co-op/gocron"
|
||||
)
|
||||
|
||||
var (
|
||||
s = gocron.NewScheduler(time.UTC)
|
||||
)
|
||||
|
||||
func CronStart(f func()) {
|
||||
s.Every("1m").Do(f)
|
||||
s.StartAsync()
|
||||
}
|
||||
24
utils/database/db.go
Normal file
24
utils/database/db.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func init() {
|
||||
godotenv.Load()
|
||||
initSqlite()
|
||||
}
|
||||
|
||||
func GetDB() *gorm.DB {
|
||||
return GetSqlite()
|
||||
}
|
||||
|
||||
func CloseDB(db *gorm.DB) {
|
||||
tdb, err := db.DB()
|
||||
if err != nil {
|
||||
logrus.WithError(err).Errorf("Close DB error")
|
||||
}
|
||||
tdb.Close()
|
||||
}
|
||||
31
utils/database/sqlite.go
Normal file
31
utils/database/sqlite.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"tg-mc/conf"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func initSqlite() {
|
||||
var err error
|
||||
godotenv.Load()
|
||||
|
||||
dbPath := conf.GetBotSettings().DBPath
|
||||
db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
|
||||
if err != nil {
|
||||
logrus.Panic(err, "Initializing DB Error")
|
||||
}
|
||||
CloseDB(db)
|
||||
}
|
||||
|
||||
func GetSqlite() *gorm.DB {
|
||||
dbPath := conf.GetBotSettings().DBPath
|
||||
db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return db
|
||||
}
|
||||
Reference in New Issue
Block a user