python - 如何在 discord.py 中创建多个同名的不和谐命令?
问题描述
如何在 Discord.py 中添加多个同名命令?例如:
@client.command(aliases=["dices"])
async def dice(ctx, num):
try:
num=int(num)
bla bla bla
except ValueError:
await ctx.send("Invalid Number!")
@client.command(name='dice',aliases=["dices"])
async def dice_no_param(ctx):
try:
roll = random.randint(1,6)
bla bla bla
except ValueError:
await ctx.send("Invalid Number!")
但显然,我得到了一个错误。
Traceback (most recent call last):
File "main.py", line 1, in <module>
import bot
File "/home/runner/HamburgerBot/bot.py", line 147, in <module>
async def dice_no_param(ctx):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 1163, in decorator
self.add_command(result)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 1071, in add_command
raise discord.ClientException('Command {0.name} is already registered.'.format(command))
discord.errors.ClientException: Command dice is already registered.
解决方案
你不能,但为了你的dice
命令目的,你可以这样做
@client.command(name='dice', aliases=['dices'])
async def dice(ctx, num=None):
num = num or random.randint(1, 6)
try:
num = int(num)
except ValueError:
return await ctx.send("Invalid Number!")
# bla bla bla other code
推荐阅读
- android - 在删除受影响的行之前识别它们
- angular - Angular 8 - 在其他组件内显示组件
- r - 在 R 中使用 ggplot() 时 y 轴比例限制不起作用
- json - 忽略 BEFORE_APP_SERIALIZED 异常:TypeError:将循环结构转换为 JSON
- javascript - 如何写出 Promise.race 的反义词
- c++ - C++,“由于数据类型的范围有限,比较总是正确的”
- winapi - Windows 7 上的 GDI+ 透明位图
- node.js - RTSP 流因错误退出”:连接超时
- r - 子集列表中的问题
- python - Python seaborn plot 将数据名称显示为ㅁㅁㅁㅁ。我怎样才能解决这个问题?