Fix relationship, add github links, improve error messages

This commit is contained in:
stijndcl 2022-09-20 17:34:49 +02:00
parent 41c8c9d0ab
commit 9e3527ae8a
8 changed files with 152 additions and 13 deletions

View file

@ -4,7 +4,7 @@ import discord
from discord import app_commands
from discord.ext import commands
from database.crud import birthdays, bookmarks
from database.crud import birthdays, bookmarks, github
from database.exceptions import (
DuplicateInsertException,
Forbidden,
@ -15,6 +15,7 @@ from didier import Didier
from didier.exceptions import expect
from didier.menus.bookmarks import BookmarkSource
from didier.menus.common import Menu
from didier.utils.discord import colours
from didier.utils.discord.assets import get_author_avatar
from didier.utils.types.datetime import str_to_date
from didier.utils.types.string import leading
@ -193,6 +194,40 @@ class Discord(commands.Cog):
modal = CreateBookmark(self.client, message.jump_url)
await interaction.response.send_modal(modal)
@commands.group(name="github", aliases=["gh", "git"], case_insensitive=True, invoke_without_command=True)
async def github(self, ctx: commands.Context, user: discord.User):
"""Show a user's GitHub links"""
embed = discord.Embed(colour=colours.github_white(), title="GitHub Links")
embed.set_author(name=user.display_name, icon_url=user.avatar.url or user.default_avatar.url)
embed.set_footer(text="Links can be added using `didier github add <link>`.")
async with self.client.postgres_session as session:
links = await github.get_github_links(session, user.id)
if not links:
embed.description = "This user has not set any GitHub links yet."
else:
regular_links = []
ugent_links = []
for link in links:
if "github.ugent.be" in link.url.lower():
ugent_links.append(link)
else:
regular_links.append(link)
regular_links.sort()
ugent_links.sort()
if ugent_links:
embed.add_field(name="Ghent University", value="\n".join(ugent_links), inline=False)
if regular_links:
embed.add_field(name="Other", value="\n".join(regular_links), inline=False)
return await ctx.reply(embed=embed, mention_author=False)
@commands.command(name="join")
async def join(self, ctx: commands.Context, thread: discord.Thread):
"""Make Didier join `thread`.

View file

@ -188,7 +188,7 @@ class CustomHelpCommand(commands.MinimalHelpCommand):
embed.add_field(name="Signature", value=signature, inline=False)
if command.aliases:
embed.add_field(name="Aliases", value=", ".join(command.aliases), inline=False)
embed.add_field(name="Aliases", value=", ".join(sorted(command.aliases)), inline=False)
def _get_cog(self, cogs: list[commands.Cog], name: str) -> Optional[commands.Cog]:
"""Try to find a cog, case-insensitively"""

View file

@ -1,6 +1,7 @@
import logging
import os
import pathlib
import re
from functools import cached_property
from typing import Union
@ -284,23 +285,31 @@ class Didier(commands.Bot):
):
return await ctx.reply(str(exception.original), mention_author=False)
# Print everything that we care about to the logs/stderr
await super().on_command_error(ctx, exception)
if isinstance(exception, commands.MessageNotFound):
return await ctx.reply("This message could not be found.", ephemeral=True, delete_after=10)
if isinstance(exception, (commands.MissingRequiredArgument,)):
message = str(exception)
match = re.search(r"(.*) is a required argument that is missing\.", message)
if match.groups():
message = f"Found no value for the `{match.groups()[0]}`-argument."
return await ctx.reply(message, ephemeral=True, delete_after=10)
if isinstance(
exception,
(
commands.BadArgument,
commands.MissingRequiredArgument,
commands.UnexpectedQuoteError,
commands.ExpectedClosingQuoteError,
),
):
return await ctx.reply("Invalid arguments.", ephemeral=True, delete_after=10)
# Print everything that we care about to the logs/stderr
await super().on_command_error(ctx, exception)
if settings.ERRORS_CHANNEL is not None:
embed = create_error_embed(ctx, exception)
channel = self.get_channel(settings.ERRORS_CHANNEL)

View file

@ -1,12 +1,23 @@
import discord
__all__ = ["error_red", "ghent_university_blue", "ghent_university_yellow", "google_blue", "urban_dictionary_green"]
__all__ = [
"error_red",
"github_white",
"ghent_university_blue",
"ghent_university_yellow",
"google_blue",
"urban_dictionary_green",
]
def error_red() -> discord.Colour:
return discord.Colour.red()
def github_white() -> discord.Colour:
return discord.Colour.from_rgb(250, 250, 250)
def ghent_university_blue() -> discord.Colour:
return discord.Colour.from_rgb(30, 100, 200)