From cd98db84895878ea4c55c8a3a2b633abb685c132 Mon Sep 17 00:00:00 2001 From: shoopea Date: Sun, 28 Nov 2021 18:31:49 +0800 Subject: [PATCH] add help and transfer --- bot.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ version.go | 8 +++---- 2 files changed, 70 insertions(+), 4 deletions(-) diff --git a/bot.go b/bot.go index 0ff6852..1ec3a4e 100644 --- a/bot.go +++ b/bot.go @@ -73,6 +73,8 @@ func (b *Bot) BotHandlers() { b.bot.Handle("/take", botTake) b.bot.Handle("/passwd", botPasswd) 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.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)) } +func botTransfer(m *tb.Message) { + r := regexp.MustCompile("^/transfer @(?P[a-zA-Z0-9]+) (?P[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) { if m.Sender.ID != int(cfg.Telegram.AdminID) { 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") if err != nil { bot.SendChat(m.Chat.ID, "Cannot parse duration.") + return } 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)) } +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) { r := regexp.MustCompile("^\\/say( )+(?P.+)$") if !r.MatchString(m.Text) { diff --git a/version.go b/version.go index f0de50a..fb6a726 100644 --- a/version.go +++ b/version.go @@ -1,6 +1,6 @@ // Code generated by version.sh (@generated) DO NOT EDIT. package main -var githash = "d09710e" -var buildstamp = "2021-11-26_00:35:33" -var commits = "207" -var version = "d09710e-b207 - 2021-11-26_00:35:33" +var githash = "9614fcc" +var buildstamp = "2021-11-28_10:31:27" +var commits = "208" +var version = "9614fcc-b208 - 2021-11-28_10:31:27"