Skip to content

Commit

Permalink
Better exception logging and mention transformation
Browse files Browse the repository at this point in the history
This is pretty much necessary for future tests
  • Loading branch information
SocksTheWolf committed Dec 7, 2024
1 parent 46a700a commit 4fb1885
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
12 changes: 11 additions & 1 deletion CommandHelpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from Logger import Logger, LogLevel
from discord import Interaction, app_commands
import traceback, re

UserIdReg = re.compile("\<\@([0-9]+)\>")

# This transformer allows us to take in a discord id (as the default int is too small)
# and properly convert it to a value that we can use to observe Discord data
Expand All @@ -8,8 +11,15 @@ async def OnTransform(self, interaction: Interaction, TargetId:int) -> int:
return TargetId

async def transform(self, interaction: Interaction, value: str) -> int:
# Capture any mention targets
matches = UserIdReg.match(value)
if (matches is not None):
value = matches.group(1)

# Check if the value is numeric
if (not value.isnumeric()):
return -1

ConvertedValue:int = int(value)
# Prevent any targets on the bot
if (ConvertedValue == interaction.client.user.id):
Expand Down Expand Up @@ -46,7 +56,7 @@ async def CommandErrorHandler(interaction: Interaction, error: app_commands.AppC
else:
ErrorMsg = "To change settings, run `/scamguard config`. To uninstall the bot, simply kick it from your server."
else:
Logger.Log(LogLevel.Error, f"Encountered error running command /{InteractionName}: {str(error)}")
Logger.Log(LogLevel.Error, f"Encountered error running command /{InteractionName}: {str(error)} {traceback.format_exc()}")
ErrorMsg = "An error has occurred while processing your request"

await interaction.response.send_message(ErrorMsg, ephemeral=True, delete_after=5.0)
3 changes: 2 additions & 1 deletion ModalHelpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from discord import ui, Interaction, SelectOption, WebhookMessage, ButtonStyle, Message, TextChannel, ChannelType, Member, Permissions
from Logger import Logger, LogLevel
import traceback

class YesNoSelector(ui.Select):
CurrentSelection:str = ""
Expand Down Expand Up @@ -115,7 +116,7 @@ async def on_timeout(self):
await self.StopInteractions()

async def on_error(self, interaction:Interaction, error:Exception, object:ui.Item):
Logger.Log(LogLevel.Error, f"View interaction encountered an error {str(error)}")
Logger.Log(LogLevel.Error, f"View interaction encountered an error {str(error)} {traceback.format_exc()}")

async def on_cancel(self, interaction:Interaction):
pass
Expand Down

0 comments on commit 4fb1885

Please sign in to comment.