Skip to content

Commit

Permalink
refactore error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
AshokShau committed Jul 25, 2024
1 parent 13bec7c commit e332c5a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 40 deletions.
10 changes: 3 additions & 7 deletions FallenSub/modules/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package modules

import (
"fmt"
"github.com/Abishnoi69/Force-Sub-Bot/FallenSub/config"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"time"
Expand All @@ -19,8 +18,7 @@ func start(b *gotgbot.Bot, ctx *ext.Context) error {
}

if _, err := ctx.EffectiveMessage.Reply(b, text, &gotgbot.SendMessageOpts{ReplyMarkup: button}); err != nil {
config.ErrorLog.Printf("[Start] Error sending message - %v", err)
return err
return logError(fmt.Sprintf("[Start] Error sending message - %v", err), err)
}

return ext.EndGroups
Expand All @@ -31,11 +29,9 @@ func ping(b *gotgbot.Bot, ctx *ext.Context) error {
startTime := time.Now()

if msg, err := ctx.EffectiveMessage.Reply(b, "Pong!", nil); err != nil {
config.ErrorLog.Printf("[Ping] Error sending message - %v", err)
return err
return logError(fmt.Sprintf("[Ping] Error sending message - %v", err), err)
} else if _, _, err := msg.EditText(b, "Pong! "+time.Since(startTime).String(), nil); err != nil {
config.ErrorLog.Printf("[Ping] Error editing message - %v", err)
return err
return logError(fmt.Sprintf("[Ping] Error editing message - %v", err), err)
}

return ext.EndGroups
Expand Down
48 changes: 15 additions & 33 deletions FallenSub/modules/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,10 @@ func fSubWatcher(b *gotgbot.Bot, ctx *ext.Context) error {

member, err := b.GetChatMember(fSub.ForceSubChannel, user.Id, nil)
if err != nil {
go func() {
err = db.SetFSub(chat.Id, false)
if err != nil {
config.ErrorLog.Printf("[fSubWatcher]Error setting fSub to false: %s [chatId: %d]", err, chat.Id)
}
}()
_ = db.SetFSub(chat.Id, false)
text := "Force Sub disabled because I can't get your chat member status. Please add me as an admin."
_, _ = b.SendMessage(chat.Id, text, nil)
config.ErrorLog.Printf("[fSubWatcher]Error getting chat member: %s [chatId: %d]", err, fSub.ForceSubChannel)
return err
return logError(fmt.Sprintf("[fSubWatcher]Error getting chat member: %s [chatId: %d]", err, fSub.ForceSubChannel), err)
}

if member.GetStatus() == "member" || member.GetStatus() == "administrator" || member.GetStatus() == "creator" {
Expand All @@ -60,20 +54,17 @@ func fSubWatcher(b *gotgbot.Bot, ctx *ext.Context) error {

_, err = b.RestrictChatMember(chat.Id, user.Id, chatMutePermissions, &gotgbot.RestrictChatMemberOpts{UseIndependentChatPermissions: false})
if err != nil {
config.ErrorLog.Printf("[fSubWatcher]Error restricting user: %s [chatId: %d]", err, chat.Id)
return err
return logError(fmt.Sprintf("[fSubWatcher]Error restricting user: %s [chatId: %d]", err, chat.Id), err)
}

err = db.UpdateMuted(chat.Id, user.Id)
if err != nil {
config.ErrorLog.Printf("[fSubWatcher]Error updating muted user: %s [chatId: %d]", err, chat.Id)
return err
return logError(fmt.Sprintf("[fSubWatcher]Error updating muted user: %s [chatId: %d]", err, chat.Id), err)
}

channel, err := b.GetChat(fSub.ForceSubChannel, nil)
if err != nil {
config.ErrorLog.Printf("[fSubWatcher]Error getting channel info: %s [chatId: %d]", err, chat.Id)
return err
return logError(fmt.Sprintf("[fSubWatcher]Error getting channel info: %s [chatId: %d]", err, chat.Id), err)
}

inviteLink := channel.InviteLink
Expand All @@ -92,7 +83,7 @@ func fSubWatcher(b *gotgbot.Bot, ctx *ext.Context) error {
}
_, err = msg.Reply(b, text, &gotgbot.SendMessageOpts{ReplyMarkup: button, ReplyParameters: &gotgbot.ReplyParameters{AllowSendingWithoutReply: true}})
if err != nil {
config.ErrorLog.Printf("[fSubWatcher]Error sending message: %s [chatId: %d]", err, chat.Id)
return logError(fmt.Sprintf("[fSubWatcher]Error replying to message: %s [chatId: %d]", err, chat.Id), err)
}

return ext.EndGroups
Expand All @@ -107,15 +98,12 @@ func unMuteMe(b *gotgbot.Bot, ctx *ext.Context) error {
isMuted, err := db.IsMuted(chat.Id, user.Id)
if !isMuted {
_, err = query.Answer(b, &gotgbot.AnswerCallbackQueryOpts{Text: "You are not muted by me.", ShowAlert: true})
if err != nil {
return err
}
return ext.EndGroups
return err
}

fSub, err := db.GetFSubSetting(chat.Id)
if err != nil {
config.ErrorLog.Printf("[unMuteMe]Error getting fSub setting: %s [chatId: %d]", err, chat.Id)
return err
return logError(fmt.Sprintf("[unMuteMe]Error getting fSub setting: %s [chatId: %d]", err, chat.Id), err)
}

member, err := b.GetChatMember(fSub.ForceSubChannel, user.Id, nil)
Expand All @@ -128,36 +116,30 @@ func unMuteMe(b *gotgbot.Bot, ctx *ext.Context) error {

if stats.Status != "member" && stats.Status != "administrator" && stats.Status != "creator" {
_, err = query.Answer(b, &gotgbot.AnswerCallbackQueryOpts{Text: "You are not a member of the channel.\nTap on Join Channel Button", ShowAlert: true})
if err != nil {
return err
}
return ext.EndGroups
return err
}

c, err := b.GetChat(chat.Id, nil)
if err != nil {
config.ErrorLog.Printf("[unMuteMe]Error getting chat info: %s [chatId: %d]", err, chat.Id)
return err
return logError(fmt.Sprintf("[unMuteMe]Error getting chat info: %s [chatId: %d]", err, chat.Id), err)
}

_, err = b.RestrictChatMember(chat.Id, user.Id, *c.Permissions, &gotgbot.RestrictChatMemberOpts{UseIndependentChatPermissions: true})
if err != nil {
config.ErrorLog.Printf("[unMuteMe]Error unrestricting user: %s [chatId: %d]", err, chat.Id)
return err
return logError(fmt.Sprintf("[unMuteMe]Error restricting user: %s [chatId: %d]", err, chat.Id), err)
}

err = db.RemoveMuted(chat.Id, user.Id)
if err != nil {
config.ErrorLog.Printf("[unMuteMe]Error removing muted user: %s [chatId: %d]", err, chat.Id)
return err
return logError(fmt.Sprintf("[unMuteMe]Error removing muted user: %s [chatId: %d]", err, chat.Id), err)
}

_, err = query.Answer(b, &gotgbot.AnswerCallbackQueryOpts{Text: "You are unMuted now.", ShowAlert: true})
if err != nil {
return err
return logError(fmt.Sprintf("[unMuteMe]Error answering callback: %s [chatId: %d]", err, chat.Id), err)
}

_, _, _ = query.Message.EditText(b, "You are unMuted now.", nil)
_, _, _ = query.Message.EditText(b, "You are now unMuted and can participate in the chat again.", nil)

return ext.EndGroups
}

0 comments on commit e332c5a

Please sign in to comment.