【发布时间】:2021-06-30 14:22:38
【问题描述】:
我希望当用户使用命令时禁用 {command_name} 。该命令变得可行,我不知道该怎么做,请帮帮我。 请分享一个例子
@bot.command()
async def hello(ctx):
await ctx.send("hi")
如果我想禁用 hello 命令,那么......
【问题讨论】:
标签: python discord.py
我希望当用户使用命令时禁用 {command_name} 。该命令变得可行,我不知道该怎么做,请帮帮我。 请分享一个例子
@bot.command()
async def hello(ctx):
await ctx.send("hi")
如果我想禁用 hello 命令,那么......
【问题讨论】:
标签: python discord.py
您可以使用command.update 禁用命令。您可以通过 command.update(enable=False) 禁用它。进一步的解释将在下面提供的代码中。
请注意:下次您提出这样的问题时,提供您事先尝试过的内容,大多数人不会通过提供完整代码来取悦您。
@bot.command()
async def disable(ctx, command_name=None):
if command_name == None:
# comes here if no command is given
await ctx.send("Please give me a command you want to disable!")
return
try:
# the bot will try to get the command
command = bot.get_command(command_name)
# then the bot will try to disable it
command.update(enabled=False)
# command.update(enabled=True) allows you to enable the command
except:
# this gets sent if the bot can't: a) get the command or b) disable the command
await ctx.send("That isn't a valid command!")
return
# finally, if the command gets successfully disabled, it sends this message
await ctx.send(f"I have disabled the command {command_name}")
(PS:最后一条错误消息是我的机器人预先编程的,你可能想要一个错误处理程序)
【讨论】: