discord.py 1vs1 游戏 - 接受邀请功能
discord.py 1vs1 game - accept invite function
我正在制作一个向用户发送邀请并等待接受的 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 是否为真。一旦您发送 Waiting for accept...
,它就会检查 hasRun 是否为真,这很可能为假。
您可以使用 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
我正在制作一个向用户发送邀请并等待接受的 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 是否为真。一旦您发送 Waiting for accept...
,它就会检查 hasRun 是否为真,这很可能为假。
您可以使用 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