I’m trying to write a Discord bot in discord.py that just responds to commands, like when you type =!firewall it says Hello! Here’s my code:
import discord from discord.ext import commands TOKEN = ('') client = discord.Client() bot = commands.Bot(command_prefix='=!') @bot.event async def on_ready(): print('Bot ready') @bot.event async def on_message(message): if message.author == client.user: return @bot.command(pass_context=True) async def chickennuggets(ctx): await message.channel.send("Hello!") await bot.process_commands(message) bot.run(TOKEN)
However, when I try to run the command with =!firewall in Discord, the console returns this:
Ignoring exception in on_message Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event await coro(*args, **kwargs) File "/Users/pixdoet/Code/python/firewall/bottest.py", line 19, in on_message async def chickennuggets(ctx): File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/ext/commands/core.py", line 1257, in decorator self.add_command(result) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/discord/ext/commands/core.py", line 1143, in add_command raise CommandRegistrationError(command.name) discord.ext.commands.errors.CommandRegistrationError: The command chickennuggets is already an existing command or alias.
As it is pretty obvious, chickennuggets is not a default command, nor a command used by any other libraries in Python. I have searched the Internet for this but with no avail. Please help. Thanks!
Advertisement
Answer
First of all, you’re referencing your command chickennuggets
with a different name in your post, which tells me that you’re using an alias for the command in your code. However, you haven’t shown the alias or how you’re defining it. You’re indentation in your code is also incorrect. Try this out:
import discord from discord.ext import commands client = discord.Client() bot = commands.Bot(command_prefix='=!') @bot.event async def on_ready(): print('Bot ready') @bot.event async def on_message(message): if message.author == client.user: return await bot.process_commands(message) @bot.command(pass_context=True, aliases = ['firewall']) async def chickennuggets(ctx): await ctx.send("Hello!") TOKEN = ('') # insert your token here bot.run(TOKEN)
Also, please don’t leak your bot’s token to any public forum, because others can run their code through your bot using your bot’s token. Now, you’ll have to go to your application in Discord’s Developer Portal and generate a new token. This will null your previous token so that nobody can use it to run their code on your bot.