python - 我想在 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))
有任何想法吗?
解决方案
您可以使用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
您还可以使用问题和答案字典创建更复杂的系统。我认为实施起来并不难。
推荐阅读
- r - 为什么数字没有映射到每一行?
- xamarin - Xamarin 形成 AppIcon.xcassets 导致上传错误。
- python - Keras ValueError:应在输入列表上调用合并层
- python - 如何通过多列的功能对 Pandas 行进行分组
- java - 消息:PrimeFaces 扩展 DocumentViewer 中缺少 PDF
- apache-spark - spark数据帧写入HDFS的文件名
- php - CI_session 未保存在 Mozilla Firefox 中
- android - 在没有 Android Studio 的情况下从 Android 源代码生成签名的 APK
- python-3.x - 在 Python 中枚举和打印行。
- c# - System.ObjectDisposedException:“无法写入已关闭的 TextWriter。” 设置输出错误