Closed
Description
Summary
When using a slash or application command from within a cog, the function never executes. Instead, a TypeError is thrown claiming that the function received multiple values for a single parameter.
Reproduction Steps
- git clone pycord
- pip install pycord/.
- Run app.py as below, with cogs/roles.py loaded
- Attempt to use the
/color
command
Minimal Reproducible Code
app.py:
import discord
from discord.ext import commands
""" Declare intents that the bot will use """
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
intents.reactions = True
""" Initialize the bot """
bot = commands.Bot(
command_prefix=commands.when_mentioned_or('..', '>', '.'),
description="A list of commands available",
intents=intents
)
@bot.event
async def on_ready():
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
print("------")
""" Load the bot token """
from dotenv import load_dotenv
load_dotenv()
import os
TOKEN = os.getenv("BOT_TOKEN")
""" Load cogs """
for file in os.listdir("cogs"):
if not file.endswith(".py"):
continue
name = file[:-3]
bot.load_extension(f"cogs.{name}")
""" Run the bot """
bot.run(TOKEN)
cogs/roles.py:
import discord
from discord.ext import commands
from discord.commands import Option, slash_command
def setup(bot):
bot.add_cog(Roles(bot))
class Roles(commands.Cog):
"""Let users choose their roles"""
def __init__(self, bot):
self.bot = bot
print("Initialized Roles cog")
roles = {
'COLOR_LIGHT_RED': 608191366090063892,
'COLOR_RED': 519029920530169857,
'COLOR_SATAN_RED': 608084225115160616,
[...]
}
async def get_colors(ctx: discord.AutocompleteContext):
"""Returns a list of colors matching the partial input"""
COLORS = [
"Light Red",
"Red",
[...]
"Gray",
"Black",
]
return [color for color in COLORS if color.lower().startswith(ctx.value.lower())]
@slash_command(guild_ids=[518624497780391948])
async def color(
ctx: discord.ApplicationContext,
name: Option(str, "Color name", autocomplete=get_colors),
):
"""Choose the color of your display name"""
color = "COLOR_" + name.replace(" ", "_").upper()
print(color)
try:
role = guild.get_role(self.roles[color])
except KeyError:
return await ctx.respond("Please provide a valid color name.")
await ctx.respond(f"You should now have the {name} role.")
Expected Results
The command is entered and begins to execute (hopefully correctly)
Actual Results
Ignoring exception in command color:
Traceback (most recent call last):
File "/home/trwnh/git/cyberbus2/.venv/lib/python3.10/site-packages/discord/commands/core.py", line 113, in wrapped
ret = await coro(arg)
File "/home/trwnh/git/cyberbus2/.venv/lib/python3.10/site-packages/discord/commands/core.py", line 768, in _invoke
await self.callback(self.cog, ctx, **kwargs)
TypeError: Roles.color() got multiple values for argument 'name'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/trwnh/git/cyberbus2/.venv/lib/python3.10/site-packages/discord/bot.py", line 994, in invoke_application_command
await ctx.command.invoke(ctx)
File "/home/trwnh/git/cyberbus2/.venv/lib/python3.10/site-packages/discord/commands/core.py", line 314, in invoke
await injected(ctx)
File "/home/trwnh/git/cyberbus2/.venv/lib/python3.10/site-packages/discord/commands/core.py", line 119, in wrapped
raise ApplicationCommandInvokeError(exc) from exc
discord.commands.errors.ApplicationCommandInvokeError: Application Command raised an exception: TypeError: Roles.color() got multiple values for argument 'name'
Intents
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
intents.reactions = True
System Information
$ python -m discord -v
- Python v3.10.2-final
- py-cord v2.0.0-beta
- py-cord pkg_resources: v2.0.0b5
- aiohttp v3.7.4.post0
- system info: Linux 5.16.13-arch1-1 #1 SMP PREEMPT Tue, 08 Mar 2022 20:07:36 +0000
Checklist
- I have searched the open issues for duplicates.
- I have shown the entire traceback, if possible.
- I have removed my token from display, if visible.
Additional Context
I am running pycord from the latest commits on the master branch.