Added channel param to auto-role add & fixed on reaction add event stuff #54

This commit is contained in:
Sven Heidemann 2022-10-05 16:43:24 +02:00
parent c7c60edbd7
commit 8966bded81
4 changed files with 30 additions and 13 deletions

View File

@ -9,19 +9,19 @@
"Command": { "Command": {
"Path": "logs/", "Path": "logs/",
"Filename": "commands.log", "Filename": "commands.log",
"ConsoleLogLevel": "DEBUG", "ConsoleLogLevel": "TRACE",
"FileLogLevel": "TRACE" "FileLogLevel": "TRACE"
}, },
"Database": { "Database": {
"Path": "logs/", "Path": "logs/",
"Filename": "database.log", "Filename": "database.log",
"ConsoleLogLevel": "DEBUG", "ConsoleLogLevel": "TRACE",
"FileLogLevel": "TRACE" "FileLogLevel": "TRACE"
}, },
"Message": { "Message": {
"Path": "logs/", "Path": "logs/",
"Filename": "message.log", "Filename": "message.log",
"ConsoleLogLevel": "DEBUG", "ConsoleLogLevel": "TRACE",
"FileLogLevel": "TRACE" "FileLogLevel": "TRACE"
} }
}, },

View File

@ -63,7 +63,7 @@
"add": { "add": {
"success": "auto-role für die Nachricht {} wurde hinzugefügt :D", "success": "auto-role für die Nachricht {} wurde hinzugefügt :D",
"error": { "error": {
"not_found": "auto-role für die Nachricht {} nicht gefunden!", "not_found": "Nachricht {} nicht in {} nicht gefunden!",
"already_exists": "auto-role für die Nachricht {} existiert bereits!" "already_exists": "auto-role für die Nachricht {} existiert bereits!"
} }
}, },
@ -88,7 +88,7 @@
"success": "Regel {} -> {} für auto-role {} wurde hinzugefügt :D", "success": "Regel {} -> {} für auto-role {} wurde hinzugefügt :D",
"error": { "error": {
"not_found": "Regel für auto-role {} nicht gefunden!", "not_found": "Regel für auto-role {} nicht gefunden!",
"emoji_not_found": "Eomji {} für auto-role Regel {} nicht gefunden!", "emoji_not_found": "Emoji {} für auto-role Regel {} nicht gefunden!",
"rule_not_found": "Rolle {} für auto-role Regel {} nicht gefunden!", "rule_not_found": "Rolle {} für auto-role Regel {} nicht gefunden!",
"already_exists": "Regel für auto-role {} existiert bereits!" "already_exists": "Regel für auto-role {} existiert bereits!"
} }

View File

