From fa71616a149005095de8663f121094d9fb99f517 Mon Sep 17 00:00:00 2001 From: Vladimir Hodakov Date: Mon, 4 Dec 2017 23:14:41 +0400 Subject: [PATCH] Pokememe deletion command --- lib/forwarder/forwarder.go | 6 +-- lib/pokedexer/cleaners.go | 52 +++++++++++++++++++ .../pokedexerinterface/pokedexerinterface.go | 2 + lib/router/private_request.go | 6 +++ 4 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 lib/pokedexer/cleaners.go diff --git a/lib/forwarder/forwarder.go b/lib/forwarder/forwarder.go index d5277aa..5a9dadb 100644 --- a/lib/forwarder/forwarder.go +++ b/lib/forwarder/forwarder.go @@ -19,11 +19,11 @@ func (f *Forwarder) ProcessForward(update *tgbotapi.Update, playerRaw *dbmapping switch { case pokememeMsg.MatchString(text): c.Log.Debug("Pokememe posted!") - if playerRaw.LeagueID == 1 { + if c.Users.PlayerBetterThan(playerRaw, "admin") { return c.Pokedexer.ParsePokememe(update, playerRaw) - } else { - return c.Talkers.AnyMessageUnauthorized(update) } + + return c.Talkers.AnyMessageUnauthorized(update) case profileMsg.MatchString(text): c.Log.Debug("Profile posted!") return c.Users.ParseProfile(update, playerRaw) diff --git a/lib/pokedexer/cleaners.go b/lib/pokedexer/cleaners.go new file mode 100644 index 0000000..e12cb40 --- /dev/null +++ b/lib/pokedexer/cleaners.go @@ -0,0 +1,52 @@ +// i2_bot – Instinct PokememBro Bot +// Copyright (c) 2017 Vladimir "fat0troll" Hodakov + +package pokedexer + +import ( + "github.com/go-telegram-bot-api/telegram-bot-api" + "strconv" +) + +// DeletePokememe deletes pokememe by its ID +func (p *Pokedexer) DeletePokememe(update *tgbotapi.Update) string { + pokememeNum, _ := strconv.Atoi(update.Message.CommandArguments()) + if pokememeNum == 0 { + return "fail" + } + + pokememe, ok := p.GetPokememeByID(strconv.Itoa(pokememeNum)) + if !ok { + return "fail" + } + + _, err := c.Db.NamedExec("DELETE FROM pokememes WHERE id=:id", &pokememe.Pokememe) + if err != nil { + c.Log.Error(err.Error()) + return "fail" + } + + _, err = c.Db.NamedExec("DELETE FROM pokememes_elements WHERE pokememe_id=:id", &pokememe.Pokememe) + if err != nil { + c.Log.Debug(err.Error()) + } + + _, err = c.Db.NamedExec("DELETE FROM pokememes_locations WHERE pokememe_id=:id", &pokememe.Pokememe) + if err != nil { + c.Log.Debug(err.Error()) + } + + _, err = c.Db.NamedExec("DELETE FROM profiles_pokememes WHERE pokememe_id=:id", &pokememe.Pokememe) + if err != nil { + c.Log.Debug(err.Error()) + } + + message := "Покемем удалён." + + msg := tgbotapi.NewMessage(update.Message.Chat.ID, message) + msg.ParseMode = "Markdown" + + c.Bot.Send(msg) + + return "ok" +} diff --git a/lib/pokedexer/pokedexerinterface/pokedexerinterface.go b/lib/pokedexer/pokedexerinterface/pokedexerinterface.go index e82ec4a..a162df3 100644 --- a/lib/pokedexer/pokedexerinterface/pokedexerinterface.go +++ b/lib/pokedexer/pokedexerinterface/pokedexerinterface.go @@ -18,4 +18,6 @@ type PokedexerInterface interface { GetPokememes() ([]dbmapping.PokememeFull, bool) GetPokememeByID(pokememeID string) (dbmapping.PokememeFull, bool) + + DeletePokememe(update *tgbotapi.Update) string } diff --git a/lib/router/private_request.go b/lib/router/private_request.go index 5bf740a..215b3fa 100644 --- a/lib/router/private_request.go +++ b/lib/router/private_request.go @@ -64,6 +64,12 @@ func (r *Router) routePrivateRequest(update *tgbotapi.Update, playerRaw *dbmappi case pokememeInfoMsg.MatchString(text): c.Pokedexer.PokememeInfo(update, playerRaw) return "ok" + case update.Message.Command() == "delete_pokememe": + if c.Users.PlayerBetterThan(playerRaw, "owner") { + return c.Pokedexer.DeletePokememe(update) + } + + return c.Talkers.AnyMessageUnauthorized(update) case update.Message.Command() == "me": if playerRaw.ID != 0 { c.Users.ProfileMessage(update, playerRaw)