首页 > 解决方案 > 命令引发异常:NameError: name 'challenge_player' is not defined

问题描述

我和我的朋友正在尝试制作一种迷你游戏的不和谐机器人。我正在尝试制作一个挑战命令,该命令采用用户指定的 id 并询问他们是否要接受。

#imports
import discord
from discord.ext import commands

#DISCORD PART#
client = commands.Bot(command_prefix = '-')
@client.event
async def on_ready():
  print(f'We have logged in as {client.user}')

@client.command()
async def challenge(ctx, member: discord.Member = None):
    if not member:
        await ctx.send("Please specify a member!")
    elif member.bot:
        await ctx.send("Bot detected!")
    else:
        await ctx.send(f"**{member.mention} please respond with -accept to accept the challenge!**")
        challenge_player_mention = member.mention
        challenge_player = member.id

@client.command()
async def accept(ctx):
  if ctx.message.author.id == challenge_player:
    await ctx.send(f"{challenge_player_mention} has accepted!")
  else:
    await ctx.send("No one has challenged you!")

@client.event
async def on_message(message):
  print(f"{message.channel}: {message.author}: {message.author.name}: {message.content}")
  
  await client.process_commands(message)

client.run("token")

除了接受命令外,一切正常。

这是错误:

  Traceback (most recent call last):
File "C:\Users\impec\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
  ret = await coro(*args, **kwargs)
File "c:/Users/impec/Downloads/bots/epic gamer bot/epic_gamer.py", line 30, in accept
  if ctx.message.author.id == challenge_player:
NameError: name 'challenge_player' is not defined

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

Traceback (most recent call last):
  File "C:\Users\impec\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\impec\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\impec\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'challenge_player' is not defined

我无法理解我做错了什么。

标签: pythondiscord.py

解决方案


challenge_playerchallenge函数中本地定义,因此您可以从accept函数中访问它。
您需要challenge_player在函数外部声明并在函数内部添加global challenge_player,与以下内容相同challenge_player_mention

import discord
from discord.ext import commands

challenge_player, challenge_player_mention = "", ""

client = commands.Bot(command_prefix = '-')
@client.event
async def on_ready():
  print(f'We have logged in as {client.user}')

@client.command()
async def challenge(ctx, member: discord.Member = None):
    global challenge_player, challenge_player_mention
    if not member:
        await ctx.send("Please specify a member!")
    elif member.bot:
        await ctx.send("Bot detected!")
    else:
        await ctx.send(f"**{member.mention} please respond with -accept to accept the challenge!**")
        challenge_player_mention = member.mention
        challenge_player = member.id

@client.command()
async def accept(ctx):
  global challenge_player, challenge_player_mention
  if ctx.message.author.id == challenge_player:
    await ctx.send(f"{challenge_player_mention} has accepted!")
  else:
    await ctx.send("No one has challenged you!")

@client.event
async def on_message(message):
  print(f"{message.channel}: {message.author}: {message.author.name}: {message.content}")
  
  await client.process_commands(message)

client.run("token")

PS:不要在互联网上分享你的机器人令牌!任何人都可以使用它访问您的机器人并用它做任何他们想做的事情。您应该生成另一个并使用它。


推荐阅读