首页 > 解决方案 > discord.py 中的条件命令

问题描述

我希望用户能够通过例如说 !ask [___] 之类的话来了解某事。我会有一份它可能会问的事情的清单。具体来说,特定音阶中的音符。所以他们会问 !scale Cmajor 并得到“C 大调中的音符是 C、D、E、F、G、A、B”的响应。我是一个绝对的初学者,不知道如何做到这一点。我以前做过一个机器人,但这就是我所有的 python 经验。

标签: pythonbotsdiscorddiscord.py

解决方案


您可以从 discord.py文档中查看如何执行此类任务

具体来说,假设您使用的是最新版本的 discord.py 并遵循适当的设置,这样的事情可能会起作用:

# This is assuming your prefix is already defined, and you have a general bot setup
@bot.command()
async def scale(ctx, arg): # assuming the prefix is !, the command will be !scale argument
    if arg.lower() == "cmajor":
        await ctx.send("The notes in C major are C,D,E,F,G,A,B.")
    else:
        await ctx.send("Invalid scale.") # add more scales as needed

推荐阅读