Add a /settings command to check group settings

Signed-off-by: Sayan Biswas <sayan@pokurt.me>
This commit is contained in:
Sayan Biswas 2021-10-20 00:03:47 +05:30
parent d5123be9d1
commit 93c799fed3
No known key found for this signature in database
GPG Key ID: E1220C019C89B488
8 changed files with 92 additions and 21 deletions

View File

@ -7,13 +7,16 @@ import (
)
type ShiinaConfig struct {
BotToken string
DatabaseName string
BotAdmins []int64
Debug bool
DropUpdates bool
BotToken string
DatabaseName string
CoffeeHouseKey string
BotAdmins []int64
Debug bool
DropUpdates bool
}
var Data *ShiinaConfig
func NewShiinaConfig() *ShiinaConfig {
return &ShiinaConfig{}
}
@ -30,6 +33,7 @@ func (c *ShiinaConfig) ReadFile(path string) error {
c.BotAdmins = conf.GetInt64List("ShiinaConfig.BotAdmins")
c.Debug = conf.GetBoolean("ShiinaConfig.Debug")
c.DropUpdates = conf.GetBoolean("ShiinaConfig.DropUpdates")
Data = c
return nil
}

View File

@ -10,9 +10,19 @@ type Chat struct {
}
func (c *Chat) DoesAutoBan() bool {
if c == nil {
return false
}
return c.AutoBan
}
func (c *Chat) DetectSpam() bool {
if c == nil {
return false
}
return c.SpamDetect
}
func InsertChat(ChatID int64, AutoBan bool, SpamDetect bool, SpamAction string) {
tx := SESSION.Begin()
chat := &Chat{ChatID: ChatID, AutoBan: AutoBan, SpamDetect: SpamDetect, SpamAction: SpamAction}

View File

@ -21,8 +21,7 @@ func autoBanHandler(b *gotgbot.Bot, ctx *ext.Context) error {
}
data, err := spamProtection.GetInfoByID(user.Id)
if err != nil {
helpers.SendError(err, ctx, b)
return err
return helpers.SendError(err, ctx, b)
}
if !data.Success {
return nil
@ -46,4 +45,4 @@ func msgFilter(msg *gotgbot.Message) bool {
return false
}
return msg.From != nil
}
}

View File

@ -1,7 +1,11 @@
package handlers
const (
HELP_CMD = "help"
START_CMD = "start"
FETCH_CMD = "fetch"
HelpCmd = "help"
StartCmd = "start"
FetchCmd = "fetch"
SettingsCmd = "settings"
PrivateChat = "private"
MarkdownV2 = "markdownv2"
TimeLayout = "Jan 2, 2006 at 3:04pm (MST)"
)

View File

