作为我的上一问题的扩展,我现在在同步混合命令时遇到问题。我的代码如下:
import discord
import os
import sys
import subprocess
import random
from dotenv import load_dotenv
from discord.ext import commands
from discord import app_commands
from typing import Literal, Optional
from discord.ext.commands import Greedy, Context
intents = discord.Intents.default()
bot = commands.Bot(command_prefix='$', intents=intents)
intents.message_content = True
load_dotenv()
bot.help_command=None
..........
#Startup confirmation message
@bot.event
async def on_ready():
print(f'{bot.user} has connected to Discord! Version {discord.__version__}')
@bot.command()
@commands.guild_only()
@commands.is_owner()
async def sync(
ctx: Context, guilds: Greedy[discord.Object], spec: Optional[Literal["~", "*", "^"]] = None) -> None:
if not guilds:
if spec == "~":
synced = await ctx.bot.tree.sync(guild=ctx.guild)
elif spec == "*":
ctx.bot.tree.copy_global_to(guild=ctx.guild)
synced = await ctx.bot.tree.sync(guild=ctx.guild)
elif spec == "^":
ctx.bot.tree.clear_commands(guild=ctx.guild)
await ctx.bot.tree.sync(guild=ctx.guild)
synced = []
else:
synced = await ctx.bot.tree.sync()
await ctx.reply(
f"Synced {len(synced)} commands {'globally' if spec is None else 'to the current guild.'}"
)
return
ret = 0
for guild in guilds:
try:
await ctx.bot.tree.sync(guild=guild)
except discord.HTTPException:
pass
else:
ret += 1
await ctx.reply(f"Synced the tree to {ret}/{len(guilds)}.")
@bot.hybrid_command(name='help', with_app_command=True)
async def help2(ctx: commands.Context):
await ctx.send("Test was good!")
@bot.tree.command()
async def test1(interaction: discord.Interaction) -> None:
await interaction.response.send_message("test successful")
.................
bot.run(os.getenv("token_dev"))
当我执行
$sync
命令时,它将更新 test2
@bot.tree.command()
将同步,然后根据我对其所做的任何更改正常运行,但我的混合命令不会。由于某种原因,我还有一个 额外的命令? 我认为它是我在修改不同的 API 时留下的,但我不知道如何删除它。
当我执行
$sync
时,它会发送消息 Synced 1 commands globally
,并且我的命令 test2
将与我对其所做的任何更改同步并正常运行。然而,我的混合命令不会。我似乎还有一个额外的命令,无论我同步多少次,它都不会消失,而且我不知道如何删除它。如果我尝试运行它,我会得到:
2023-10-11 17:45:35 ERROR discord.app_commands.tree Ignoring exception in command tree
Traceback (most recent call last):
File "C:\Users\malak\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\app_commands\tree.py", line 1089, in wrapper
await self._call(interaction)
File "C:\Users\malak\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\app_commands\tree.py", line 1221, in _call
command, options = self._get_app_command_options(data)
File "C:\Users\malak\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\discord\app_commands\tree.py", line 1125, in _get_app_command_options
raise CommandNotFound(name, parents)
discord.app_commands.errors.CommandNotFound: Application command 'test' not found
如果我尝试使用
$test1
运行混合命令,它会出现“找不到命令”错误。 /test1
什么也不做。在调试模式下运行时不会出现错误。
编辑:我将
@commands.hybrid_command(...)
更改为 @bot.hybrid_command(...)
,现在在执行 $sync
时,它会说它已同步两个命令,并且如果我执行 $test1
则可以正常工作,但斜线仍然没有任何效果。
我还应该在执行
/test
时添加它会自动完成,但是当我发送它时应用程序没有响应。
编辑2:好的,所以我忘记了命令同步可能需要一段时间,所以现在
test1
出现在我的斜杠命令列表中,但我不想的正常 test
也是如此在那里。
@bot.event
async def on_ready():
print(f'{bot.user} has connected to Discord! Version {discord.__version__}')
try:
synced = await bot.tree.sync()
print(f"Synced {len(synced)} commands.")
except Exception as e:
print(e)
当您运行机器人时,这将在每个
bot.tree.command()
同步。
显然它被缓存在 Python 文件中的某个地方,所以我所要做的就是重新安装 Python 并再次同步命令。