Fix a few macro and logfilereader issues (#43)

* Try to improve exception logging

* Fix KeyError for new aliases

* List aliases and macros together

* Fix AttributeError when reading logs
This commit is contained in:
TSRBerry 2023-04-26 20:38:52 +02:00 committed by GitHub
parent 994438d3fa
commit e937abb41c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 16 deletions

View file

@ -6,6 +6,7 @@ import sys
import aiohttp
import discord
from discord.ext import commands
from discord.ext.commands import CommandError, Context
if len(sys.argv[1:]) != 1:
sys.stderr.write("usage: <state_dir>")
@ -133,12 +134,12 @@ async def on_command(ctx):
@bot.event
async def on_error(event_method, *args, **kwargs):
log.exception(f"Error on {event_method}:")
async def on_error(event: str, *args, **kwargs):
log.exception(f"Error on {event}:")
@bot.event
async def on_command_error(ctx, error):
async def on_command_error(ctx: Context, error: CommandError):
error_text = str(error)
err_msg = (
@ -147,7 +148,7 @@ async def on_command_error(ctx, error):
f"of type {type(error)}: {error_text}"
)
log.error(err_msg)
log.exception(err_msg, error)
if not isinstance(error, commands.CommandNotFound):
err_msg = bot.escape_message(err_msg)
@ -259,8 +260,8 @@ async def main():
for cog in config.initial_cogs:
try:
await bot.load_extension(cog)
except:
log.exception(f"Failed to load cog {cog}.")
except Exception as e:
log.exception(f"Failed to load cog {cog}:", e)
await bot.start(config.token)

View file

@ -23,7 +23,7 @@ logging.basicConfig(
class LogFileReader(Cog):
@staticmethod
def is_valid_log(attachment: Attachment) -> tuple[bool, bool]:
def is_valid_log_name(attachment: Attachment) -> tuple[bool, bool]:
filename = attachment.filename
# Any message over 2000 chars is uploaded as message.txt, so this is accounted for
ryujinx_log_file_regex = re.compile(r"^Ryujinx_.*\.log|message\.txt$")
@ -94,7 +94,15 @@ class LogFileReader(Cog):
# Large files show a header value when not downloaded completely
# this regex makes sure that the log text to read starts from the first timestamp, ignoring headers
log_file_header_regex = re.compile(r"\d{2}:\d{2}:\d{2}\.\d{3}.*", re.DOTALL)
log_file = re.search(log_file_header_regex, log_file).group(0)
log_file_match = re.search(log_file_header_regex, log_file)
if log_file_match:
log_file = log_file_match.group(0)
else:
return Embed(
colour=self.ryujinx_blue,
description="This log file appears to be invalid. Please make sure to upload a Ryujinx log file.",
)
def is_tid_blocked(log_file=log_file):
game_name = re.search(
@ -828,7 +836,7 @@ class LogFileReader(Cog):
return await message.channel.send(
content=author_mention,
embed=Embed(
description=f"This log file appears to be invalid. Please re-check and re-upload your log file.",
description="This log file appears to be invalid. Please re-check and re-upload your log file.",
colour=self.ryujinx_blue,
),
)
@ -866,7 +874,7 @@ class LogFileReader(Cog):
message = await ctx.fetch_message(ctx.message.reference.message_id)
if len(message.attachments) >= attachment_number:
attachment = message.attachments[attachment_number - 1]
is_log_file, _ = self.is_valid_log(attachment)
is_log_file, _ = self.is_valid_log_name(attachment)
if is_log_file:
return await self.analyse_log_message(
@ -888,7 +896,7 @@ class LogFileReader(Cog):
if message.author.bot:
return
for attachment in message.attachments:
is_log_file, is_ryujinx_log_file = self.is_valid_log(attachment)
is_log_file, is_ryujinx_log_file = self.is_valid_log_name(attachment)
if message.channel.id in self.bot_log_allowed_channels.values():
return await self.analyse_log_message(
@ -904,7 +912,7 @@ class LogFileReader(Cog):
)
elif (
is_log_file
and not message.channel.id in self.bot_log_allowed_channels.values()
and message.channel.id not in self.bot_log_allowed_channels.values()
):
return await message.author.send(
content=message.author.mention,

View file

@ -127,13 +127,24 @@ class Macro(Cog):
@commands.cooldown(3, 30, BucketType.channel)
@commands.command(name="macros", aliases=["ml", "listmacros", "list_macros"])
async def list_macros(self, ctx: Context):
async def list_macros(self, ctx: Context, macros_only=False):
macros = get_macros_dict(self.bot)
if len(macros["macros"]) > 0:
macros = [f"- {key}\n" for key in sorted(macros["macros"].keys())]
message = "📝 **Macros**:\n"
for macro_key in macros:
message += macro_key
for key in sorted(macros["macros"].keys()):
message += f"- {key}\n"
if not macros_only and key in macros["aliases"].keys():
message += " - __aliases__: "
first_alias = True
for alias in macros["aliases"][key]:
if first_alias:
message += alias
first_alias = False
continue
message += f", {alias}"
message += "\n"
await ctx.send(message)
else:
await ctx.send("Couldn't find any macros.")

View file

@ -85,6 +85,8 @@ def add_aliases(bot, key: str, aliases: list[str]) -> bool:
for alias in aliases:
alias = alias.lower()
if is_macro_key_available(bot, alias, macros):
if key not in macros["aliases"].keys():
macros["aliases"][key] = []
macros["aliases"][key].append(alias)
success = True
if success: