python - 命令引发异常: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
我无法理解我做错了什么。
解决方案
challenge_player
在challenge
函数中本地定义,因此您可以从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:不要在互联网上分享你的机器人令牌!任何人都可以使用它访问您的机器人并用它做任何他们想做的事情。您应该生成另一个并使用它。
推荐阅读
- php - 从 PHP 中失败的 curl 响应中获取状态文本
- node.js - EHOSTUNREACH - 尝试通过 localhost 向后端 API 发出请求时出错
- vba - 在 VBA 中使用数组元素计算计算值平均值的公式
- c - 为什么缺少一些 libhandy 库函数?
- python-3.x - 在 elif 正文中将数组元素转换为整数导致错误
- angular - 如何根据另一个 observable 的结果订阅(或不订阅!)一个 observable
- javascript - 如何在打字稿中访问枚举?给出错误“元素隐式具有任何类型,因为索引表达式不是数字类型”
- ajax - 如何在laravel中有正常查询数据的页面中使用AJAX
- java - 如何加速非常大的集合的延迟加载
- visual-studio - Visual Studio VTR8.0 中的 CMake 构建错误