Skip to content

Instantly share code, notes, and snippets.

@Phxntxm
Last active April 21, 2024 22:42
Show Gist options
  • Save Phxntxm/5027106223b366616823553cb03f4482 to your computer and use it in GitHub Desktop.
Save Phxntxm/5027106223b366616823553cb03f4482 to your computer and use it in GitHub Desktop.
A pretty simplified way to implement custom commands into your bot.
from discord.ext import commands
# The check to ensure this is the guild used where the command was made
def guild_check(_custom_commands):
async def predicate(ctx):
return _custom_commands.get(ctx.command.qualified_name) and ctx.guild.id in _custom_commands.get(ctx.command.qualified_name)
return commands.check(predicate)
class CustomCommands(commands.Cog):
""" Each entry in _custom_commands will look like this:
{
"command_name": {
guild_id: "This guild's output",
guild_id2: "This other guild's output",
}
}
"""
_custom_commands = {}
@commands.command()
async def add_command(self, ctx, name, *, output):
# First check if there's a custom command with that name already
existing_command = self._custom_commands.get(name)
# Check if there's a built in command, we don't want to override that
if existing_command is None and ctx.bot.get_command(name):
return await ctx.send(f"A built in command with the name {name} is already registered")
# Now, if the command already exists then we just need to add/override the message for this guild
if existing_command:
self._custom_commands[name][ctx.guild.id] = output
# Otherwise, we need to create the command object
else:
@commands.command(name=name, help=f"Custom command: Outputs your custom provided output")
@guild_check(self._custom_commands)
async def cmd(self, ctx):
await ctx.send(self._custom_commands[ctx.invoked_with][ctx.guild.id])
cmd.cog = self
# And add it to the cog and the bot
self.__cog_commands__ = self.__cog_commands__ + (cmd,)
ctx.bot.add_command(cmd)
# Now add it to our list of custom commands
self._custom_commands[name] = {ctx.guild.id: output}
await ctx.send(f"Added a command called {name}")
@commands.command()
async def remove_command(self, ctx, name):
# Make sure it's actually a custom command, to avoid removing a real command
if name not in self._custom_commands or ctx.guild.id not in self._custom_commands[name]:
return await ctx.send(f"There is no custom command called {name}")
# All that technically has to be removed, is our guild from the dict for the command
del self._custom_commands[name][ctx.guild.id]
await ctx.send(f"Removed a command called {name}")
def setup(bot):
bot.add_cog(CustomCommands(bot))
@Phxntxm
Copy link
Author

Phxntxm commented Nov 2, 2021

im having a error whenever i run the cmd: discord.ext.commands.errors.CommandNotFound: Command "add" is not found

There is no command called "add" in this gist. It's called "add_command" just as an example, if you want to name it something different you'll have to understand how the commands extension works. Documentation can be found here:

https://discordpy.readthedocs.io/en/stable/ext/commands/api.html
"Subcommands" probably make sense for this, but this gist is purely an example - you're meant to customize it to your liking

@benthetechguy
Copy link

This is no longer compatible with discord.py 2.0 and later. See this forum post in the discord.py server for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment