Skip to content

Commit

Permalink
feat: 发送消息action支持
Browse files Browse the repository at this point in the history
  • Loading branch information
snowykami committed Mar 16, 2024
1 parent 9589ee8 commit e2d714a
Show file tree
Hide file tree
Showing 10 changed files with 253 additions and 155 deletions.
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,20 @@ $$ $$/ $$ | $$ |$$ |$$ $$/ $$ $$/ $$ $$/
return
}

eventChan := make(chan common.Event, 10000)
eventChan := make(chan common.Event, 1)
sendChan := make(chan common.Event, 1)
clientManager := minecraft.NewClientManager(*config)
clientManager.EventChan = eventChan
clientManager.SendChan = sendChan
go clientManager.Run()

botManager, err := onebot.NewBotManager(*config)
botManager.EventChan = eventChan
botManager.SendChan = sendChan
if err != nil {
common.Logger.Warnf("初始化 OneBot 失败: %v", err)
}
go botManager.Run()
//go botManager.OpenEventChan()

select {}
}

Expand Down
16 changes: 10 additions & 6 deletions pkg/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ import (

// Config 配置
type Config struct {
Common CommConfig `yaml:"common"` // 通用配置
Servers map[string]ServerConfig `yaml:"servers"` // 服务器列表
Auth map[string]AuthConfig `yaml:"auth"` // 验证信息
Onebot OnebotConfig `yaml:"onebot"` // Onebot 配置列表
Common CommConfig `yaml:"common"` // 通用配置
Servers []ServerConfig `yaml:"servers"` // 服务器列表
Auth map[string]AuthConfig `yaml:"auth"` // 验证信息
Onebot OnebotConfig `yaml:"onebot"` // Onebot 配置列表
}

// CommConfig 通用配置
type CommConfig struct {
JoinInterval int `yaml:"join_interval"` // 加入间隔,单位秒,建议长一点
JoinInterval int `yaml:"join_interval"` // 加入间隔,单位秒,建议长一点
IllegalChar []string `yaml:"illegal_char"` // 非法字符
}

// ServerConfig 服务器配置
type ServerConfig struct {
Name string `yaml:"name"` // 服务器名称
ID int64 `yaml:"id"` // 服务器ID
Address string `yaml:"address"`
ReconnectInterval int `yaml:"reconnect_interval"` // 重连间隔,单位秒,建议长一点
Auth string `yaml:"auth"` // 验证信息
Expand Down Expand Up @@ -53,7 +56,8 @@ type OnebotConfig struct {

// BotConfig 本地机器人配置
type BotConfig struct {
SelfID string `yaml:"self_id"` // 机器人 QQ 号
Nickname string `yaml:"nickname"` // 机器人 QQ 号
SelfID int64 `yaml:"self_id"` // 机器人 QQ 号
HeartbeatInterval int `yaml:"heartbeat_interval"` // 心跳间隔, 单位为秒
PlayerIDType string `yaml:"player_id_type"` // 玩家号传输类型
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/common/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ type Event struct {
Type string
SubType string
DetailType string
GroupID string
UserID string
GroupID int64
UserID int64
Username string
GroupName string
OperatorID string
MessageID string
UserTitle string
Expand Down
6 changes: 4 additions & 2 deletions pkg/libonebotv11/intf_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ func ReplySegment(messageID string, userID string) Segment {
// https://12.onebot.dev/interface/message/actions/

const (
ActionSendMessage = "send_msg" // 发送消息
ActionDeleteMessage = "delete_msg" // 删除消息
ActionSendMessage = "send_msg" // 发送消息
ActionDeleteMessage = "delete_msg" // 删除消息
ActionSendGroupMessage = "send_group_msg" // 发送群消息
ActionSendPrivateMessage = "send_private_msg" // 发送私聊消息
)
26 changes: 14 additions & 12 deletions pkg/minecraft/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ var defaultName = "MCOnebot"
// Manager 客户端管理器
type Manager struct {
Config common.Config
Connections map[string]*Connection // 客户端连接列表 name/group_id -> client
EventChan chan common.Event // 与bot通信的事件通道
ConnectionChan chan common.Event // 与客户端通信的事件通道
Connections []*Connection // 客户端连接列表 name/group_id -> client
EventChan chan common.Event // Minecraft -> Bot
SendChan chan common.Event // Bot -> Minecraft
ConnectionChan chan common.Event // 与客户端通信的事件通道
AuthCache map[string]*bot.Auth
}

type Connection struct {
// 主体信息
Name string
ID int64
ServerConfig common.ServerConfig
BotAuth *bot.Auth
Client *bot.Client
Expand All @@ -41,15 +43,16 @@ type Connection struct {
player *basic.Player
playerList *playerlist.PlayerList
// 编译后的正则表达式
messageRegexps []*regexp.Regexp
IntID int64
messageRegexps []*regexp.Regexp
illegalChar []string
lastSentMessage string
}

// NewClientManager 创建一个新的客户端管理器
func NewClientManager(config common.Config) *Manager {
return &Manager{
Config: config,
Connections: make(map[string]*Connection),
Connections: make([]*Connection, 0),
AuthCache: make(map[string]*bot.Auth),
EventChan: make(chan common.Event),
ConnectionChan: make(chan common.Event, 1),
Expand Down Expand Up @@ -87,11 +90,9 @@ func (m *Manager) InitBotAuth() {
func (m *Manager) Run() {
// 等待账户验证完成后才可以启动游戏
m.InitBotAuth()

//go m.HandleEvents()
go m.HandleEventsMux()
// 初始化连接内容
for serverName, serverConfig := range m.Config.Servers {
for _, serverConfig := range m.Config.Servers {
// 当缓存中没有验证信息时,使用默认离线账户
botAuth, ok := m.AuthCache[serverConfig.Auth]
if !ok {
Expand All @@ -101,17 +102,19 @@ func (m *Manager) Run() {
}
}
connection := &Connection{
Name: serverName,
Name: serverConfig.Name,
ServerConfig: serverConfig,
BotAuth: botAuth,
ID: serverConfig.ID,

closeChan: make(chan int),
}
m.Connections[serverName] = connection
m.Connections = append(m.Connections, connection)
}
// 启动连接
for _, connection := range m.Connections {
connection.InitConnection()
connection.illegalChar = m.Config.Common.IllegalChar
go connection.HandleConnectEvents(m.EventChan)
go connection.Run()
go connection.RunRCON()
Expand All @@ -123,7 +126,6 @@ func (m *Manager) Run() {
// Run 运行客户端,客户端一旦开始运行,自动重连由客户端自行管理
func (c *Connection) Run() {
// 循环检测IsAlive状态
c.IntID = GenerateUserID(c.Name)
go c.Join()
var reason string
for {
Expand Down
Loading

0 comments on commit e2d714a

Please sign in to comment.