首页 > 解决方案 > 如何用 discord bot python 警告某人

问题描述

我试图用这个功能警告我的不和谐机器人的人:

async def warn(ctx, member : discord.Member=None, * , reason=None):
  if member is None:
    return await ctx.send("This user could not be found or you didnt't provide one :(")
  if reason is None:
    return await ctx.send("Woah Woah Woah. I'm not letting you ban a user for no reason! Please provide a reason.")
  try:
    first_warning = False
    client.warnings[ctx.guild.id][member.id][0] += 1 
    client.warnings[ctx.guild.id][member.id][1].append((ctx.author.id, reason))
  except KeyError:
    first_warning = True
    client.warnings[ctx.guild.id][member.id] = [1, [(ctx.author.id, reason)]]
  count = client.warnings[ctx.guild.id][member.id][0]
  await ctx.send(f"{member.mention} has {count} {'warning' if first_warning else 'warnings'}")

但它标记了这个回溯错误:

Ignoring exception in command warn:
Traceback (most recent call last):
  File "C:\Users\josho\Documents\Stuuf\bot.py", line 31, in warn
    client.warnings[ctx.guild.id][member.id][0] += 1
KeyError: 852609248352272414

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\***\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\***\Documents\Stuuf\bot.py", line 35, in warn
    client.warnings[ctx.guild.id][member.id] = [1, [(ctx.author.id, reason)]]
KeyError: 852609248352272414

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\***\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\j****\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\j***\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: 852609248352272414

请帮忙!每个人都拒绝了我。你是我唯一的希望!:)

标签: pythonpython-3.xdiscorddiscord.py

解决方案


推荐阅读