diff --git a/.gitignore b/.gitignore index 25dc6c9..b5b1db9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .env run.sh -mcbot \ No newline at end of file +mcbot +*.sqlite \ No newline at end of file diff --git a/conf/env.go b/conf/env.go index 16c64dd..57c90d5 100644 --- a/conf/env.go +++ b/conf/env.go @@ -6,13 +6,14 @@ import ( ) type botSettings struct { - 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"` + 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 ( diff --git a/db.sqlite b/db.sqlite deleted file mode 100644 index 8e31b19..0000000 Binary files a/db.sqlite and /dev/null differ diff --git a/go.mod b/go.mod index 4451248..653f5a3 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,11 @@ go 1.20 require ( github.com/Tnze/go-mc v1.19.4-pre1 github.com/glebarez/sqlite v1.8.0 + github.com/go-co-op/gocron v1.28.3 github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 + github.com/ilyakaznacheev/cleanenv v1.4.2 github.com/joho/godotenv v1.5.1 + github.com/samber/lo v1.38.1 github.com/sirupsen/logrus v1.9.1 gorm.io/gorm v1.25.1 ) @@ -15,16 +18,14 @@ require ( github.com/BurntSushi/toml v1.3.2 // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/glebarez/go-sqlite v1.21.1 // indirect - github.com/go-co-op/gocron v1.28.3 // indirect github.com/google/uuid v1.3.0 // indirect - github.com/iancoleman/strcase v0.2.0 // indirect - github.com/ilyakaznacheev/cleanenv v1.4.2 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/mattn/go-isatty v0.0.17 // 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.9.0 // indirect + golang.org/x/exp v0.0.0-20230321023759-10a507213a29 // indirect golang.org/x/sys v0.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect modernc.org/libc v1.22.3 // indirect diff --git a/go.sum b/go.sum index b9514d6..83af5dd 100644 --- a/go.sum +++ b/go.sum @@ -18,8 +18,6 @@ github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2 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/ilyakaznacheev/cleanenv v1.4.2 h1:nRqiriLMAC7tz7GzjzUTBHfzdzw6SQ7XvTagkFqe/zU= github.com/ilyakaznacheev/cleanenv v1.4.2/go.mod h1:i0owW+HDxeGKE0/JPREJOdSCPIyOnmh6C0xhWAkF/xA= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= @@ -31,9 +29,11 @@ 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= @@ -46,29 +46,34 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qq 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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 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 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 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= +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/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= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 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 h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= 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= diff --git a/services/mc/mc.go b/services/mc/mc.go index 01724c8..be5acd4 100644 --- a/services/mc/mc.go +++ b/services/mc/mc.go @@ -92,7 +92,7 @@ func onSystemMsg(msg chat.Message, overlay bool) error { func onPlayerMsg(msg chat.Message, validated bool) error { go func() { - if msg.Translate != "commands.message.display.incoming" { + if msg.Translate == "commands.message.display.outgoing" { return } log.Printf("Player: %s", msg) diff --git a/services/tgbot/bot.go b/services/tgbot/bot.go index 174b219..60acc3e 100644 --- a/services/tgbot/bot.go +++ b/services/tgbot/bot.go @@ -5,11 +5,14 @@ import ( "log" "net/http" "net/url" + "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/samber/lo" "github.com/sirupsen/logrus" ) @@ -102,6 +105,51 @@ func Run(sendFunc func(string)) { return } if m.Command() == "get" { + if !utils.IsAdmin(m) && + len(m.CommandArguments()) != 0 { + tm := tgbotapi.NewMessage(m.Chat.ID, "您不是管理员,没有该权限") + conf.Bot.Send(tm) + return + } else if utils.IsAdmin(m) && + len(m.CommandArguments()) != 0 { + a := commonUtils.GetArgs(m.CommandArguments()) + if len(a) != 2 { + tm := tgbotapi.NewMessage(m.Chat.ID, "参数错误,样例:\n```\n/get \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(m.Chat.ID, "ID错误,应该为int64")) + return + } + u, err := models.GetUserByTGID(tgid) + if err != nil { + tm := tgbotapi.NewMessage(m.Chat.ID, fmt.Sprintf("查询出错,err:\n```\n%+v\n```", err)) + tm.ParseMode = "Markdown" + conf.Bot.Send(tm) + return + } + tm := tgbotapi.NewMessage(m.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(m.Chat.ID, fmt.Sprintf("查询出错,err:\n```\n%+v\n```", err)) + tm.ParseMode = "Markdown" + conf.Bot.Send(tm) + return + } + tm := tgbotapi.NewMessage(m.Chat.ID, fmt.Sprintf("用户信息:\n```\n%+v\n```", u)) + tm.ParseMode = "Markdown" + conf.Bot.Send(tm) + } + return + } logrus.Infof("id is %d", m.Chat.ID) u, err := models.GetUserByTGID(m.From.ID) if err != nil { @@ -113,6 +161,42 @@ func Run(sendFunc func(string)) { conf.Bot.Send(m) return } + if m.Command() == "set" { + if !utils.IsAdmin(m) { + tm := tgbotapi.NewMessage(m.Chat.ID, "您不是管理员,没有该权限") + conf.Bot.Send(tm) + return + } + a := commonUtils.GetArgs(m.CommandArguments()) + 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, "设置用户成功")) + return + } }(update.Message) } } diff --git a/services/utils/tg.go b/services/utils/tg.go index 67a3ece..bfade3e 100644 --- a/services/utils/tg.go +++ b/services/utils/tg.go @@ -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) +} diff --git a/utils/args.go b/utils/args.go new file mode 100644 index 0000000..977fdaf --- /dev/null +++ b/utils/args.go @@ -0,0 +1,7 @@ +package utils + +import "strings" + +func GetArgs(i string) []string { + return strings.Split(i, " ") +}