我使用 dslash 和 nextcord 制作了一个简单的斜杠命令:
import nextcord
import dslash
client = dslash.CommandClient()
@client.event
async def on_ready():
print(f'Running')
class Options(dslash.Choices):
trouble = 'trouble'
foil = 'foil'
@client.command()
async def rps(interaction: nextcord.Interaction, choice: Options):
if choice == Option.foil:
await interaction.response.send_message("You were foiled")
else:
await interaction.response.send_message(f"You picked {choice}.")
client.run(token.fetch())
运行良好,但是现在我已经更改了该函数的名称,并且我不希望
/rps
不再可以从我的机器人中使用。
如何删除?我检查了服务器设置,但找不到任何内容,所以我认为应该使用 dslash 来完成?
当您启动机器人时,dslash 应该自动清除机器人中不再在您的代码中注册的任何命令 - 因此,如果您更改函数的名称,正如您所说,下次您应该更新命令的名称运行机器人。
但是,Discord 在客户端中缓存全局命令长达一个小时,这意味着您可能需要等待那么长时间才能更新命令。为了在开发过程中解决这个问题,Discord 建议使用服务器特定的命令,这些命令不会被缓存。您可以通过向客户端添加
guild_id
参数来快速使所有命令在 dslash 中特定于服务器,例如:
client = dslash.CommandClient(guild_id=640606716442050605)
一个解决方案(我使用过)是踢掉机器人并邀请它回来,然后它就清除了。
应该有一个 api 方法(例如在 Discord.js 中,你可以只提供一个空列表),但我还没有找到。通过文档、github 或其他方式,所以现在我相信这是唯一的解决方案。
我使用了在另一个网站上找到的解决方案,您可以在机器人运行时在不和谐聊天中键入
!clear
来清除未使用的命令。
这是代码:
@bot.command(name='deletecommands', aliases=['clear'])
@commands.has_any_role('Owner')
async def delete_commands(ctx):
bot.tree.clear_commands(guild=None)
await bot.tree.sync()
await ctx.send('Commands deleted.')
请注意,您必须具有
Owner
角色,将前缀设置为 !
并将其放入 on_ready()
事件中才能使此代码正常工作。
这里是来自 Discord.js Reddit 的原始帖子,其中发布了 Python 解决方案:https://www.reddit.com/r/Discordjs/comments/12cx5yq/how_to_remove_slash_commands/
如果有人需要删除公会内未同步的队伍,那么删除代码如下:
import requests
from os import getenv
application = APPLICATION_ID # your bot's application id
guild = GUILD_ID # your guild's ID for deleting commands
headers = {"Authorization": f"Bot {getenv('BOTTOKEN')}"}
# bot token ^^^^^^^^^^^^^^^^^^^^
# MAIN INFO PAGE: https://discord.com/developers/docs/interactions/application-commands#updating-and-deleting-a-command
while True:
cmd = input("[global|guild] ")
if "global" in cmd:
cmd = input("[get|delete] ")
if "get" in cmd:
r = requests.get(f"https://discord.com/api/v10//applications/{application}/commands", headers=headers)
print(r) # DEBUG
print(r.text, "\n") # DEBUG
elif "delete" in cmd:
r = requests.delete(f"https://discord.com/api/v10//applications/{application}/commands/{input('command_id ')}", headers=headers)
print(r)
print(r.text, "\n")
elif "guild" in cmd:
cmd = input("[get|delete] ")
if "get" in cmd:
r = requests.get(f"https://discord.com/api/v10//applications/{application}/guilds/{guild}/commands", headers=headers)
print(r)
print(r.text, "\n")
elif "delete" in cmd:
r = requests.delete(f"https://discord.com/api/v10//applications/{application}/guilds/{guild}/commands/{input('command_id ')}", headers=headers)
print(r)
print(r.text, "\n")
您可以沿着路径找到命令ID:服务器设置 > 集成 > [机器人名称]管理。或者使用上面的代码(输入 “公会”然后“获取”)