first commit

This commit is contained in:
Vaala Cat 2023-05-17 14:36:40 +08:00
commit 854341b902
9 changed files with 280 additions and 0 deletions

1
.gitingore Normal file
View File

@ -0,0 +1 @@
.env

11
Dockerfile Normal file
View File

@ -0,0 +1,11 @@
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/
FROM git.vaala.cloud/vaalacat/alpine
COPY --from=builder /app/mcbot /app/mcbot
ENTRYPOINT [ "/app/mcbot" ]

51
conf/env.go Normal file
View File

@ -0,0 +1,51 @@
package conf
import (
"os"
"strconv"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
)
type botSettings struct {
HTTPProxy string
BotToken string
MCServer string
MCBotName string
GroupID int64
}
var (
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,
}
}
func GetBotSettings() *botSettings {
return botSettingsInstance
}

15
go.mod Normal file
View File

@ -0,0 +1,15 @@
module tg-mc
go 1.20
require (
github.com/Tnze/go-mc v1.19.4-pre1
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/joho/godotenv v1.5.1
github.com/sirupsen/logrus v1.9.1
)
require (
github.com/google/uuid v1.3.0 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
)

23
go.sum Normal file
View File

@ -0,0 +1,23 @@
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/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/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/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
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/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/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
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=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/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/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

12
main.go Normal file
View File

@ -0,0 +1,12 @@
package main
import (
"tg-mc/services"
"github.com/joho/godotenv"
)
func main() {
godotenv.Load()
services.Run()
}

18
services/handler.go Normal file
View File

@ -0,0 +1,18 @@
package services
import (
"tg-mc/services/mc"
"tg-mc/services/tgbot"
"github.com/sirupsen/logrus"
)
func Run() {
go tgbot.Run(mc.SendMsg)
for {
if err := mc.Run(); err != nil {
tgbot.SendMsg("致命错误:" + err.Error())
logrus.Panic(err)
}
}
}

90
services/mc/mc.go Normal file
View File

@ -0,0 +1,90 @@
package mc
import (
"errors"
"fmt"
"log"
"strings"
"tg-mc/conf"
"tg-mc/services/tgbot"
"github.com/Tnze/go-mc/bot"
"github.com/Tnze/go-mc/bot/basic"
"github.com/Tnze/go-mc/bot/msg"
"github.com/Tnze/go-mc/bot/playerlist"
"github.com/Tnze/go-mc/chat"
"github.com/sirupsen/logrus"
)
var (
client *bot.Client
player *basic.Player
chatHandler *msg.Manager
playerList *playerlist.PlayerList
)
func Run() error {
client = bot.NewClient()
client.Auth.Name = conf.GetBotSettings().MCBotName
player = basic.NewPlayer(client, basic.DefaultSettings, basic.EventsListener{})
playerList = playerlist.New(client)
chatHandler = msg.New(client, player, playerList, msg.EventsHandler{
SystemChat: onSystemMsg,
PlayerChatMessage: onPlayerMsg,
DisguisedChat: onDisguisedMsg,
})
err := client.JoinServer(
conf.GetBotSettings().MCServer,
)
if err != nil {
log.Fatal(err)
}
log.Println("Login success")
var perr bot.PacketHandlerError
for {
if err = client.HandleGame(); err == nil {
return errors.New("handle game error")
}
if errors.As(err, &perr) {
log.Print(perr)
} else {
return err
}
}
}
func SendMsg(msg string) error {
if err := chatHandler.SendMessage(msg); err != nil {
return err
}
return nil
}
func onSystemMsg(msg chat.Message, overlay bool) error {
log.Printf("System: %v", msg)
tgbot.SendMsg(msg.String())
return nil
}
func onPlayerMsg(msg chat.Message, validated bool) error {
log.Printf("Player: %s", msg)
s := strings.Split(msg.String(), " ")
if len(s) > 1 {
logrus.Error(s[0])
if s[0] != fmt.Sprintf("<%v>", conf.GetBotSettings().MCBotName) {
tgbot.SendMsg(msg.String())
}
}
return nil
}
func onDisguisedMsg(msg chat.Message) error {
log.Printf("Disguised: %v", msg)
tgbot.SendMsg(msg.String())
return nil
}

59
services/tgbot/bot.go Normal file
View File

@ -0,0 +1,59 @@
package tgbot
import (
"log"
"net/http"
"net/url"
"tg-mc/conf"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/sirupsen/logrus"
)
var (
bot *tgbotapi.BotAPI
)
func Run(sendFunc func(string) error) {
var err error
HttpProxy := conf.GetBotSettings().HTTPProxy
proxyUrl, err := url.Parse(HttpProxy)
if err != nil {
log.Panic(err, "HTTP_PROXY environment variable is not set correctly")
}
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}}
bot, err = tgbotapi.NewBotAPIWithClient(
conf.GetBotSettings().BotToken,
tgbotapi.APIEndpoint,
client)
if err != nil {
log.Panic(err)
}
bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := 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)
sendFunc(update.Message.CommandArguments())
}
}
}
}
func SendMsg(msg string) error {
msgT := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, msg)
_, err := bot.Send(msgT)
return err
}