Created
September 5, 2021 13:10
-
-
Save sexnine/fdd0ced033d17158e8b0af08afdde4d9 to your computer and use it in GitHub Desktop.
py-cord dynamically adding commands example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Optional | |
import discord | |
from discord.ext import commands | |
from nekosbest import Client as NekosBestClient | |
nekos_best_client = NekosBestClient() | |
class Reaction: | |
def __init__(self, name: str, action: str = None, user_menu: bool = False, no_mention: str = None, mention: str = None): | |
self.no_mention = no_mention | |
self.mention = mention | |
self.action = action | |
self.name = name | |
self.user_menu = user_menu | |
def add_commands(reaction: Reaction, bot: commands.Bot) -> None: | |
async def base_command(ctx, user: discord.Member = None): | |
no_mention_msg = reaction.no_mention | |
mention_msg = reaction.mention | |
if user and mention_msg: | |
message = mention_msg.format(author=ctx.author.mention, user=user.mention) | |
else: | |
message = no_mention_msg.format(author=ctx.author.mention) | |
result = await nekos_best_client.get_image(reaction.name) | |
embed = discord.Embed(description=f"**{message}**", colour=discord.Colour(0x1d97c)) | |
embed.set_image(url=result.url) | |
await ctx.send(embed=embed, content=user.mention if user else None) | |
if reaction.mention and reaction.no_mention: | |
async def func(ctx, user: Optional[discord.Member]) -> None: | |
await base_command(ctx, user) | |
elif reaction.mention: | |
async def func(ctx, user: discord.Member) -> None: | |
await base_command(ctx, user) | |
else: | |
async def func(ctx) -> None: | |
await base_command(ctx) | |
bot.add_command(commands.Command(name=reaction.name, func=func)) | |
bot.application_command(name=reaction.name, clas=discord.SlashCommand)(func) | |
if reaction.user_menu: | |
bot.application_command(name=reaction.action, cls=discord.UserCommand)(func) | |
reaction_gifs = { | |
Reaction(name="hug", action="Hug", mention="{author} gave {user} a warm fuzzy hug π", user_menu=True), | |
Reaction(name="pat", action="Pat", mention="{author} patted {user}'s head π₯Ί", user_menu=True), | |
Reaction(name="poke", action="Poke", mention="{author} poked {user} π€ ", user_menu=True), | |
Reaction(name="kiss", action="Kiss", mention="{author} kissed {user} π³", user_menu=True), | |
Reaction(name="slap", action="Slap", mention="{author} slapped {user} π€", user_menu=True), | |
Reaction(name="facepalm", action="Facepalm", no_mention="{author} is very disappointed π€¦β"), | |
Reaction(name="thumbsup", action="Thumbs up", no_mention="{author} π"), | |
Reaction(name="smile", action="Smile", no_mention="{author} smiles π", mention="{author} smiles at {user} π"), | |
Reaction(name="wave", action="Wave", no_mention="{author} waves π", mention="{author} waved at {user} π"), | |
Reaction(name="shrug", action="Shrug", no_mention="{author} shrugs π€·β", mention="{author} shrugs at {user} π€·"), | |
} | |
class ReactionGifs(commands.Cog): | |
def __init__(self, bot: commands.bot): | |
print("Loading ReactionGif") | |
self.bot = bot | |
for reaction in reaction_gifs: | |
add_commands(reaction, self.bot) | |
print("Loaded ReactionGif") | |
def setup(bot): | |
bot.add_cog(ReactionGifs(bot)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment