Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Context Menu command for Whois #868

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ __pycache__
.pytest_cache
.coverage
.hypothesis
.venv/
36 changes: 31 additions & 5 deletions techsupport_bot/commands/who.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"""Module for the who extension for the discord bot."""

from __future__ import annotations

import datetime
import io
from typing import TYPE_CHECKING

import discord
import ui
Expand All @@ -11,6 +14,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."""
Expand Down Expand Up @@ -47,14 +53,23 @@ 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 = (
Expand All @@ -64,15 +79,14 @@ 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)

await interaction.response.send_message(embed=embed, ephemeral=True)

raise commands.CommandError(message)

@app_commands.check(is_reader)
@app_commands.command(
name="whois",
description="Gets Discord user information",
Expand All @@ -81,7 +95,19 @@ 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."""
# 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 "",
Expand Down
Loading