@ -90,7 +90,7 @@ class AutoRoleGroup(DiscordCommandABC):
@auto_role.command() @auto_role.command()
@commands.guild_only() @commands.guild_only()
async def add(self, ctx: Context, message_id: str): async def add(self, ctx: Context, channel: discord.TextChannel, message_id: str):
self._logger.debug(__name__, f'Received command auto-role add {ctx} {message_id}') self._logger.debug(__name__, f'Received command auto-role add {ctx} {message_id}')
if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx): if not await self._client_utils.check_if_bot_is_ready_yet_and_respond(ctx):
return return
@ -100,10 +100,10 @@ class AutoRoleGroup(DiscordCommandABC):
self._logger.trace(__name__, f'Finished command auto-role add') self._logger.trace(__name__, f'Finished command auto-role add')
return return
message = List(discord.Message, [message async for message in ctx.channel.history(limit=50)]).where(lambda m: m.id == int(message_id)).single_or_default() message = List(discord.Message, [message async for message in channel.history(limit=50)]).where(lambda m: m.id == int(message_id)).single_or_default()
if message is None: if message is None:
self._logger.debug(__name__, f'Message with id {message_id} not found in {ctx.channel.name}') self._logger.debug(__name__, f'Message with id {message_id} not found in {channel.name}')
await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.add.error.not_found').format(message_id)) await self._message_service.send_ctx_msg(ctx, self._t.transform('modules.auto_role.add.error.not_found').format(message_id, channel.name))
self._logger.trace(__name__, f'Finished command auto-role add') self._logger.trace(__name__, f'Finished command auto-role add')
return return
@ -123,6 +123,14 @@ class AutoRoleGroup(DiscordCommandABC):
@add.autocomplete('message_id') @add.autocomplete('message_id')
async def add_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: async def add_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
channel = discord.utils.get(interaction.guild.text_channels, id=interaction.channel_id) channel = discord.utils.get(interaction.guild.text_channels, id=interaction.channel_id)
try:
channel_from_data = interaction.data['options'][0]['options'][0]['value']
found_channel = discord.utils.get(interaction.guild.text_channels, id=int(channel_from_data))
if found_channel is not None:
channel = found_channel
finally:
pass
messages = [message async for message in channel.history(limit=10)] messages = [message async for message in channel.history(limit=10)]
return [app_commands.Choice(name=f'{message.author}@{message.created_at}', value=str(message.id)) for message in messages if current in str(message.id)] return [app_commands.Choice(name=f'{message.author}@{message.created_at}', value=str(message.id)) for message in messages if current in str(message.id)]
@ -292,4 +300,5 @@ class AutoRoleGroup(DiscordCommandABC):
@remove.autocomplete('auto_role_rule') @remove.autocomplete('auto_role_rule')
async def remove_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]: async def remove_autocomplete(self, interaction: discord.Interaction, current: str) -> TList[app_commands.Choice[str]]:
rules = self._auto_roles.get_auto_role_rules() rules = self._auto_roles.get_auto_role_rules()
return [app_commands.Choice(name=f'{rule.auto_role_rule_id} {rule.emoji_name} {interaction.guild.get_role(int(rule.role_id))}', value=rule.auto_role_rule_id) for rule in rules] return [app_commands.Choice(name=f'{rule.auto_role_rule_id} {rule.emoji_name} {interaction.guild.get_role(int(rule.role_id))}', value=rule.auto_role_rule_id) for rule in
rules]

View File

@ -28,20 +28,27 @@ class ReactionHandler:
self._roles = self._auto_roles.get_auto_roles() self._roles = self._auto_roles.get_auto_roles()
async def handle(self, payload: RawReactionActionEvent, r_type=None) -> None: async def handle(self, payload: RawReactionActionEvent, r_type=None) -> None:
self._logger.trace(__name__, f'Handle reaction {payload} {r_type}')
if payload.message_id not in self._message_ids: if payload.message_id not in self._message_ids:
self._logger.debug(__name__, f'Message not in auto-roles - skipping')
return return
guild = self._bot.get_guild(payload.guild_id) guild = self._bot.get_guild(payload.guild_id)
user = await guild.fetch_member(payload.user_id) user = await guild.fetch_member(payload.user_id)
if user.bot: if user.bot:
self._logger.debug(__name__, f'User is bot - skipping')
return return
emoji = payload.emoji.name emoji = payload.emoji.name
auto_role: AutoRole = self._roles.where(lambda x: x.discord_message_id).first_or_default() auto_role: AutoRole = self._roles.where(lambda x: x.discord_message_id == payload.message_id).single_or_default()
if auto_role is None: if auto_role is None:
self._logger.debug(__name__, f'auto-role for message not found - skipping')
return return
rules: List[AutoRoleRule] = self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role.auto_role_id) rules: List[AutoRoleRule] = self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role.auto_role_id)
if rules.count() == 0:
self._logger.debug(__name__, f'auto-role rules not found - skipping')
return
for rule in rules: for rule in rules:
if emoji != rule.emoji_name: if emoji != rule.emoji_name:
@ -51,8 +58,9 @@ class ReactionHandler:
role = guild.get_role(rule.role_id) role = guild.get_role(rule.role_id)
self._logger.debug(__name__, f'Assign role {role.name} to {user.name}') self._logger.debug(__name__, f'Assign role {role.name} to {user.name}')
await user.add_roles(role) await user.add_roles(role)
elif r_type == 'remove':
if r_type == 'remove':
role = guild.get_role(rule.role_id) role = guild.get_role(rule.role_id)
self._logger.debug(__name__, f'Remove role {role.name} to {user.name}') self._logger.debug(__name__, f'Remove role {role.name} to {user.name}')
await user.remove_roles(role) await user.remove_roles(role)
else:
self._logger.warn(__name__, f'Invalid reaction type {r_type}')