Pokememes database saving. Now we're stable
This commit is contained in:
@@ -12,7 +12,7 @@ import (
|
||||
"gopkg.in/telegram-bot-api.v4"
|
||||
)
|
||||
|
||||
func DurakMessage(bot *tgbotapi.BotAPI, update tgbotapi.Update) {
|
||||
func (t *Talkers) DurakMessage(update tgbotapi.Update) {
|
||||
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
|
||||
|
||||
reactions := make([]string, 0)
|
||||
@@ -27,10 +27,10 @@ func DurakMessage(bot *tgbotapi.BotAPI, update tgbotapi.Update) {
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, reactions[rand.Intn(len(reactions))])
|
||||
msg.ReplyToMessageID = update.Message.MessageID
|
||||
|
||||
bot.Send(msg)
|
||||
c.Bot.Send(msg)
|
||||
}
|
||||
|
||||
func MatMessage(bot *tgbotapi.BotAPI, update tgbotapi.Update) {
|
||||
func (t *Talkers) MatMessage(update tgbotapi.Update) {
|
||||
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
|
||||
|
||||
reactions := make([]string, 0)
|
||||
@@ -44,5 +44,5 @@ func MatMessage(bot *tgbotapi.BotAPI, update tgbotapi.Update) {
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, reactions[rand.Intn(len(reactions))])
|
||||
msg.ReplyToMessageID = update.Message.MessageID
|
||||
|
||||
bot.Send(msg)
|
||||
c.Bot.Send(msg)
|
||||
}
|
||||
|
@@ -8,12 +8,12 @@ import (
|
||||
"gopkg.in/telegram-bot-api.v4"
|
||||
)
|
||||
|
||||
func AnyMessageUnauthorized(bot *tgbotapi.BotAPI, update tgbotapi.Update) {
|
||||
func (t *Talkers) AnyMessageUnauthorized(update tgbotapi.Update) {
|
||||
error_message := "Извини, действие для тебя недоступно. Возможно, у меня нет твоего профиля или же твои права недостаточны для совершения данного действия\n\n"
|
||||
error_message += "Если тебе кажется, что это ошибка, пиши @fat0troll.\n"
|
||||
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, error_message)
|
||||
msg.ParseMode = "Markdown"
|
||||
|
||||
bot.Send(msg)
|
||||
c.Bot.Send(msg)
|
||||
}
|
||||
|
28
lib/talkers/exported.go
Normal file
28
lib/talkers/exported.go
Normal file
@@ -0,0 +1,28 @@
|
||||
// i2_bot – Instinct PokememBro Bot
|
||||
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package talkers
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"log"
|
||||
// local
|
||||
"../appcontext"
|
||||
"../talkers/talkersinterface"
|
||||
)
|
||||
|
||||
var (
|
||||
c *appcontext.Context
|
||||
)
|
||||
|
||||
type Talkers struct {}
|
||||
|
||||
func New(ac *appcontext.Context) {
|
||||
c = ac
|
||||
m := &Talkers{}
|
||||
c.RegisterTalkersInterface(talkersinterface.TalkersInterface(m))
|
||||
}
|
||||
|
||||
func (t *Talkers) Init() {
|
||||
log.Printf("Initializing responders...")
|
||||
}
|
@@ -10,7 +10,7 @@ import (
|
||||
"../dbmappings"
|
||||
)
|
||||
|
||||
func HelloMessageUnauthorized(bot *tgbotapi.BotAPI, update tgbotapi.Update) {
|
||||
func (t *Talkers) HelloMessageUnauthorized(update tgbotapi.Update) {
|
||||
hello_message := "*Бот Инстинкта приветствует тебя!*\n\n"
|
||||
hello_message += "Для начала работы с ботом, пожалуйста, перешли от бота игры @PokememBroBot профиль героя.\n"
|
||||
hello_message += "Все дальнейшие действия с ботом возможны лишь при наличии профиля игрока."
|
||||
@@ -18,15 +18,15 @@ func HelloMessageUnauthorized(bot *tgbotapi.BotAPI, update tgbotapi.Update) {
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, hello_message)
|
||||
msg.ParseMode = "Markdown"
|
||||
|
||||
bot.Send(msg)
|
||||
c.Bot.Send(msg)
|
||||
}
|
||||
|
||||
func HelloMessageAuthorized(bot *tgbotapi.BotAPI, update tgbotapi.Update, player_raw dbmappings.Players) {
|
||||
func (t *Talkers) HelloMessageAuthorized(update tgbotapi.Update, player_raw dbmappings.Players) {
|
||||
hello_message := "*Бот Инстинкта приветствует тебя. Снова.*\n\n"
|
||||
hello_message += "Привет, " + update.Message.From.FirstName + " " + update.Message.From.LastName + "!\n"
|
||||
hello_message += "Последнее обновление информации о тебе: " + player_raw.Updated_at.Format("02.01.2006 15:04:05 -0700")
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, hello_message)
|
||||
msg.ParseMode = "Markdown"
|
||||
|
||||
bot.Send(msg)
|
||||
c.Bot.Send(msg)
|
||||
}
|
||||
|
@@ -10,7 +10,7 @@ import (
|
||||
"../config"
|
||||
)
|
||||
|
||||
func HelpMessage(bot *tgbotapi.BotAPI, update tgbotapi.Update) {
|
||||
func (t *Talkers) HelpMessage(update tgbotapi.Update) {
|
||||
help_message := "*Бот Инстинкта Enchanched.*\n\n"
|
||||
help_message += "Текущая версия: *" + config.VERSION + "*\n\n"
|
||||
help_message += "Список команд:\n\n"
|
||||
@@ -21,5 +21,5 @@ func HelpMessage(bot *tgbotapi.BotAPI, update tgbotapi.Update) {
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, help_message)
|
||||
msg.ParseMode = "Markdown"
|
||||
|
||||
bot.Send(msg)
|
||||
c.Bot.Send(msg)
|
||||
}
|
||||
|
118
lib/talkers/pokedex.go
Normal file
118
lib/talkers/pokedex.go
Normal file
@@ -0,0 +1,118 @@
|
||||
// i2_bot – Instinct PokememBro Bot
|
||||
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package talkers
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"log"
|
||||
"strconv"
|
||||
// 3rd party
|
||||
"gopkg.in/telegram-bot-api.v4"
|
||||
// local
|
||||
"../dbmappings"
|
||||
)
|
||||
|
||||
type PokememeFull struct {
|
||||
Pokememe dbmappings.Pokememes
|
||||
Elements []dbmappings.Elements
|
||||
Locations []dbmappings.Locations
|
||||
}
|
||||
|
||||
func (t *Talkers) PokememesList(update tgbotapi.Update, page int) {
|
||||
pokememes := []dbmappings.Pokememes{}
|
||||
err := c.Db.Select(&pokememes, "SELECT * FROM pokememes");
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
pokememes_limited := []dbmappings.Pokememes{}
|
||||
err = c.Db.Select(&pokememes_limited, "SELECT * FROM pokememes ORDER BY grade asc, name asc LIMIT 50 OFFSET " + strconv.Itoa(50*(page-1)));
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
elements := []dbmappings.Elements{}
|
||||
err = c.Db.Select(&elements, "SELECT * FROM elements");
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
locations := []dbmappings.Locations{}
|
||||
err = c.Db.Select(&locations, "SELECT * FROM locations");
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
pokememes_elements := []dbmappings.PokememesElements{}
|
||||
err = c.Db.Select(&pokememes_elements, "SELECT * FROM pokememes_elements");
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
pokememes_locations := []dbmappings.PokememesLocations{}
|
||||
err = c.Db.Select(&pokememes_locations, "SELECT * FROM pokememes_locations");
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
pokememes_full := []PokememeFull{}
|
||||
|
||||
for i := range(pokememes_limited) {
|
||||
full_pokememe := PokememeFull{}
|
||||
elements_listed := []dbmappings.Elements{}
|
||||
locations_listed := []dbmappings.Locations{}
|
||||
|
||||
for j := range(pokememes_locations) {
|
||||
if pokememes_locations[j].Pokememe_id == pokememes_limited[i].Id {
|
||||
for l := range(locations) {
|
||||
if pokememes_locations[j].Location_id == locations[l].Id {
|
||||
locations_listed = append(locations_listed, locations[l])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for k := range(pokememes_elements) {
|
||||
if pokememes_elements[k].Pokememe_id == pokememes_limited[i].Id {
|
||||
for e := range(elements) {
|
||||
if pokememes_elements[k].Element_id == elements[e].Id {
|
||||
elements_listed = append(elements_listed, elements[e])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
full_pokememe.Pokememe = pokememes_limited[i]
|
||||
full_pokememe.Elements = elements_listed
|
||||
full_pokememe.Locations = locations_listed
|
||||
|
||||
pokememes_full = append(pokememes_full, full_pokememe)
|
||||
}
|
||||
|
||||
message := "*Известные боту покемемы*\n"
|
||||
message += "Список отсортирован по грейду и алфавиту.\n"
|
||||
message += "Покедекс: " + strconv.Itoa(len(pokememes)) + " / 206\n"
|
||||
message += "Отображаем покемемов с " + strconv.Itoa(((page - 1)*50)+1) + " по " + strconv.Itoa(page*50) + "\n"
|
||||
if len(pokememes) > page*50 {
|
||||
message += "Переход на следующую страницу: /pokedeks" + strconv.Itoa(page + 1)
|
||||
}
|
||||
if page > 1 {
|
||||
message += "\nПереход на предыдущую страницу: /pokedeks" + strconv.Itoa(page - 1)
|
||||
}
|
||||
message += "\n\n"
|
||||
|
||||
for i := range(pokememes_full) {
|
||||
pk := pokememes_full[i].Pokememe
|
||||
pk_e := pokememes_full[i].Elements
|
||||
message += strconv.Itoa(i + 1 + (50*(page-1))) + ". " + strconv.Itoa(pk.Grade)
|
||||
message += "⃣ *" + pk.Name
|
||||
message += "* (" + c.Parsers.ReturnPoints(pk.HP) + "-" + c.Parsers.ReturnPoints(pk.MP) + ") ⚔️ *"
|
||||
message += c.Parsers.ReturnPoints(pk.Attack) + "* \\["
|
||||
for j := range(pk_e) {
|
||||
message += pk_e[j].Symbol
|
||||
}
|
||||
message += "] " + c.Parsers.ReturnPoints(pk.Price) + "$ /pk" + strconv.Itoa(pk.Id)
|
||||
message += "\n"
|
||||
}
|
||||
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message)
|
||||
msg.ParseMode = "Markdown"
|
||||
|
||||
c.Bot.Send(msg)
|
||||
}
|
41
lib/talkers/pokememe_add.go
Normal file
41
lib/talkers/pokememe_add.go
Normal file
@@ -0,0 +1,41 @@
|
||||
// i2_bot – Instinct PokememBro Bot
|
||||
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package talkers
|
||||
|
||||
import (
|
||||
// 3rd party
|
||||
"gopkg.in/telegram-bot-api.v4"
|
||||
)
|
||||
|
||||
func (t *Talkers) PokememeAddSuccessMessage(update tgbotapi.Update) {
|
||||
message := "*Покемем успешно добавлен.*\n\n"
|
||||
message += "Посмотреть всех известных боту покемемов можно командой /pokedeks"
|
||||
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message)
|
||||
msg.ParseMode = "Markdown"
|
||||
|
||||
c.Bot.Send(msg)
|
||||
}
|
||||
|
||||
func (t *Talkers) PokememeAddDuplicateMessage(update tgbotapi.Update) {
|
||||
message := "*Мы уже знаем об этом покемеме*\n\n"
|
||||
message += "Посмотреть всех известных боту покемемов можно командой /pokedeks\n\n"
|
||||
message += "Если у покемема изменились описание или характеристики, напиши @fat0troll для обновления базы."
|
||||
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message)
|
||||
msg.ParseMode = "Markdown"
|
||||
|
||||
c.Bot.Send(msg)
|
||||
}
|
||||
|
||||
func (t *Talkers) PokememeAddFailureMessage(update tgbotapi.Update) {
|
||||
message := "*Неудачно получилось :(*\n\n"
|
||||
message += "Случилась жуткая ошибка, и мы не смогли записать бота в базу. Напиши @fat0troll, он разбер]тся.\n\n"
|
||||
message += "Посмотреть всех известных боту покемемов можно командой /pokedeks"
|
||||
|
||||
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message)
|
||||
msg.ParseMode = "Markdown"
|
||||
|
||||
c.Bot.Send(msg)
|
||||
}
|
32
lib/talkers/talkersinterface/talkersinterface.go
Normal file
32
lib/talkers/talkersinterface/talkersinterface.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// i2_bot – Instinct PokememBro Bot
|
||||
// Copyright (c) 2017 Vladimir "fat0troll" Hodakov
|
||||
|
||||
package talkersinterface
|
||||
|
||||
import (
|
||||
// 3rd party
|
||||
"gopkg.in/telegram-bot-api.v4"
|
||||
// local
|
||||
"../../dbmappings"
|
||||
)
|
||||
|
||||
type TalkersInterface interface {
|
||||
Init()
|
||||
// Commands
|
||||
HelloMessageUnauthorized(update tgbotapi.Update)
|
||||
HelloMessageAuthorized(update tgbotapi.Update, player_raw dbmappings.Players)
|
||||
HelpMessage(update tgbotapi.Update)
|
||||
PokememesList(update tgbotapi.Update, page int)
|
||||
|
||||
// Returns
|
||||
PokememeAddSuccessMessage(update tgbotapi.Update)
|
||||
PokememeAddDuplicateMessage(update tgbotapi.Update)
|
||||
PokememeAddFailureMessage(update tgbotapi.Update)
|
||||
|
||||
// Errors
|
||||
AnyMessageUnauthorized(update tgbotapi.Update)
|
||||
|
||||
// Easter eggs
|
||||
DurakMessage(update tgbotapi.Update)
|
||||
MatMessage(update tgbotapi.Update)
|
||||
}
|
Reference in New Issue
Block a user