【发布时间】:2021-04-27 22:27:24
【问题描述】:
当我在 Discord 中通过 *grant @wolex9 add 时,我得到以下异常:
Ignoring exception in command grant:
Traceback (most recent call last):
File "/Users/test/PycharmProjects/slapdash/venv/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 902, in invoke
await ctx.command.invoke(ctx)
File "/Users/test/PycharmProjects/slapdash/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 856, in invoke
await self.prepare(ctx)
File "/Users/test/PycharmProjects/slapdash/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 790, in prepare
await self._parse_arguments(ctx)
File "/Users/test/PycharmProjects/slapdash/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 706, in _parse_arguments
kwargs[name] = await self.transform(ctx, param)
File "/Users/test/PycharmProjects/slapdash/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 552, in transform
return await self.do_conversion(ctx, converter, argument, param)
File "/Users/test/PycharmProjects/slapdash/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 505, in do_conversion
return await self._actual_conversion(ctx, converter, argument, param)
File "/Users/test/PycharmProjects/slapdash/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 451, in _actual_conversion
ret = await instance.convert(ctx, argument)
File "/Users/test/PycharmProjects/slapdash/venv/lib/python3.8/site-packages/discord/ext/commands/converter.py", line 191, in convert
raise MemberNotFound(argument)
discord.ext.commands.errors.MemberNotFound: Member "<@!id> add" not found.
@wolex9 是在 Discord 中提及我自己,它被 Discord 识别,而不是在转换为会员时被我的机器人识别。
grant函数代码(虽然是私人房间烫发系统):
@bot.command()
async def grant(ctx, *, member: discord.Member = None, command=None):
if (
member is not None and # if member argument provided
command is not None and # if command argument provided
ctx.channel.category_id in instances and # if in dictionary
member.id != instances[ctx.channel.category_id]['admin_id'] and # if not admin
command in ['add', 'remove', 'exit', 'grant', 'deny'] and
(
ctx.author.id == instances[ctx.channel.category_id]['admin_id'] or # if admin
ctx.author.id in instances[ctx.channel.category_id]['granted']['grant_id_list'] # if granted
)
):
if command == 'add' and member.id not in instances[ctx.channel.category_id]['granted']['add_id_list']:
instances[ctx.channel.category_id]['granted']['add_id_list'].append(member.id)
await ctx.send(f"Successfully granted to {member.mention}, but you'd better grant `remove` as well!")
elif (
member is not None and # if member argument provided
command is None and # if command argument not provided
ctx.channel.category_id in instances and # if in dictionary
member.id != instances[ctx.channel.category_id]['admin_id'] and # if not admin
(
ctx.author.id == instances[ctx.channel.category_id]['admin_id'] or # if admin
ctx.author.id in instances[ctx.channel.category_id]['granted']['grant_id_list'] # if granted
)
):
if member.id not in instances[ctx.channel.category_id]['granted']['add_id_list']: # if not add
instances[ctx.channel.category_id]['granted']['add_id_list'].append(member.id)
if member.id not in instances[ctx.channel.category_id]['granted']['remove_id_list']: # if not remove
instances[ctx.channel.category_id]['granted']['remove_id_list'].append(member.id)
await ctx.send(f"Successfully granted `add` and `remove` to {member.mention}!")
else:
await ctx.send('Use `*help` to learn commands ????')
【问题讨论】:
-
这是一个非常混乱的代码,请编辑您的答案以便于阅读
-
@ŁukaszKwieciński 现在好点了吗?我删除了不相关的 elifs
标签: python python-3.x exception discord discord.py