我正在制作一个1vs1游戏,它向用户发送邀请并等待接受。这是我的代码:
global hasRun
hasRun = False
@client.command()
async def accept(ctx):
global hasRun
hasRun = True
@client.command()
async def fight(ctx, user: discord.Member):
await ctx.send('sending invite to user')
await user.send('press `/accept` to start the game')
await ctx.send('waiting for accept..')
global hasRun
if hasRun == True:
await ctx.send("game started")
代码不会删除任何错误,但它就是不起作用。知道错在哪里吗?
您不需要等待hasRun是否为true。一旦您发送Waiting for accept...
,它就会检查hasRun是否为true,这很可能是False。
您可以使用wait_for()
,而不是使用单独的命令和全局变量
检查功能将检查您要求的用户是否发送了接受游戏的消息。您还应该包括一个超时。
import asyncio # Required for timeout
# ...
@client.command()
async def fight(ctx, user: discord.Member):
await ctx.send('sending invite to user')
await user.send('press `accept` to start the game')
await ctx.send('waiting for accept..')
def check(message):
return message.author == user and message.content in ('accept', '/accept')
try:
msg = await client.wait_for('message', check=check, timeout=60.0)
except asyncio.TimeoutError:
await ctx.send("User didn't accept game in time")
# Code can go here