@ -13,9 +13,9 @@ func getHelp(u *gotgbot.User) string {
msg = msg.AppendNormal("I am a bot, with the primary purpose of adding more overhead on @SpamProtectionBot.").AppendNormal("\n")
msg = msg.AppendNormal("Due to this, I function quite similarly to it, I might look like a clone of it.").AppendNormalThis("\n\n")
msg = msg.AppendBold("Here are the commands I currently accept").AppendNormal(":\n")
msg = msg.AppendNormal("- ").AppendMono(START_CMD).AppendNormal(": starts me, as usual\n")
msg = msg.AppendNormal("- ").AppendMono(HELP_CMD).AppendNormal(": makes me send THIS message\n")
msg = msg.AppendNormal("- ").AppendMono(FETCH_CMD).AppendNormalThis(": fetch a user on the API")
msg = msg.AppendNormal("- ").AppendMono(StartCmd).AppendNormal(": starts me, as usual\n")
msg = msg.AppendNormal("- ").AppendMono(HelpCmd).AppendNormal(": makes me send THIS message\n")
msg = msg.AppendNormal("- ").AppendMono(FetchCmd).AppendNormalThis(": fetch a user on the API")
return msg.ToString()
}
@ -26,8 +26,7 @@ func helpHandler(b *gotgbot.Bot, ctx *ext.Context) error {
{Text: "Click me for help!", Url: fmt.Sprintf("t.me/%s?start=help", b.Username)},
}}}})
if err != nil {
helpers.SendError(err, ctx, b)
return err
return helpers.SendError(err, ctx, b)
}
} else {

View File

@ -6,14 +6,16 @@ import (
)
func LoadHandlers(d *ext.Dispatcher) {
startCMD := handlers.NewCommand(START_CMD, startHandler)
helpCMD := handlers.NewCommand(HELP_CMD, helpHandler)
getMeCMD := handlers.NewCommand(FETCH_CMD, fetchHandler)
startCMD := handlers.NewCommand(StartCmd, startHandler)
helpCMD := handlers.NewCommand(HelpCmd, helpHandler)
getMeCMD := handlers.NewCommand(FetchCmd, fetchHandler)
settingsCMD := handlers.NewCommand(SettingsCmd, settingsHandler)
autoBan := handlers.NewMessage(msgFilter, autoBanHandler)
logHandler := handlers.NewMessage(logChatFilter, logChat)
d.AddHandler(startCMD)
d.AddHandler(helpCMD)
d.AddHandler(getMeCMD)
d.AddHandler(settingsCMD)
d.AddHandler(autoBan)
d.AddHandler(logHandler)
}

46
handlers/settings.go Normal file
View File

@ -0,0 +1,46 @@
package handlers
import (
"fmt"
"github.com/ALiwoto/mdparser/mdparser"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"gitlab.com/Dank-del/SpamProtection-Mirror-Bot/database"
"gitlab.com/Dank-del/SpamProtection-Mirror-Bot/helpers"
"time"
)
func settingsHandler(b *gotgbot.Bot, ctx *ext.Context) error {
chat := ctx.EffectiveChat
message := ctx.EffectiveMessage
if chat.Type == PrivateChat {
_, err := message.Reply(b, mdparser.GetItalic("Command only for group").ToString(), &gotgbot.SendMessageOpts{ParseMode: MarkdownV2})
return helpers.SendError(err, ctx, b)
}
data, err := database.GetChat(chat.Id)
if err != nil {
return helpers.SendError(err, ctx, b)
}
txt := mdparser.GetBold(chat.Title).AppendMono(fmt.Sprintf(" (%d)", chat.Id)).AppendNormal("\n\n")
if data == nil {
txt.AppendItalicThis("No data found")
} else {
txt.AppendBoldThis("SpamProtection Bans: ")
if data.DoesAutoBan() {
txt.AppendMonoThis("On")
} else {
txt.AppendMonoThis("Off")
}
txt.AppendNormalThis("\n")
txt.AppendBoldThis("Spam Prediction: ")
if data.DetectSpam() {
txt.AppendMonoThis("On")
} else {
txt.AppendMonoThis("Off")
}
txt.AppendNormalThis("\n")
txt.AppendNormalThis("\n").AppendBoldThis("Fetched on ").AppendBoldThis(time.Now().UTC().Format(TimeLayout))
}
_, err = message.Reply(b, txt.ToString(), &gotgbot.SendMessageOpts{ParseMode: MarkdownV2})
return helpers.SendError(err, ctx, b)
}

View File

@ -8,10 +8,17 @@ import (
"strings"
)
func SendError(err error, ctx *ext.Context, b *gotgbot.Bot) {
func SendError(err error, ctx *ext.Context, b *gotgbot.Bot) error {
if err == nil {
return nil
}
msg := mdparser.GetBold("Error: ").AppendItalic(err.Error())
_, er := b.SendMessage(ctx.EffectiveChat.Id, strings.ReplaceAll(msg.ToString(), b.Token, "token"), &gotgbot.SendMessageOpts{ParseMode: "markdownv2"})
txt := msg.ToString()
m := strings.ReplaceAll(txt, b.Token, "token")
m = strings.ReplaceAll(m, core.Data.CoffeeHouseKey, "cf_key")
_, er := b.SendMessage(ctx.EffectiveChat.Id, m, &gotgbot.SendMessageOpts{ParseMode: "markdownv2"})
if er != nil {
core.SUGARED.Error(err)
}
return err
}