From 66e5578da379b18a2c885dca3552a0e018832887 Mon Sep 17 00:00:00 2001 From: dkay0670 Date: Mon, 12 Feb 2024 17:29:50 -0800 Subject: [PATCH 1/3] Add Context Menu command for Whois --- .gitignore | 1 + techsupport_bot/commands/who.py | 29 +++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index fddf80af4..a7caa1e67 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ __pycache__ .pytest_cache .coverage .hypothesis +.venv/ \ No newline at end of file diff --git a/techsupport_bot/commands/who.py b/techsupport_bot/commands/who.py index b9d8f3228..ff30dfdaf 100644 --- a/techsupport_bot/commands/who.py +++ b/techsupport_bot/commands/who.py @@ -1,5 +1,9 @@ """Module for the who extension for the discord bot.""" +from __future__ import annotations + +from typing import TYPE_CHECKING, List + import datetime import io @@ -11,6 +15,9 @@ from discord import app_commands from discord.ext import commands +if TYPE_CHECKING: + import bot + async def setup(bot): """Adding the who configuration to the config file.""" @@ -47,14 +54,22 @@ async def setup(bot): class Who(cogs.BaseCog): """Class to set up who for the extension.""" + def __init__(self, bot: bot.TechSupportBot, extension_name): + super().__init__(bot, extension_name=extension_name) + self.ctx_menu = app_commands.ContextMenu( + name="Whois", + callback=self.get_note_command, + extras={"brief": "Gets user data", "usage": "@user", "module": "who"}, + ) + self.bot.tree.add_command(self.ctx_menu) + notes = app_commands.Group( name="note", description="Command Group for the Notes Extension" ) @staticmethod async def is_reader(interaction: discord.Interaction) -> bool: - """Checks whether invoker can read notes. If at least one reader - role is not set, all members can read notes.""" + """Checks whether invoker can read notes. If no reader role is set, all members can read notes.""" config = interaction.client.guild_configs[str(interaction.guild.id)] if reader_roles := config.extensions.who.note_readers.value: roles = ( @@ -64,7 +79,7 @@ async def is_reader(interaction: discord.Interaction) -> bool: return any((role in interaction.user.roles for role in roles)) - # Reader_roles are empty (not set) + # Reader_roles is empty (not set) message = "There aren't any `note_readers` roles set in the config!" embed = auxiliary.prepare_deny_embed(message=message) @@ -81,7 +96,13 @@ async def is_reader(interaction: discord.Interaction) -> bool: async def get_note( self, interaction: discord.Interaction, user: discord.Member ) -> None: - """ "Method to get notes assigned to a user.""" + """The base of the get_note command""" + await self.get_note_command(interaction, user) + + async def get_note_command( + self, interaction: discord.Interaction, user: discord.Member + ) -> None: + """Method to get notes assigned to a user.""" embed = discord.Embed( title=f"User info for `{user}`", description="**Note: this is a bot account!**" if user.bot else "", From 4b7e72c80b1206deb3042dcc67035ebd9c9ed75d Mon Sep 17 00:00:00 2001 From: dkay0670 Date: Mon, 12 Feb 2024 17:46:41 -0800 Subject: [PATCH 2/3] CI Fixes --- techsupport_bot/commands/who.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/techsupport_bot/commands/who.py b/techsupport_bot/commands/who.py index ff30dfdaf..a99186da9 100644 --- a/techsupport_bot/commands/who.py +++ b/techsupport_bot/commands/who.py @@ -2,10 +2,9 @@ from __future__ import annotations -from typing import TYPE_CHECKING, List - import datetime import io +from typing import TYPE_CHECKING import discord import ui @@ -69,7 +68,8 @@ def __init__(self, bot: bot.TechSupportBot, extension_name): @staticmethod async def is_reader(interaction: discord.Interaction) -> bool: - """Checks whether invoker can read notes. If no reader role is set, all members can read notes.""" + """Checks whether invoker can read notes. + If no reader role is set, all members can read notes.""" config = interaction.client.guild_configs[str(interaction.guild.id)] if reader_roles := config.extensions.who.note_readers.value: roles = ( From 6665debecbfe7aff5fefed28e929f77d312fcd1a Mon Sep 17 00:00:00 2001 From: dkay0670 Date: Tue, 13 Feb 2024 08:25:26 -0800 Subject: [PATCH 3/3] Add newline to gitignore and fix Whois check --- .gitignore | 2 +- techsupport_bot/commands/who.py | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index a7caa1e67..77190459c 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,4 @@ __pycache__ .pytest_cache .coverage .hypothesis -.venv/ \ No newline at end of file +.venv/ diff --git a/techsupport_bot/commands/who.py b/techsupport_bot/commands/who.py index a99186da9..4e25d5a20 100644 --- a/techsupport_bot/commands/who.py +++ b/techsupport_bot/commands/who.py @@ -87,7 +87,6 @@ async def is_reader(interaction: discord.Interaction) -> bool: raise commands.CommandError(message) - @app_commands.check(is_reader) @app_commands.command( name="whois", description="Gets Discord user information", @@ -103,6 +102,12 @@ async def get_note_command( self, interaction: discord.Interaction, user: discord.Member ) -> None: """Method to get notes assigned to a user.""" + # Check if user is a note reader + if not await self.is_reader(interaction): + embed = auxiliary.prepare_deny_embed(message="You cannot run whois") + await interaction.response.send_message(embed=embed, ephemeral=True) + return + embed = discord.Embed( title=f"User info for `{user}`", description="**Note: this is a bot account!**" if user.bot else "",