Add commands to track compbio leaderboards

This commit is contained in:
stijndcl 2022-03-11 14:11:59 +01:00
parent d18860cae0
commit c43710a429
7 changed files with 125 additions and 16 deletions

View file

@ -45,7 +45,7 @@ def format_command_usage(ctx: Context) -> str:
def format_slash_command_usage(interaction: Interaction) -> str:
# Create a string with the options used
options = " ".join(list(map(
lambda o: f"{o['name']}: \"{o['value']}\"",
lambda o: f"{o['name']}: \"{o['value']}\"" if "value" in o else o["name"],
interaction.data.get("options", [])
)))

View file

@ -4,34 +4,57 @@ import discord
from discord import ApplicationContext
from discord.ext.commands import Context
from data import constants
import settings
def get_display_name(ctx: Union[ApplicationContext, Context], user_id: int) -> str:
def get_member_or_user(ctx: Union[ApplicationContext, Context], user_id: int) -> Optional[Union[discord.Member, discord.User]]:
"""Get a COC Member instance of a user if they are in the server,
otherwise the regular User instance
"""
author = ctx.author if isinstance(ctx, Context) else ctx.user
# Check if this is a DM, or the user is not in the guild
if ctx.guild is None or ctx.guild.get_member(user_id) is None:
# User is the author, no need to fetch their name
if user_id == author.id:
return author.display_name
return author
# Get member instance from CoC
COC = ctx.bot.get_guild(int(constants.DeZandbak))
COC = ctx.bot.get_guild(settings.COC_ID)
member = COC.get_member(user_id)
if member is not None:
return member.display_name
return member
# Try to fetch the user
user = ctx.bot.get_user(user_id)
if user is not None:
return user.name
return user
# User couldn't be found
# Guild exists, use that instead
mem = ctx.guild.get_member(user_id)
return mem
def get_display_name(ctx: Union[ApplicationContext, Context], user_id: int) -> str:
member = get_member_or_user(ctx, user_id)
# Nothing found
if member is None:
return f"[? | {user_id}]"
mem = ctx.guild.get_member(user_id)
return mem.display_name
if isinstance(member, discord.Member):
return member.display_name
return member.name
def get_mention(ctx: Union[ApplicationContext, Context], user_id: int) -> str:
member = get_member_or_user(ctx, user_id)
# Nothing found
if member is None:
return f"[? | {user_id}]"
return member.mention
async def reply_to_reference(ctx: Context, content: Optional[str] = None, embed: Optional[discord.Embed] = None, always_mention=False):