InstantCommands

Note

These docs refers to the version 2..0. Make sure you’re under the good version by typing [p]cog update.

This is the guide for the instantcmd cog. Everything you need is here.

[p] is considered as your prefix.

Installation

To install the cog, first load the downloader cog, included in core Red.:

[p]load downloader

Then you will need to install the Laggron’s Dumb Cogs repository:

[p]repo add Laggrons-Dumb-Cogs https://github.com/retke/Laggrons-Dumb-Cogs v3

Finally, you can install the cog:

[p]cog install Laggrons-Dumb-Cogs instantcmd

Warning

The cog is not loaded by default. To load it, type this:

[p]load instantcmd

Usage

InstantCommands is designed to create new objects, like commands, directly from Discord. You just need basic Python and discord.py knowledge.

You can also edit the Dev’s environment added with Red 3.4.6.

From a code snippet in Discord, you can create the following objects:

More objects will come in future releases, like application commands, message components, cogs…

To add a code snippet, use instantcmd create and paste the code you want, following the format described below. You can then manage code snippets with instantcmd list.

Adding commands

Adding a command is very straightforward:

Warning

Don’t forget to always return your object at the end!

Adding listeners

Adding a listener requires a custom decorator:

To prevent conflicts, or name your code snippets better, you can give your function a different name and provide the listener name in the decorator:

Your code will be saved and referred as “member_welcomer”.

Adding dev env values

You can add custom dev env values, which will be made available to Red’s dev cog ([p]debug, [p]eval and [p]repl commands). For more information, see Red’s documentation.

The format is similar to listeners:

Just like listeners, you can give your function a different name and provide the dev value name in the decorator:

Your code will be saved and referred as “give_me_a_dragon”.

Adding views

You can register views that are then sent using the sendview command.

You do not need to write a function with a decorator, instead it’s a class, just like a normal view:

from discord.ui import View, button

class SecretPing(View):
    @button(label="Ping", style=discord.ButtonStyle.primary)
    async def ping(self, interaction, button):
        await interaction.response.send_message(
            f"Hi {interaction.user.mention} but in private", ephemeral=True
        )

return SecretPing

Then run [p]instantcmd sendview SecretPing Some message content to make the bot send a message with your view attached.

Check out the documentation on discord.ui.View and the corresponding decorators below.

Warning

The default timeout for a view is 180 seconds! You can change it by overriding the default parameters of the view object.

The cog currently has no support for permanent views.

Commands

Here’s a list of all commands of this cog:

instantcommand

Syntax:

[p][instacmd|instantcmd|instantcommand]

Description

This is the main command used for setting up the code. It will be used for all other commands.

instantcommand create

Syntax:

[p]instantcommand [create|add]

Description

Creates a new command/listener from a code snippet.

You will be asked to give a code snippet which will contain your function. It can be any supported object as described above.

Tip

Here are the available values within your code snippet:

  • bot (client object)

  • discord

  • commands

  • checks

  • asyncio

  • redbot

  • instantcmd_cog (well, the InstantCommands cog)

If you try to add a new command/listener that already exists, the bot will ask you if you want to replace the command/listener, useful for a quick bug fix instead of deleting each time.

The code can be provided in the same message of the command, in a new followup message, or inside an attached text file.

instantcommand list

Syntax

[p]instantcommand list

Description

Lists the code snippets added with instantcmd.

Multiple select menus will be sent for each type of object, click them and select the object you want to edit.

Once selected, a new message will be sent containing the source of the message and 3 buttons: download the source file, enable/disable this object, and delete it.

instantcommand sendview

Syntax:

[p]instantcommand sendview <view> [channel] <message>

Description

Make the bot send a message with content <message>, in [channel] or the current channel if not specified.

The instantcmd-registered <view> will be attached to that message.

Frequently Asked Questions

My command was added but doesn’t respond when invoked.

If a command is not invoked, this is most likely due to missing arguments. Please check that you only have the ctx argument and no self argument.

Can I use Config in my command?

Yes you can. The Config module is already imported, you just need to use it as in a cog.

Tip

Here’s an example

@commands.command(name="test")
async def my_command(ctx):
    config = Config.get_conf(cog_instance="InstantCommands", identifier=42)
    # use anything but 260 for the identifier
    # since it's the one used for the cog settings
    config.register_guild(**{
        "foo": None
    })

    await config.guild(ctx.guild).foo.set("bar")
    await ctx.send("Well done")

return my_command

How can limit a command for some users?

You can use the checks module, like in a normal cog.

Tip

Here’s an example

@commands.command()
@checks.admin_or_permissions(administrator=True)
async def command(ctx):
    # your code

return command

How can I import a module?

You can import your modules outside the function as you wish.

Tip

Here’s an example

from redbot.core import modlog
import time

@commands.command()
async def command(ctx):
    # your code

return command