Archived
1

Work on squads, some refactoring

Closes #14
See #8
This commit is contained in:
2017-11-19 22:16:11 +04:00
parent 95a9a2146a
commit e4102e9a90
26 changed files with 587 additions and 112 deletions

43
lib/chatter/updaters.go Normal file
View File

@@ -0,0 +1,43 @@
// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
package chatter
import (
"github.com/go-telegram-bot-api/telegram-bot-api"
"lab.pztrn.name/fat0troll/i2_bot/lib/dbmapping"
)
// UpdateChatTitle updates chat title in database
func (ct *Chatter) UpdateChatTitle(chatRaw *dbmapping.Chat, newTitle string) (*dbmapping.Chat, bool) {
chatRaw.Name = newTitle
_, err := c.Db.NamedExec("UPDATE chats SET name=:name WHERE id=:id", &chatRaw)
if err != nil {
c.Log.Error(err)
return chatRaw, false
}
return chatRaw, true
}
// UpdateChatTelegramID updates chat's TelegramID when it converts to supergroup
func (ct *Chatter) UpdateChatTelegramID(update *tgbotapi.Update) (*dbmapping.Chat, bool) {
c.Log.Debug("Updating existing Telegram chat ID...")
chatRaw := dbmapping.Chat{}
err := c.Db.Get(&chatRaw, c.Db.Rebind("SELECT * FROM chats WHERE telegram_id=?"), update.Message.MigrateFromChatID)
if err != nil {
c.Log.Error(err.Error())
return &chatRaw, false
}
if update.Message.SuperGroupChatCreated {
chatRaw.ChatType = "supergroup"
}
chatRaw.TelegramID = update.Message.MigrateToChatID
_, err = c.Db.NamedExec("UPDATE chats SET chat_type=:chat_type, telegram_id=:telegram_id WHERE id=:id", &chatRaw)
if err != nil {
c.Log.Error(err.Error())
return &chatRaw, false
}
return &chatRaw, true
}