From 40a0b5e800b1041d2e46ba6469f16bac9b591ed5 Mon Sep 17 00:00:00 2001 From: shoopea Date: Sat, 13 Nov 2021 13:44:57 +0800 Subject: [PATCH] test /say --- bot.go | 22 ++++++++++++++++++++++ packet.go | 41 +++++++++++++++++++++++++++++++++++++++++ version.go | 8 ++++---- 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/bot.go b/bot.go index 0c50fe8..a7bb328 100644 --- a/bot.go +++ b/bot.go @@ -72,6 +72,7 @@ func (b *Bot) BotHandlers() { b.bot.Handle("/give", botGive) b.bot.Handle("/take", botTake) b.bot.Handle("/passwd", botPasswd) + b.bot.Handle("/say", botSay) b.bot.Handle(tb.OnPhoto, botPhoto) b.bot.Handle(tb.OnChannelPost, botChannelPost) @@ -299,6 +300,27 @@ func botTake(m *tb.Message) { bot.SendChat(m.Chat.ID, fmt.Sprintf("@%s now has %s left.", uStr, cc.TimeLeft)) } +func botSay(m *tb.Message) { + r := regexp.MustCompile("^\\/say( )+(?P.+)$") + if !r.MatchString(m.Text) { + bot.SendChat(m.Chat.ID, "No message provided") + return + } + + msg := r.ReplaceAllString(m.Text, "${Message}") + + px := PacketAdminChat{ + Packet: Packet{PType: AdminPacketAdminChat}, + ActionType: AdminNetworkActionChat, + DestinationType: AdminDestinationTypeBroadcast, + DestinationID: 0, + Message: msg, + } + srv.Send(px.Bytes()) + + return +} + func botPasswd(m *tb.Message) { cc, ok := cfg.Clients[m.Sender.ID] if !ok { diff --git a/packet.go b/packet.go index 48f1ede..e8db4eb 100644 --- a/packet.go +++ b/packet.go @@ -63,6 +63,24 @@ const ( AdminFrequencyQuarterly = 0x10 AdminFrequencyAnnually = 0x20 AdminFrequencyAutomatic = 0x40 + + AdminNetworkActionJoin = 0 + AdminNetworkActionLeave = 1 + AdminNetworkActionServerMessage = 2 + AdminNetworkActionChat = 3 + AdminNetworkActionChatCompany = 4 + AdminNetworkActionChatClient = 5 + AdminNetworkActionGiveMoney = 6 + AdminNetworkActionNameChange = 7 + AdminNetworkActionCompanySpectator = 8 + AdminNetworkActionCompanyJoin = 9 + AdminNetworkActionCompanyNew = 10 + AdminNetworkActionKicked = 11 + AdminNetworkActionExternalChat = 12 + + AdminDestinationTypeBroadcast = 0 + AdminDestinationTypeTeam = 1 + AdminDestinationTypeClient = 2 ) type Packet struct { @@ -82,6 +100,14 @@ type PacketAdminRCon struct { Command string } +type PacketAdminChat struct { + Packet + ActionType uint8 + DestinationType uint8 + DestinationID uint32 + Message string +} + type PacketServerProtocol struct { Packet ProtocolVersion uint8 @@ -239,6 +265,21 @@ func (p *PacketAdminRCon) Bytes() []byte { return buf.Bytes() } +func (p *PacketAdminChat) Bytes() []byte { + buf := new(bytes.Buffer) + p.PLength = uint16(len(p.Message) + 10) + + binary.Write(buf, binary.LittleEndian, p.PLength) + binary.Write(buf, binary.LittleEndian, p.ActionType) + binary.Write(buf, binary.LittleEndian, p.DestinationType) + binary.Write(buf, binary.LittleEndian, p.DestinationID) + buf.WriteString(p.Message) + buf.WriteByte(0) + + return buf.Bytes() + +} + func (p *PacketAdminUpdateFrequency) Bytes() []byte { buf := new(bytes.Buffer) p.PLength = 7 diff --git a/version.go b/version.go index 5d6f899..9e9d5a4 100644 --- a/version.go +++ b/version.go @@ -1,6 +1,6 @@ // Code generated by version.sh (@generated) DO NOT EDIT. package main -var githash = "aa2a229" -var buildstamp = "2021-11-13_02:45:07" -var commits = "190" -var version = "aa2a229-b190 - 2021-11-13_02:45:07" +var githash = "bb1a7d0" +var buildstamp = "2021-11-13_05:44:46" +var commits = "193" +var version = "bb1a7d0-b193 - 2021-11-13_05:44:46"