add help and transfer

This commit is contained in:
shoopea 2021-11-28 18:31:49 +08:00
parent 9614fccb68
commit cd98db8489
2 changed files with 70 additions and 4 deletions

66
bot.go
View File

@ -73,6 +73,8 @@ func (b *Bot) BotHandlers() {
b.bot.Handle("/take", botTake) b.bot.Handle("/take", botTake)
b.bot.Handle("/passwd", botPasswd) b.bot.Handle("/passwd", botPasswd)
b.bot.Handle("/say", botSay) b.bot.Handle("/say", botSay)
b.bot.Handle("/help", botHelp)
b.bot.Handle("/transfer", botTransfer)
b.bot.Handle(tb.OnPhoto, botPhoto) b.bot.Handle(tb.OnPhoto, botPhoto)
b.bot.Handle(tb.OnChannelPost, botChannelPost) b.bot.Handle(tb.OnChannelPost, botChannelPost)
@ -268,6 +270,48 @@ func botGive(m *tb.Message) {
bot.SendChat(m.Chat.ID, fmt.Sprintf("@%s now has %s left.", uStr, cc.TimeLeft)) bot.SendChat(m.Chat.ID, fmt.Sprintf("@%s now has %s left.", uStr, cc.TimeLeft))
} }
func botTransfer(m *tb.Message) {
r := regexp.MustCompile("^/transfer @(?P<Username>[a-zA-Z0-9]+) (?P<Duration>[a-z0-9]+)")
ccFrom, ok := cfg.Clients[m.Sender.ID]
if !ok {
bot.SendChat(m.Chat.ID, "Client not registered.")
return
}
dStr := r.ReplaceAllString(m.Text, "${Duration}")
d, err := time.ParseDuration(dStr)
logErrorDebug(err, "botTransfer : time.ParseDuration")
if err != nil {
bot.SendChat(m.Chat.ID, "Cannot parse duration.")
return
}
if ccFrom.TimeLeft < d {
bot.SendChat(m.Chat.ID, "Not enough time left for user.")
return
}
uStr := r.ReplaceAllString(m.Text, "${Username}")
var toID int
for ccID, cc := range cfg.Clients {
if cc.Username == uStr {
toID = ccID
}
}
if toID == 0 {
bot.SendChat(m.Chat.ID, "No such user found.")
return
}
ccTo := cfg.Clients[toID]
ccTo.TimeLeft += d
ccFrom.TimeLeft -= d
bot.SendChat(m.Chat.ID, fmt.Sprintf("@%s now has %s left.\r\n@%s has %s left.", uStr, ccTo.TimeLeft, ccFrom.Username, ccFrom.TimeLeft))
}
func botTake(m *tb.Message) { func botTake(m *tb.Message) {
if m.Sender.ID != int(cfg.Telegram.AdminID) { if m.Sender.ID != int(cfg.Telegram.AdminID) {
bot.SendChat(m.Chat.ID, "Only the admin can use this command.") bot.SendChat(m.Chat.ID, "Only the admin can use this command.")
@ -298,6 +342,7 @@ func botTake(m *tb.Message) {
logErrorDebug(err, "botGive : time.ParseDuration") logErrorDebug(err, "botGive : time.ParseDuration")
if err != nil { if err != nil {
bot.SendChat(m.Chat.ID, "Cannot parse duration.") bot.SendChat(m.Chat.ID, "Cannot parse duration.")
return
} }
cc := cfg.Clients[u] cc := cfg.Clients[u]
@ -306,6 +351,27 @@ func botTake(m *tb.Message) {
bot.SendChat(m.Chat.ID, fmt.Sprintf("@%s now has %s left.", uStr, cc.TimeLeft)) bot.SendChat(m.Chat.ID, fmt.Sprintf("@%s now has %s left.", uStr, cc.TimeLeft))
} }
func botHelp(m *tb.Message) {
msg := ""
msg = fmt.Sprintf("%s/pause - pause the game\r\n", msg)
msg = fmt.Sprintf("%s/unpause - unpause the game\r\n", msg)
msg = fmt.Sprintf("%s/register - register company\r\n", msg)
msg = fmt.Sprintf("%s/deregister - deregister company\r\n", msg)
msg = fmt.Sprintf("%s/delete - delete company\r\n", msg)
msg = fmt.Sprintf("%s/companies - list companies\r\n", msg)
msg = fmt.Sprintf("%s/clients - list clients\r\n", msg)
msg = fmt.Sprintf("%s/players - list players\r\n", msg)
msg = fmt.Sprintf("%s/give - give time to player\r\n", msg)
msg = fmt.Sprintf("%s/take - take time from player\r\n", msg)
msg = fmt.Sprintf("%s/transfer - transfer time to player\r\n", msg)
msg = fmt.Sprintf("%s/passwd - change passwd\r\n", msg)
msg = fmt.Sprintf("%s/say - send msg to the game\r\n", msg)
msg = fmt.Sprintf("%s/help - this\r\n", msg)
bot.SendChat(m.Chat.ID, msg)
return
}
func botSay(m *tb.Message) { func botSay(m *tb.Message) {
r := regexp.MustCompile("^\\/say( )+(?P<Message>.+)$") r := regexp.MustCompile("^\\/say( )+(?P<Message>.+)$")
if !r.MatchString(m.Text) { if !r.MatchString(m.Text) {

View File

@ -1,6 +1,6 @@
// Code generated by version.sh (@generated) DO NOT EDIT. // Code generated by version.sh (@generated) DO NOT EDIT.
package main package main
var githash = "d09710e" var githash = "9614fcc"
var buildstamp = "2021-11-26_00:35:33" var buildstamp = "2021-11-28_10:31:27"
var commits = "207" var commits = "208"
var version = "d09710e-b207 - 2021-11-26_00:35:33" var version = "9614fcc-b208 - 2021-11-28_10:31:27"