Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions bot/bot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package bot

import (
"fmt"
"github.com/Szzrain/Milky-go-sdk"
"github.com/ratel-online/core/log"
)

// Session 全局机器人会话
var Session *Milky_go_sdk.Session

// GroupID 群ID
var GroupID int64

// Logger 实现 Milky_go_sdk 的 Logger 接口
type Logger struct{}

func (l *Logger) Infof(format string, args ...interface{}) {
log.Infof(format, args...)
}

func (l *Logger) Errorf(format string, args ...interface{}) {
log.Errorf(format, args...)
}

func (l *Logger) Debugf(format string, args ...interface{}) {
_ = format
_ = args
}

func (l *Logger) Warnf(format string, args ...interface{}) {
log.Infof(format, args...)
}

func (l *Logger) Info(args ...interface{}) {
log.Info(fmt.Sprint(args...))
}

func (l *Logger) Error(args ...interface{}) {
log.Error(fmt.Sprint(args...))
}

func (l *Logger) Debug(args ...interface{}) {
_ = args
}

func (l *Logger) Warn(args ...interface{}) {
log.Info(fmt.Sprint(args...))
}

// SendGroupMessage 发送群消息
func SendGroupMessage(groupID int64, content string) error {
if Session == nil {
return fmt.Errorf("bot not connected")
}
var message []Milky_go_sdk.IMessageElement
message = append(message, &Milky_go_sdk.TextElement{Text: content})
_, err := Session.SendGroupMessage(groupID, &message)
return err
}

// Connect 连接机器人
func Connect(addr, token string, groupID int64) error {
GroupID = groupID
m, err := Milky_go_sdk.New("ws://"+addr+"/event", "http://"+addr+"/api", token, &Logger{})
if err != nil {
return fmt.Errorf("创建Bot会话失败: %v", err)
}
err = m.Open()
if err != nil {
return fmt.Errorf("连接Bot失败: %v", err)
}
Session = m
log.Infof("Bot已连接: %s", addr)
return nil
}

// Close 关闭机器人连接
func Close() {
if Session != nil {
Session.Close()
}
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ require (
github.com/awesome-cap/hashmap v0.0.0-20211211100532-e3300ac4ae14
github.com/feel-easy/mahjong v0.0.0-20220721030133-7a0f4032c008
github.com/feel-easy/uno v0.0.0-20220721061415-e6a3189cfd70
github.com/gorilla/websocket v1.4.2
github.com/gorilla/websocket v1.5.3
github.com/ratel-online/core v0.0.0-20250225062905-81b6faff6d25
)

require (
github.com/Szzrain/Milky-go-sdk v1.0.1 // indirect
github.com/awesome-cap/im v0.0.0-20210720090440-7556eb92965d // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/Szzrain/Milky-go-sdk v1.0.1 h1:dsKIUtW7lI7RGLOuNut2ChxqQpM+bqd+3AafCIIhYRc=
github.com/Szzrain/Milky-go-sdk v1.0.1/go.mod h1:vyl5G/6TQha+Ygf9ZWkDN//7dJFq2V0ezdk3Rm2kDKQ=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/awesome-cap/hashmap v0.0.0-20211211100532-e3300ac4ae14 h1:YH6IXdLClQbuKrhYdyXYzdE9cicwU7HaD7YDMmo0oRY=
github.com/awesome-cap/hashmap v0.0.0-20211211100532-e3300ac4ae14/go.mod h1:5vIRKw3P2HirHOPm1xQfJ6GmPfZQemzbIXfU0MqLlZU=
Expand All @@ -16,6 +18,8 @@ github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Z
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-immutable-radix v1.0.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM=
Expand Down
33 changes: 29 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,46 @@ package main

import (
"flag"
"fmt"
"strconv"

"github.com/ratel-online/core/log"
"github.com/ratel-online/core/util/async"
"github.com/ratel-online/server/bot"
"github.com/ratel-online/server/network"
"strconv"
)

var (
Wsport int
Tcpport int
Wsport int
Tcpport int
BotAddr string
BotToken string
BotGroup int64
)

func main() {
flag.IntVar(&Wsport, "w", 9998, "WebsocketServer Port")
flag.IntVar(&Tcpport, "t", 9999, "TcpServer Port")
flag.StringVar(&BotAddr, "bot", "", "Bot connection address")
flag.StringVar(&BotToken, "bot-token", "", "Bot token")
flag.Int64Var(&BotGroup, "bot-group", 0, "Bot group ID")

flag.Parse()
// 连接机器人
if BotAddr != "" && BotToken != "" && BotGroup != 0 {
err := bot.Connect(BotAddr, BotToken, BotGroup)
if err != nil {
log.Panic(fmt.Sprintf("连接Bot失败: %v", err))
}
// 发送测试消息到 BotGroup 群
err = bot.SendGroupMessage(BotGroup, "Server started!")
if err != nil {
log.Errorf("发送群消息失败: %v", err)
} else {
log.Infof("已发送群消息到 %d", BotGroup)
}
defer bot.Close()
}

async.Async(func() {
wsServer := network.NewWebsocketServer(":" + strconv.Itoa(Wsport))
Expand All @@ -25,4 +50,4 @@ func main() {

server := network.NewTcpServer(":" + strconv.Itoa(Tcpport))
log.Panic(server.Serve())
}
}
2 changes: 2 additions & 0 deletions state/game/texas/round.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/ratel-online/core/model"
"github.com/ratel-online/core/util/poker"
"github.com/ratel-online/server/bot"
"github.com/ratel-online/server/consts"
"github.com/ratel-online/server/database"
)
Expand Down Expand Up @@ -33,6 +34,7 @@ func preFlopRound(game *database.Texas) error {
if player.Amount < 100 {
player.Amount += 2000
database.Broadcast(game.Room.ID, fmt.Sprintf("%s is too poor, system give him 2000\n", player.Name))
bot.SendGroupMessage(bot.GroupID, fmt.Sprintf("%s is too poor, system give him 2000", player.Name))
}
}

Expand Down
Loading