Part 5 - Command Groups & Subcommands#

Create a new file named fun.py in the extensions folder - this will contain our bot’s second extension.

In fun.py paste the following:

 1import hikari
 2import lightbulb
 3
 4fun_plugin = lightbulb.Plugin("Fun")
 5
 6
 7@fun_plugin.command
 8@lightbulb.command("fun", "All the entertainment commands you'll ever need!")
 9@lightbulb.implements(lightbulb.PrefixCommandGroup, lightbulb.SlashCommandGroup)
10async def fun_group(ctx: lightbulb.Context) -> None:
11    pass  # as slash commands cannot have their top-level command ran, we simply pass here
12
13
14@fun_group.child
15@lightbulb.command("meme", "Get a meme!")
16@lightbulb.implements(lightbulb.PrefixSubCommand, lightbulb.SlashSubCommand)
17async def meme_subcommand(ctx: lightbulb.Context) -> None:
18    async with ctx.bot.d.aio_session.get(
19        "https://meme-api.herokuapp.com/gimme"
20    ) as response:
21        res = await response.json()
22        if response.ok and res["nsfw"] != True:
23            link = res["postLink"]
24            title = res["title"]
25            img_url = res["url"]
26
27            embed = hikari.Embed(colour=0x3B9DFF)
28            embed.set_author(name=title, url=link)
29            embed.set_image(img_url)
30
31            await ctx.respond(embed)
32
33        else:
34            await ctx.respond(
35                "Could not fetch a meme :c", flags=hikari.MessageFlag.EPHEMERAL
36            )
37
38
39def load(bot: lightbulb.BotApp) -> None:
40    bot.add_plugin(fun_plugin)
  • Line 4 - Create a new plugin named Fun

  • Line 7 - Decorator to attach the following command to the plugin

  • Line 8 - Decorator to create the command, setting the name to “fun” and adding a description

  • Line 9 - Converts the decorated function to a PrefixCommandGroup and SlashCommandGroup

  • Line 10 - The command’s function

  • Line 11 - pass the function, as slash commands cannot have their top-level command ran

  • Line 14 - attach the decorated function to the fun_group command

  • Line 15 - Decorator to create the subcommand, setting the name to meme and adding a description

  • Line 16 - Converts the decorated function to a PrefixSubCommand and SlashSubCommand

  • Line 17 - The subcommand’s function

  • Line 18-21 - Using the aio_session from the bot.d data store that we created in the previous section, get a meme from the API
  • Line 22 - If the response is successful and the meme is not NSFW (Not Safe For Work), then
    • Line 23-25 - Get the meme’s link, title and image url

    • Line 27 - Create an embed

    • Line 28 - Set the embed’s author to the meme’s title and link

    • Line 29 - Set the embed’s image to the meme’s image url

    • Line 31 - Respond to the interaction with the embed

  • Line 33 - Otherwise, if the response was not successful or the meme was NSFW, then
    • Line 34-36 - Respond to the interaction with an ephemeral message, stating that we could not fetch a meme

Now, let’s test it!

_static/meme_1.png _static/meme_2.png

and if we can’t fetch a meme:

_static/meme_3.png

Note

Ephemeral response only work with application commands, not prefix commands