Python 如何修复命令多次不工作的问题
我有一个启动游戏的命令Python 如何修复命令多次不工作的问题,python,discord.py,Python,Discord.py,我有一个启动游戏的命令,-host。担任管理员角色的人员可以通过变量主机使用-cleargames停止游戏并删除消息 但是,如果我运行-host,然后运行-cleargames,它就会工作。如果我再这样做,我就会出错 这适用于使用discord.py asyncio的discord服务器。我不断得到错误: cleargames命令已注册 它应该能够多次执行-host和-cleargames命令,而不会出现错误。同一命令不能有多个版本。当您第二次尝试运行host时,它会再次尝试以cleargame
,-host
。担任管理员
角色的人员可以通过变量主机
使用-cleargames
停止游戏并删除消息
但是,如果我运行-host
,然后运行-cleargames
,它就会工作。如果我再这样做,我就会出错
这适用于使用discord.py asyncio的discord服务器。我不断得到错误:
cleargames命令已注册
它应该能够多次执行
-host
和-cleargames
命令,而不会出现错误。同一命令不能有多个版本。当您第二次尝试运行host
时,它会再次尝试以cleargames
的名称注册命令,但失败
相反,编写两个单独的命令,通过相互访问的全局变量共享状态
playerList = None
@client.command(pass_context=True)
async def host(ctx):
global playerList
if playerList:
return
host = ctx.message.author
message = (f"__**Player List**__ \n \n{host.mention} (Host)")
playerList = await client.say(message)
@client.command(pass_context=True)
@has_role("Admin")
async def cleargames(ctx):
global playerList
if playerList:
await client.delete_message(playerList)
playerList = None
await client.delete_message(ctx.message)
notfication = await client.say("Games cleared.")
await asyncio.sleep(5)
await client.delete_message(notification)
当我第二次尝试时,主持人甚至不起作用。没有错误或任何事情。您正在执行
主机主机cleargames
或主机cleargames
主机主机?
playerList = None
@client.command(pass_context=True)
async def host(ctx):
global playerList
if playerList:
return
host = ctx.message.author
message = (f"__**Player List**__ \n \n{host.mention} (Host)")
playerList = await client.say(message)
@client.command(pass_context=True)
@has_role("Admin")
async def cleargames(ctx):
global playerList
if playerList:
await client.delete_message(playerList)
playerList = None
await client.delete_message(ctx.message)
notfication = await client.say("Games cleared.")
await asyncio.sleep(5)
await client.delete_message(notification)