简体   繁体   English

我正在尝试让用户回复机器人响应,然后发送另一条消息

[英]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.

相关问题 我试图让我的 discord 机器人等待用户在执行原始命令后发送另一条消息 - I'm trying to get my discord bot to wait for the user to send another message after they did the original command 如何从机器人向另一个用户发送消息 - How to send message to another user from bot 如何让机器人回复音频消息? - How can I make bot reply to an audio message? Discord Bot Python。我试图让我的机器人看到来自用户的命令,然后获取下一条消息内容(永远是机器人) - Discord Bot Python. I'm trying to get my bot to see a command from a user, then take the next message content (will always be a bot) 我正在尝试创建一个书签机器人,向对表情符号做出反应的用户发送消息 - I'm trying to create a bookmark bot that DMs a message to the user who reacts with an emoji 我试图让机器人从 700 行的实际 txt 文件中发送 10 行文本 - I'm trying to make a bot send 10 lines of text from an actual txt file of 700 lines 如果消息是回复,bot 应该获取用户的 id - If the message is a reply, bot should get id of a user 从用户/机器人获取回复消息 - Get reply message from user/bot 如何使漫游器发送多个命令以得到1个命令 - How to make bot send multiple reply for 1 commmand 我无法从Microsoft Teams bot向另一个用户发送消息 - I can't send a message to another user from Microsoft Teams bot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM