【问题标题】:I wanna make a command in a command in nextcord我想在 nextcord 的命令中发出命令
【发布时间】:2021-11-16 11:42:54
【问题描述】:

好的,所以我想使用 python 在 nextcord 中创建一个子命令

会是这样的:

我:.问题

机器人:1 米等于多少厘米?

我:100 厘米

机器人:正确!

这是我目前的代码...

from nextcord.ext import commands



class Fun(commands.Cog, name="Fun Cog"):
    
    def __init__(self, bot:commands.Bot):
        self.bot = bot


    @commands.command(aliases = ["q/a"])
    async def question(self, ctx: commands.Context):
        await ctx.send("How many centimeters is in 1 meter?")


def setup(bot: commands.Bot):
    bot.add_cog(Fun(bot))

有什么想法吗?

【问题讨论】:

  • 如果我能猜出你想在这里问什么,你可能想向机器人添加一个状态变量,让它知道它正在等待答案,而不是任何一般输入。

标签: python question-answering nextcord


【解决方案1】:

你可以使用wait_for()函数来做。

import asyncio

@commands.command(aliases = ["q/a"])
    async def question(self, ctx: commands.Context):
        await ctx.send("How many centimeters is in 1 meter?")  # your question
        try:
            message = await self.bot.wait_for('message', check=lambda m: m.author == ctx.author and m.channel == ctx.channel, timeout=25)  # you can specify timeout here
            await ctx.send("Correct!" if message.content == "100 cm" else "Incorrect!")
            # `message.content` is the answer
        except asyncio.TimeoutError:
            pass  # if time limit exceeded
            

您还可以使用问题和答案字典创建更复杂的系统。我认为实现起来并不难。

【讨论】:

  • 嗯,我会试试的。另外,我打算使用字典,但我不知道如何制作答案部分,所以谢谢!顺便说一句,我必须写 self.bot 还是 self.client 因为我用客户端来制作我的机器人。
  • 那你应该写self.bot
猜你喜欢
  • 2022-07-16
  • 2022-01-04
  • 2022-11-10
  • 2022-08-10
  • 2022-12-26
  • 2022-08-10
  • 2022-10-04
  • 1970-01-01
  • 2022-10-13
相关资源
最近更新 更多