状态/活动集命令



所以,我想知道是否可以编写一个命令来允许我设置机器人的状态和活动(例如~~设置状态空闲或~~设置活动监视"人们键入~~help"(或类似的东西。

不相关的问题:如何设置仅供我使用的命令?

我还没有找到任何示例代码,而且我是初学者。

您可以使用

is_owner检查来确保您是唯一可以调用命令的人。

若要更改机器人的状态或状态,请使用change_presence方法:

from discord.ext.commands import Bot, is_owner
from discord import Status, Activity, ActivityType
bot = Bot("~~")
def getEnum(enum):
    def getter(arg):
        return enum[arg]
    return getter
@bot.group(invoke_without_command=True)
@is_owner()
async def set(ctx):
    await ctx.send("You must provide a subcommand.")
@set.command()
async def presence(ctx, status: getEnum(Status)):
    await bot.change_presence(status=status)
@set.command(invoke_without_command=True)
async def activity(ctx, type: getEnum(ActivityType), *, description):
    await bot.change_presence(activity=Activity(type=type, name=description))
@set.error
async def set_error(ctx, error):
    if isinstance(error, BadArgument):
        await ctx.send(error.message)
        await ctx.send(error.args)
bot.run("token")

如果您尝试向 StatusActivityType提供无法识别的名称,上述操作将静默失败,您也可以尝试编写错误处理程序以提供一些反馈。

最新更新