From e0f0f2d282a7a2380d425dcad84664b2bee3d156 Mon Sep 17 00:00:00 2001 From: Watn3y Date: Sun, 5 Mar 2023 05:08:34 +0100 Subject: [PATCH] refine shutup feature --- bot.go | 48 +++------------------------- build.sh | 4 +++ commands.go | 1 - commands/commands.go | 17 ++++++++++ config/config.go | 2 +- text.go => text/balonlyl/balonlyl.go | 4 +-- text/matcher.go | 42 ++++++++++++++++++++++++ 7 files changed, 70 insertions(+), 48 deletions(-) create mode 100644 build.sh delete mode 100644 commands.go create mode 100644 commands/commands.go rename text.go => text/balonlyl/balonlyl.go (90%) create mode 100644 text/matcher.go diff --git a/bot.go b/bot.go index eac9813..e755f79 100644 --- a/bot.go +++ b/bot.go @@ -1,15 +1,10 @@ package main import ( - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" - "log" - "regexp" - "strings" "time" "watn3y/bloaterbotv3/botIO" - "watn3y/bloaterbotv3/commonlogic" - "watn3y/bloaterbotv3/config" - "watn3y/bloaterbotv3/text/nhentai" + "watn3y/bloaterbotv3/commands" + "watn3y/bloaterbotv3/text" ) var shutuptime time.Time @@ -23,48 +18,13 @@ func bot() { } if update.Message.IsCommand() { - go commands(update, bot) + commands.Commands(update, bot) } else { if time.Since(shutuptime).Minutes() >= 60 { - go textMatcher(update, bot) + text.Matcher(update, bot) } } } } - -func textMatcher(update tgbotapi.Update, bot *tgbotapi.BotAPI) { - - //nhentai - const nhentaiRegex string = "([ \\t\\n\\r]|^)\\d{2,}([ \\t\\n\\r]|$)" - isNhentai, err := regexp.MatchString(nhentaiRegex, update.Message.Text) - if err != nil { - log.Panicf("Failed to compile regex for nhentai", err) - } - if isNhentai && commonlogic.ContainsInt64(config.BotConfig.Nhentai.EnabledChats, update.Message.Chat.ID) { - go nhentai.Nhentai(nhentaiRegex, update, bot) - } - - //balonlyl - var isBalonlyl bool = false - for _, word := range config.BotConfig.Balonlyl.TriggerWords { - if strings.Contains(strings.ToLower(update.Message.Text), word) && commonlogic.ContainsInt64(config.BotConfig.Balonlyl.EnabledChats, update.Message.Chat.ID) { - isBalonlyl = true - break - } - } - if isBalonlyl { - go balonlyl(update, bot) - } - -} - -func commands(update tgbotapi.Update, bot *tgbotapi.BotAPI) { - if strings.ToLower(update.Message.Command()) == "shutup" || strings.ToLower(update.Message.Command()) == "shut" { - shutuptime = time.Now().UTC() - - msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Shutting up") - botIO.SendMessage(msg, bot) - } -} diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..32ffd0f --- /dev/null +++ b/build.sh @@ -0,0 +1,4 @@ +###!/usr/bin/env bash + +HASH=$(git log -1 --pretty=%h) +docker build -t bloaterbot:latest -t bloaterbot:$HASH . \ No newline at end of file diff --git a/commands.go b/commands.go deleted file mode 100644 index 06ab7d0..0000000 --- a/commands.go +++ /dev/null @@ -1 +0,0 @@ -package main diff --git a/commands/commands.go b/commands/commands.go new file mode 100644 index 0000000..1a45343 --- /dev/null +++ b/commands/commands.go @@ -0,0 +1,17 @@ +package commands + +import ( + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + "strings" + "time" + "watn3y/bloaterbotv3/botIO" + "watn3y/bloaterbotv3/config" +) + +func Commands(update tgbotapi.Update, bot *tgbotapi.BotAPI) { + if strings.ToLower(update.Message.Command()) == "shutup" || strings.ToLower(update.Message.Command()) == "shut" { + config.BotConfig.Shutup[update.Message.Chat.ID] = time.Now().UTC() + msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Shutting up") + botIO.SendMessage(msg, bot) + } +} diff --git a/config/config.go b/config/config.go index 4430076..af3b32e 100644 --- a/config/config.go +++ b/config/config.go @@ -12,7 +12,7 @@ type config struct { APIToken string DebugMode bool PrivilegedUsers []int64 - ShutupTime time.Time + Shutup map[int64]time.Time Nhentai struct { EnabledChats []int64 APIEndpoint string diff --git a/text.go b/text/balonlyl/balonlyl.go similarity index 90% rename from text.go rename to text/balonlyl/balonlyl.go index b846db2..6f2bc90 100644 --- a/text.go +++ b/text/balonlyl/balonlyl.go @@ -1,4 +1,4 @@ -package main +package balonlyl import ( tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" @@ -6,7 +6,7 @@ import ( "watn3y/bloaterbotv3/config" ) -func balonlyl(update tgbotapi.Update, bot *tgbotapi.BotAPI) { +func Balonlyl(update tgbotapi.Update, bot *tgbotapi.BotAPI) { /*balonlylLines := [4]string{ balonlylAT + " they're talking about you", diff --git a/text/matcher.go b/text/matcher.go new file mode 100644 index 0000000..36ca770 --- /dev/null +++ b/text/matcher.go @@ -0,0 +1,42 @@ +package text + +import ( + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + "log" + "regexp" + "strings" + "time" + "watn3y/bloaterbotv3/commonlogic" + "watn3y/bloaterbotv3/config" + "watn3y/bloaterbotv3/text/balonlyl" + "watn3y/bloaterbotv3/text/nhentai" +) + +func Matcher(update tgbotapi.Update, bot *tgbotapi.BotAPI) { + if time.Since(config.BotConfig.Shutup[update.Message.Chat.ID]) <= time.Minute { + return + } + + //nhentai + const nhentaiRegex string = "([ \\t\\n\\r]|^)\\d{2,}([ \\t\\n\\r]|$)" + isNhentai, err := regexp.MatchString(nhentaiRegex, update.Message.Text) + if err != nil { + log.Panicf("Failed to compile regex for nhentai", err) + } + if isNhentai && commonlogic.ContainsInt64(config.BotConfig.Nhentai.EnabledChats, update.Message.Chat.ID) { + go nhentai.Nhentai(nhentaiRegex, update, bot) + } + + //balonlyl + var isBalonlyl bool = false + for _, word := range config.BotConfig.Balonlyl.TriggerWords { + if strings.Contains(strings.ToLower(update.Message.Text), word) && commonlogic.ContainsInt64(config.BotConfig.Balonlyl.EnabledChats, update.Message.Chat.ID) { + isBalonlyl = true + break + } + } + if isBalonlyl { + go balonlyl.Balonlyl(update, bot) + } + +}