Skip to content

Instantly share code, notes, and snippets.

@sexnine
Created September 5, 2021 13:10
Show Gist options
  • Save sexnine/fdd0ced033d17158e8b0af08afdde4d9 to your computer and use it in GitHub Desktop.
Save sexnine/fdd0ced033d17158e8b0af08afdde4d9 to your computer and use it in GitHub Desktop.
py-cord dynamically adding commands example
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