[英]Make a discord.py bot wait for a command
我正在尝试在我的机器人中创建一个 tictactoe 命令,并且我想让它在玩家 1 使用 tictactoe 命令后,它会等待玩家 2 执行 tictactoe 命令,但我希望它在 30 后超时秒无tictactoe 命令。 这是我的 tictactoe 命令代码:
@commands.command(aliases=['ristinolla', 'ttt', 'rn'])
async def tictactoe(ctx, x, y):
lang = await ctx.bot.get_lang(ctx)
try:
turn = ctx.bot.players[ctx.channel.id][2]
X = ctx.bot.players[ctx.channel.id][0]
O = ctx.bot.players[ctx.channel.id][1]
if ctx.author.id != turn:
if lang == "fi":
await ctx.send("❌ **|** Ei ole sinun vuorosi")
else:
await ctx.send("❌ **|** It is not your turn")
elif x[0] not in ctx.bot.x and y[0] not in ctx.bot.y:
await ctx.send("Usage:\n{}tictactoe <top/middle/bottom> <left/middle/right>".format(bot.prefix))
else:
if turn == X:
piece = "X"
else:
piece = "O"
lauta = ctx.bot.boards[ctx.channel.id]
if lauta[int(ctx.bot.x[str(x[0])])][int(ctx.bot.y[str(y[0])])] != " ":
if lang == "fi":
await ctx.send("❌ **|** Paikka varattu, kokeile toista paikkaa")
else:
await ctx.send("❌ **|** Spot is already taken, try another spot")
return
lauta[int(ctx.bot.x[str(x[0])])][int(ctx.bot.y[str(y[0])])] = piece
await ctx.send(await ctx.bot.lautavisual(lauta))
#wincheck
if (lauta[0] == [piece, piece, piece]
or lauta[1] == [piece, piece, piece]
or lauta[2] == [piece, piece, piece]
or lauta[0][0] == piece and lauta[1][1] == piece and lauta[2][2] == piece
or lauta[0][2] == piece and lauta[1][1] == piece and lauta[2][0] == piece
or lauta[0][0] == piece and lauta[1][0] == piece and lauta[2][0] == piece
or lauta[0][1] == piece and lauta[1][1] == piece and lauta[2][1] == piece
or lauta[0][2] == piece and lauta[1][2] == piece and lauta[2][2] == piece):
if lang == "fi":
await ctx.send("🎲 **|** {} on voittanut!".format(ctx.author.mention))
else:
await ctx.send("🎲 **|** {} has won!".format(ctx.author.mention))
ctx.bot.boards.pop(ctx.channel.id)
ctx.bot.players.pop(ctx.channel.id)
return
elif (lauta[0][0] != " "
and lauta[0][1] != " "
and lauta[0][2] != " "
and lauta[1][0] != " "
and lauta[1][1] != " "
and lauta[1][2] != " "
and lauta[2][0] != " "
and lauta[2][1] != " "
and lauta[2][2] != " "):
if lang == "fi":
await ctx.send("🎲 **|** Tasapeli!")
else:
await ctx.send("🎲 **|** Tie!")
ctx.bot.boards.pop(ctx.channel.id)
ctx.bot.players.pop(ctx.channel.id)
return
else:
if turn == X:
ctx.bot.players[ctx.channel.id][2] = O
else:
ctx.bot.players[ctx.channel.id][2] = X
if lang == "fi":
await ctx.send("🎲 **|** <@{}>, nyt on sinun vuorosi".format(ctx.bot.players[ctx.guild.id][2]))
else:
await ctx.send("🎲 **|** <@{}>, it is now your turn".format(ctx.bot.players[ctx.guild.id][2]))
注意:您不能将此代码复制粘贴到您的代码中。 这只是给你一个想法
def check(m: discord.Message):
return m.author.id==turn.id and m.content.startswith("{your prefix}tictactoe")
try:
msg = await bot.wait_for(event = 'message', check = check, timeout = 30.0)
return
except asyncio.TimeoutError:
await ctx.send(f"**{ctx.author}**, you didn't play in 30 seconds..")
return
else:
#your code to place the piece
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.