簡體   English   中英

我正在嘗試讓用戶回復機器人響應,然后發送另一條消息

[英]I'm trying to make a user reply to a bot response then send another message

我怎樣才能讓機器人再次響應? 我試圖讓機器人讓用戶響應正在發送的消息,然后回復說是否為時已晚或他們做到了。

  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!")

您可以使用await bot.wait_for('message')來等待消息。 通過傳遞一個check參數,我們還可以指定我們正在等待的消息的詳細信息。 我正在重用來自其他答案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!")

您不想將其捆綁到一個命令中。 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]

唯一的缺點是它會等到您發送消息后才告訴您您已經過了時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM