首页 > 解决方案 > 我想在 nextcord 的命令中发出命令

问题描述

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

它会是这样的:

我:.问题

Bot: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))

有任何想法吗?

标签: pythonquestion-answeringnextcord

解决方案


您可以使用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
            

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


推荐阅读