Reply to original message if command invocation was a reply

This commit is contained in:
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

@ -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)