fix leaving message for 255 company

This commit is contained in:
shoopea 2021-11-28 21:41:00 +08:00
parent 3b878e0641
commit 044eac9a77
3 changed files with 65 additions and 10 deletions

64
bot.go
View File

@ -75,6 +75,7 @@ func (b *Bot) BotHandlers() {
b.bot.Handle("/say", botSay) b.bot.Handle("/say", botSay)
b.bot.Handle("/help", botHelp) b.bot.Handle("/help", botHelp)
b.bot.Handle("/transfer", botTransfer) b.bot.Handle("/transfer", botTransfer)
b.bot.Handle("/version", botVersion)
b.bot.Handle(tb.OnPhoto, botPhoto) b.bot.Handle(tb.OnPhoto, botPhoto)
b.bot.Handle(tb.OnChannelPost, botChannelPost) b.bot.Handle(tb.OnChannelPost, botChannelPost)
@ -133,7 +134,7 @@ func botUnpause(m *tb.Message) {
func botDelete(m *tb.Message) { func botDelete(m *tb.Message) {
logInfoDebug("[%d] %s(%d) | %s(%d) : delete : %s\n", m.ID, m.Chat.Title, m.Chat.ID, m.Sender.Username, m.Sender.ID, m.Text) logInfoDebug("[%d] %s(%d) | %s(%d) : delete : %s\n", m.ID, m.Chat.Title, m.Chat.ID, m.Sender.Username, m.Sender.ID, m.Text)
r := regexp.MustCompile("/delete (?P<CompanyID>[0-9]+)") r := regexp.MustCompile("\\/delete (?P<CompanyID>[0-9]+)")
id := uint8(255) id := uint8(255)
if r.MatchString(m.Text) { if r.MatchString(m.Text) {
ID64, _ := strconv.ParseInt(r.ReplaceAllString(m.Text, "${CompanyID}"), 10, 64) ID64, _ := strconv.ParseInt(r.ReplaceAllString(m.Text, "${CompanyID}"), 10, 64)
@ -238,7 +239,7 @@ func botGive(m *tb.Message) {
return return
} }
r := regexp.MustCompile("^/give @(?P<Username>[a-zA-Z0-9]+) (?P<Duration>[a-z0-9]+)") r := regexp.MustCompile("^\\/give @(?P<Username>[a-zA-Z0-9]+) (?P<Duration>[a-z0-9]+)")
if !r.MatchString(m.Text) { if !r.MatchString(m.Text) {
bot.SendChat(m.Chat.ID, "Wrong format.") bot.SendChat(m.Chat.ID, "Wrong format.")
@ -270,8 +271,18 @@ func botGive(m *tb.Message) {
bot.SendChat(m.Chat.ID, fmt.Sprintf("@%s now has %s left.", uStr, cc.TimeLeft.Round(time.Second))) bot.SendChat(m.Chat.ID, fmt.Sprintf("@%s now has %s left.", uStr, cc.TimeLeft.Round(time.Second)))
} }
func botVersion(m *tb.Message) {
bot.SendChat(m.Chat.ID, version)
return
}
func botTransfer(m *tb.Message) { func botTransfer(m *tb.Message) {
r := regexp.MustCompile("^/transfer @(?P<Username>[a-zA-Z0-9]+) (?P<Duration>[a-z0-9]+)") r := regexp.MustCompile("^\\/transfer @(?P<Username>[a-zA-Z0-9]+) (?P<Duration>[a-z0-9]+)")
if !r.MatchString(m.Text) {
bot.SendChat(m.Chat.ID, "Wrong usage.")
return
}
ccFrom, ok := cfg.Clients[m.Sender.ID] ccFrom, ok := cfg.Clients[m.Sender.ID]
if !ok { if !ok {
@ -318,7 +329,7 @@ func botTake(m *tb.Message) {
return return
} }
r := regexp.MustCompile("^/take @(?P<Username>[a-zA-Z0-9]+) (?P<Duration>[a-z0-9]+)") r := regexp.MustCompile("^\\/take @(?P<Username>[a-zA-Z0-9]+) (?P<Duration>[a-z0-9]+)")
if !r.MatchString(m.Text) { if !r.MatchString(m.Text) {
bot.SendChat(m.Chat.ID, "Wrong format.") bot.SendChat(m.Chat.ID, "Wrong format.")
@ -363,9 +374,10 @@ func botHelp(m *tb.Message) {
msg = fmt.Sprintf("%s/players - list players\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/give - give time to player\r\n", msg)
msg = fmt.Sprintf("%s/take - take time from 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/transfer - transfer time between players\r\n", msg)
msg = fmt.Sprintf("%s/passwd - change passwd\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/say - send msg to the game\r\n", msg)
msg = fmt.Sprintf("%s/version - version\r\n", msg)
msg = fmt.Sprintf("%s/help - this\r\n", msg) msg = fmt.Sprintf("%s/help - this\r\n", msg)
bot.SendChat(m.Chat.ID, msg) bot.SendChat(m.Chat.ID, msg)
@ -426,6 +438,46 @@ func botPasswd(m *tb.Message) {
} }
func botDeregister(m *tb.Message) { func botDeregister(m *tb.Message) {
r := regexp.MustCompile("^\\/deregister @(?P<Username>[a-zA-Z0-9]+)$")
if r.MatchString(m.Text) {
if m.Sender.ID != int(cfg.Telegram.AdminID) {
bot.SendChat(m.Chat.ID, "Not admin.")
return
}
uStr := r.ReplaceAllString(m.Text, "${Username}")
var uID int
for ccID, cc := range cfg.Clients {
if cc.Username == uStr {
uID = ccID
}
}
if uID == 0 {
bot.SendChat(m.Chat.ID, "No such user found.")
return
}
cc := cfg.Clients[uID]
if cc.CompanyID != 255 {
for coID, co := range srv.Status.Companies {
if coID == cc.CompanyID {
cc.CompanyID = 255
bot.SendChat(m.Chat.ID, fmt.Sprintf("Deregistered from %s. %s playable left.", co.Name, cc.TimeLeft.Round(time.Second)))
return
}
}
logInfoAlert("botRegister : %s : no such CompanyID : %d", cc.Username, cc.CompanyID)
cc.CompanyID = 255
bot.SendChat(m.Chat.ID, fmt.Sprintf("Registered company didn't exist anymore. %s playable left.", cc.TimeLeft.Round(time.Second)))
return
}
return
}
cc, ok := cfg.Clients[m.Sender.ID] cc, ok := cfg.Clients[m.Sender.ID]
if !ok { if !ok {
cc = &ClientConfig{ cc = &ClientConfig{
@ -566,7 +618,7 @@ func botQuery(q *tb.Query) {
} }
func botText(m *tb.Message) { func botText(m *tb.Message) {
r := regexp.MustCompile("^/delete_(?P<Ref>[a-f0-9]{16})$") r := regexp.MustCompile("^\\/delete_(?P<Ref>[a-f0-9]{16})$")
if r.MatchString(m.Text) { if r.MatchString(m.Text) {
ref := r.ReplaceAllString(m.Text, "${Ref}") ref := r.ReplaceAllString(m.Text, "${Ref}")
d, ok := companyDeleteMap[ref] d, ok := companyDeleteMap[ref]

View File

@ -101,6 +101,9 @@ func (c *Config) Export() ([]byte, error) {
} }
func (c *Config) CompanyIsRegistered(id uint8) bool { func (c *Config) CompanyIsRegistered(id uint8) bool {
if id == 255 {
return false
}
for _, cc := range c.Clients { for _, cc := range c.Clients {
if cc.CompanyID == id { if cc.CompanyID == id {
return true return true

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 = "cd98db8" var githash = "3b878e0"
var buildstamp = "2021-11-28_13:25:25" var buildstamp = "2021-11-28_13:40:44"
var commits = "209" var commits = "210"
var version = "cd98db8-b209 - 2021-11-28_13:25:25" var version = "3b878e0-b210 - 2021-11-28_13:40:44"