From b1fdd2205867870733c39c63c9bea05b26197ebe Mon Sep 17 00:00:00 2001 From: Stijn De Clercq Date: Fri, 11 Feb 2022 23:08:28 +0100 Subject: [PATCH] Reply to original message if command invocation was a reply --- cogs/other.py | 5 +++-- cogs/school.py | 5 ++++- files/courses.json | 2 +- functions/utils.py | 22 +++++++++++++++++++++- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/cogs/other.py b/cogs/other.py index 83c5ed4..819ab72 100644 --- a/cogs/other.py +++ b/cogs/other.py @@ -7,6 +7,7 @@ from data.menus import custom_commands from data.snipe import Action, Snipe from decorators import help from enums.help_categories import Category +from functions.utils import reply_to_reference from startup.didier import Didier @@ -20,7 +21,7 @@ class Other(commands.Cog): @commands.command(name="Link", usage="[Naam]") @help.Category(category=Category.Other) - async def link(self, ctx, name: str): + async def link(self, ctx: commands.Context, name: str): """ Send commonly used links """ @@ -29,7 +30,7 @@ class Other(commands.Cog): if match is None: return await ctx.reply(f"Geen match gevonden voor \"{name}\".", mention_author=False, delete_after=15) - return await ctx.reply(match, mention_author=False) + await reply_to_reference(ctx, content=match) @commands.command(name="Custom") @help.Category(category=Category.Didier) diff --git a/cogs/school.py b/cogs/school.py index e2fc347..4e6d484 100644 --- a/cogs/school.py +++ b/cogs/school.py @@ -9,6 +9,7 @@ from enums.help_categories import Category from functions import config, les from functions.stringFormatters import capitalize from functions.timeFormatters import skip_weekends +from functions.utils import reply_to_reference class School(commands.Cog): @@ -101,7 +102,9 @@ class School(commands.Cog): # Get the guide for the current year year = 2018 + int(config.get("year")) - return await ctx.reply(f"https://studiekiezer.ugent.be/studiefiche/nl/{course.code}/{year}", mention_author=False) + link = f"https://studiekiezer.ugent.be/studiefiche/nl/{course.code}/{year}" + + return await reply_to_reference(ctx, content=link) @commands.command(name="Deadlines", aliases=["dl"]) @help.Category(category=Category.School) diff --git a/files/courses.json b/files/courses.json index 005ea83..d7ec000 100644 --- a/files/courses.json +++ b/files/courses.json @@ -77,7 +77,7 @@ "year": 2 }, "Parallelle Computersystemen": { - "abbreviations": ["PCS"], + "abbreviations": ["Paracomp", "Parallelle", "PCS"], "alt": "Parallel Computer Systems", "code": "E034140", "year": 3 diff --git a/functions/utils.py b/functions/utils.py index b1a692a..a56865c 100644 --- a/functions/utils.py +++ b/functions/utils.py @@ -1,5 +1,6 @@ -from typing import Union +from typing import Union, Optional +import discord from discord import ApplicationContext from discord.ext.commands import Context @@ -31,3 +32,22 @@ def get_display_name(ctx: Union[ApplicationContext, Context], user_id: int) -> s mem = ctx.guild.get_member(user_id) return mem.display_name + + +async def reply_to_reference(ctx: Context, content: Optional[str] = None, embed: Optional[discord.Embed] = None, always_mention=False): + """Reply to a message + In case the message is a reply to another message, try to reply to that one instead and ping the author + otherwise, reply to the message that invoked the command & only mention the author if necessary + """ + # Message is a reply + if ctx.message.reference is not None: + cached = ctx.message.reference.cached_message + + # Reference is not cached anymore: fetch it + if cached is None: + # Message is in the same channel, otherwise no way to reply to it + cached = await ctx.channel.fetch_message(ctx.message.reference.message_id) + + return await cached.reply(content, embed=embed, mention_author=cached.author != ctx.author) + + return await ctx.reply(content, embed=embed, mention_author=always_mention)