[英]I'm trying to make a user reply to a bot response then send another message
How could I make the bot respond again?我怎样才能让机器人再次响应? I trying to make the bot make a user respond to the message being sent then respond saying if it's too late or they did it.
我试图让机器人让用户响应正在发送的消息,然后回复说是否为时已晚或他们做到了。
import discord
from discord.ext import commands
import time
import random
@commands.command(aliases=["RandomChocolate", "choco"])
@commands.cooldown(1, 15, commands.BucketType.user)
async def chocolate(self, ctx):
food=["hershey", "kitkat", "milk"]
rF = random.choice(food)
rFC = rF[:1]
rFL = rF[-1]
await ctx.send(f"**Hint:** It starts with **{rFC}**
and ends with **{rFL}**, you have 15 seconds to answer
by the way.")
if ctx.message.content == rF:
await ctx.send("Ok")
else:
time.sleep(15)
await ctx.send(f"Too Late!")
You can use await bot.wait_for('message')
to wait for a message.您可以使用
await bot.wait_for('message')
来等待消息。 By passing a check
argument, we can also specify details about the message we're waiting for.通过传递一个
check
参数,我们还可以指定我们正在等待的消息的详细信息。 I'm reusing my message_check
code from this other answer我正在重用来自其他答案的
message_check
代码
class MyCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command(aliases=["RandomChocolate", "choco"])
@commands.cooldown(1, 15, commands.BucketType.user)
async def chocolate(self, ctx):
food=["hershey", "kitkat", "milk"]
rF = random.choice(food)
rFC = rF[:1]
rFL = rF[-1]
await ctx.send(f"**Hint:** It starts with **{rFC}**
and ends with **{rFL}**, you have 15 seconds to answer
by the way.")
try:
response = await self.bot.wait_for("message", timeout=15, check=message_check(channel=ctx.channel, author=ctx.author, content=rF))
await ctx.send("OK")
except asyncio.TimeoutError:
await ctx.send("Too Late!")
You don't want to bundle that into one command.您不想将其捆绑到一个命令中。 Also
time.sleep
stops the entire program, asyncio.sleep
suspends the currently running coroutine. time.sleep
也会停止整个程序, asyncio.sleep
暂停当前正在运行的协程。
import asyncio
import discord
from discord.ext import commands
import time
import random
chocolate_users = {}
@commands.command(aliases=["RandomChocolate", "choco"])
@commands.cooldown(1, 15, commands.BucketType.user)
async def chocolate(self, ctx):
food = ["hershey", "kitkat", "milk"]
rF = random.choice(food)
rFC = rF[:1]
rFL = rF[-1]
await ctx.send(f"**Hint:** It starts with **{rFC}** and ends with **{rFL}**, you have 15 seconds to answer by the way.")
chocolate_users[ctx.message.author.id] = [rF, time.time()+15]
@client.event()
async def on_message(message):
if message.author.id in chocolate_users.keys(): # check if the user started a guess, otherwise do nothing
data = chocolate_users[message.author.id]
if time.time() > data[1]:
await message.channel.send('You exceeded the 15 second time limit, sorry.')
del chocolate_users[message.author.id]
elif message.content.lower() != data[0]:
await message.channel.send('Sorry, that is wrong. Please try again.')
else:
await message.channel.send('Great job, that is correct!')
## other stuff to happen when you get it right ##
del chocolate_users[message.author.id]
The only downside to this is that it waits until you send a message before telling you that you passed the time.唯一的缺点是它会等到您发送消息后才告诉您您已经过了时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.