Reply to original message if command invocation was a reply

pull/105/head
Stijn De Clercq 2022-02-11 23:08:28 +01:00
parent 0165700d9f
commit b1fdd22058
4 changed files with 29 additions and 5 deletions

View File

@ -7,6 +7,7 @@ from data.menus import custom_commands
from data.snipe import Action, Snipe from data.snipe import Action, Snipe
from decorators import help from decorators import help
from enums.help_categories import Category from enums.help_categories import Category
from functions.utils import reply_to_reference
from startup.didier import Didier from startup.didier import Didier
@ -20,7 +21,7 @@ class Other(commands.Cog):
@commands.command(name="Link", usage="[Naam]") @commands.command(name="Link", usage="[Naam]")
@help.Category(category=Category.Other) @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 Send commonly used links
""" """
@ -29,7 +30,7 @@ class Other(commands.Cog):
if match is None: if match is None:
return await ctx.reply(f"Geen match gevonden voor \"{name}\".", mention_author=False, delete_after=15) 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") @commands.command(name="Custom")
@help.Category(category=Category.Didier) @help.Category(category=Category.Didier)

View File

@ -9,6 +9,7 @@ from enums.help_categories import Category
from functions import config, les from functions import config, les
from functions.stringFormatters import capitalize from functions.stringFormatters import capitalize
from functions.timeFormatters import skip_weekends from functions.timeFormatters import skip_weekends
from functions.utils import reply_to_reference
class School(commands.Cog): class School(commands.Cog):
@ -101,7 +102,9 @@ class School(commands.Cog):
# Get the guide for the current year # Get the guide for the current year
year = 2018 + int(config.get("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"]) @commands.command(name="Deadlines", aliases=["dl"])
@help.Category(category=Category.School) @help.Category(category=Category.School)

View File

@ -77,7 +77,7 @@
"year": 2 "year": 2
}, },
"Parallelle Computersystemen": { "Parallelle Computersystemen": {
"abbreviations": ["PCS"], "abbreviations": ["Paracomp", "Parallelle", "PCS"],
"alt": "Parallel Computer Systems", "alt": "Parallel Computer Systems",
"code": "E034140", "code": "E034140",
"year": 3 "year": 3

View File

@ -1,5 +1,6 @@
from typing import Union from typing import Union, Optional
import discord
from discord import ApplicationContext from discord import ApplicationContext
from discord.ext.commands import Context 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) mem = ctx.guild.get_member(user_id)
return mem.display_name 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)