Skip to content

Instantly share code, notes, and snippets.

@sexnine
Created October 5, 2021 01:48
Show Gist options
  • Save sexnine/3fd4c7e56c9962df2229d2662759084b to your computer and use it in GitHub Desktop.
Save sexnine/3fd4c7e56c9962df2229d2662759084b to your computer and use it in GitHub Desktop.
Slash commands in cogs - Pycord
from discord.ext import commands
import discord
from . import api
from typing import Optional
from discord.app import Option
slash_categories = {
...
}
category_aliases = {
...
}
class Jokes(commands.Cog): # May be renamed to Fun or Random
def __init__(self, bot):
self.bot = bot
self.bot.application_command(name="joke", cls=discord.SlashCommand)(self.joke_slash)
@commands.command(name="joke")
async def joke_cmd(self, ctx: commands.Context, category: Optional[str] = "any"):
category = category.lower()
category = category_aliases.get(category) or category
response = await api.get_joke(category)
joke_type = response.get("type")
if joke_type == "single":
content = response.get("joke")
elif joke_type == "twopart":
content = f"{response.get('setup')}\n\n{response.get('delivery')}"
else:
raise Exception("unknown joke type")
await ctx.send(embed=discord.Embed(description=content))
async def joke_slash(self, ctx: commands.Context, category: Option(str, "Category",
choices=["Dark", "Programming", "Dad Joke",
"Spooky", "Christmas"],
required=False)):
await self.joke_cmd(ctx, slash_categories.get(category, "any"))
def setup(bot):
bot.add_cog(Jokes(bot))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment