feat: add msg name

This commit is contained in:
Vaala Cat 2023-05-17 16:14:11 +08:00
parent b7e73e8c31
commit e516a51ebe
3 changed files with 37 additions and 35 deletions

View File

@ -7,10 +7,12 @@ import (
) )
func Run() { func Run() {
go tgbot.Run(mc.SendMsg) go func() {
for { for {
if err := mc.Run(); err != nil { if err := mc.Run(); err != nil {
utils.SendMsg("致命错误:" + err.Error()) utils.SendMsg("致命错误:" + err.Error())
} }
} }
}()
tgbot.Run(mc.SendMsg)
} }

View File

@ -1,7 +1,6 @@
package mc package mc
import ( import (
"errors"
"fmt" "fmt"
"log" "log"
"strings" "strings"
@ -16,6 +15,7 @@ import (
"github.com/Tnze/go-mc/chat" "github.com/Tnze/go-mc/chat"
"github.com/Tnze/go-mc/data/item" "github.com/Tnze/go-mc/data/item"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/sirupsen/logrus"
) )
func Run() error { func Run() error {
@ -51,17 +51,7 @@ func Run() error {
log.Println("Login success") log.Println("Login success")
var perr bot.PacketHandlerError return client.HandleGame()
for {
if err = client.HandleGame(); err == nil {
return errors.New("handle game error")
}
if errors.As(err, &perr) {
return err
} else {
return err
}
}
} }
func SendMsg(msg string) error { func SendMsg(msg string) error {
@ -72,28 +62,37 @@ func SendMsg(msg string) error {
} }
func onSystemMsg(msg chat.Message, overlay bool) error { func onSystemMsg(msg chat.Message, overlay bool) error {
go func() {
log.Printf("System: %v", msg) log.Printf("System: %v", msg)
m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("%v", msg)) m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("%v", msg))
conf.Bot.Send(m) conf.Bot.Send(m)
}()
return nil return nil
} }
func onPlayerMsg(msg chat.Message, validated bool) error { func onPlayerMsg(msg chat.Message, validated bool) error {
go func() {
log.Printf("Player: %s", msg) log.Printf("Player: %s", msg)
s := strings.Split(msg.String(), " ") s := strings.Split(msg.String(), " ")
if len(s) > 1 { if len(s) > 1 {
if s[0] != fmt.Sprintf("<%v>", conf.GetBotSettings().MCBotName) { if s[0] != fmt.Sprintf("<%v>", conf.GetBotSettings().MCBotName) {
m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("%v", msg)) m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("%v", msg))
conf.Bot.Send(m) _, err := conf.Bot.Send(m)
if err != nil {
logrus.Error(err)
} }
} }
}
}()
return nil return nil
} }
func onDisguisedMsg(msg chat.Message) error { func onDisguisedMsg(msg chat.Message) error {
go func() {
log.Printf("Disguised: %v", msg) log.Printf("Disguised: %v", msg)
m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("%v", msg)) m := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, fmt.Sprintf("%v", msg))
conf.Bot.Send(m) conf.Bot.Send(m)
}()
return nil return nil
} }
@ -112,9 +111,6 @@ func onDeath() error {
func onGameStart() error { func onGameStart() error {
log.Println("Game start") log.Println("Game start")
if err := conf.ChatHandler.SendMessage("Hello, world"); err != nil {
return err
}
return nil // if err isn't nil, HandleGame() will return it. return nil // if err isn't nil, HandleGame() will return it.
} }
@ -153,5 +149,6 @@ func (d DisconnectErr) Error() string {
func onDisconnect(reason chat.Message) error { func onDisconnect(reason chat.Message) error {
// return an error value so that we can stop main loop // return an error value so that we can stop main loop
logrus.Error("Disconnected: ", reason)
return DisconnectErr{Reason: reason} return DisconnectErr{Reason: reason}
} }

View File

@ -1,6 +1,7 @@
package tgbot package tgbot
import ( import (
"fmt"
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
@ -30,7 +31,7 @@ func Run(sendFunc func(string) error) {
log.Panic(err) log.Panic(err)
} }
conf.Bot.Debug = true conf.Bot.Debug = false
log.Printf("Authorized on account %s", conf.Bot.Self.UserName) log.Printf("Authorized on account %s", conf.Bot.Self.UserName)
@ -43,7 +44,9 @@ func Run(sendFunc func(string) error) {
logrus.Infof("[%s] %s", update.Message.From.UserName, update.Message.Text) logrus.Infof("[%s] %s", update.Message.From.UserName, update.Message.Text)
if update.Message.Command() == "talk" { if update.Message.Command() == "talk" {
logrus.Infof("id is %d", update.Message.Chat.ID) logrus.Infof("id is %d", update.Message.Chat.ID)
sendFunc(update.Message.CommandArguments()) m := fmt.Sprintf("%v: %v", update.Message.From.UserName, update.Message.CommandArguments())
err := sendFunc(m)
logrus.WithError(err).Error("send message error")
} }
if update.Message.Command() == "list" { if update.Message.Command() == "list" {
logrus.Infof("id is %d", update.Message.Chat.ID) logrus.Infof("id is %d", update.Message.Chat.ID)