我正在开发一个具有某些特定功能的不和谐机器人。其中一些有一个命令 /aviso 和 /initiar 并且它们正在工作,或者它们正在... 我对 /aviso 命令进行了修改,现在日志中出现同步错误,我不知道如何修复它,我已经输入了几个同步命令,例如:
@bot.event
async def on_ready():
try:
await bot.tree.sync()
print("Comandos sincronizados com sucesso.")
except Exception as e:
print(f"Erro ao sincronizar comandos: {e}")
但是日志中仍然出现这个错误:
2024-10-14 12:22:43 ERROR discord.app_commands.tree Ignoring exception in command 'aviso'
Traceback (most recent call last):
File "C:\Users\Rezend3\Desktop\VILAO\.venv\Lib\site-packages\discord\app_commands\tree.py", line 1310, in _call
await command._invoke_with_namespace(interaction, namespace)
File "C:\Users\Rezend3\Desktop\VILAO\.venv\Lib\site-packages\discord\app_commands\commands.py", line 882, in _invoke_with_namespace
transformed_values = await self._transform_arguments(interaction, namespace)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Rezend3\Desktop\VILAO\.venv\Lib\site-packages\discord\app_commands\commands.py", line 846, in _transform_arguments
raise CommandSignatureMismatch(self) from None
discord.app_commands.errors.CommandSignatureMismatch: The signature for command 'aviso' is different from the one provided by Discord. This can happen because either your code is out of date or you have not synced the commands with Discord, causing the mismatch in data. It is recommended to
sync the command tree to fix this issue.
任何人都可以告诉我如何解决这个问题吗??? (Python代码)
我已经尝试同步机器人的命令以使该命令起作用,但没有任何作用。我已经更新了discord.py库,例如,如果我添加另一个命令,我无法将其识别为交互命令。我不知道该怎么办了。
另外,我尝试使用通道 ID 进行手动同步:
@bot.event
async def on_ready():
# Syncs only for the specified guild
guild = discord.Object(id=1285939558738825306)
await bot.tree.sync(guild=guild)
print(f'Logged in as {bot.user}!')
我也尝试了其他命令来尝试删除旧命令并添加新命令,但没有成功:
@bot.event
async def on_ready():
# Remove the command if it already exists
bot.tree.remove_command('aviso', type=discord.AppCommandType.chat_input)
# Synchronizes tree commands
await bot.tree.sync()
print(f'Logged in as {bot.user}!')
完整命令:
import asyncio
import discord
from discord.ext import commands
from discord.ui import Button, View
# Configurações de intents
intents = discord.Intents.default()
intents.members = True
intents.messages = True
intents.guilds = True
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)
@bot.event
async def on_ready():
# Sincroniza apenas para o guild especificado
# Substitua pelo seu guild ID
guild = discord.Object(id=1285939558738825306)
await bot.tree.sync(guild=guild)
print(f'Logged in as {bot.user}!')
@bot.tree.command(name='aviso', description='Envia um aviso para o canal específico.')
@discord.app_commands.describe(mensagem='A mensagem que será enviada.')
async def aviso(interaction: discord.Interaction, mensagem: str):
# ID do canal onde o comando pode ser usado
canal_permitido_id = 1290828710844305481
# Verifica se o comando foi usado no canal permitido
if interaction.channel.id != canal_permitido_id:
await interaction.response.send_message('Este comando só pode ser usado no canal específico da staff.', ephemeral=True)
return
# Pergunte ao executor do comando qual canal deve receber a mensagem
canal_id = interaction.data.get('options')[0]['value'] # Altere para coletar o ID do canal
canal = bot.get_channel(canal_id)
# Mensagens de depuração
print(f'Canal permitido: {canal_permitido_id}, Canal atual: {interaction.channel.id}')
if canal is not None:
# Criando um embed para a mensagem
embed = discord.Embed(
title="📢 Aviso",
description=mensagem,
color=discord.Color.red(), # Você pode mudar a cor aqui
)
embed.set_footer(text="Equipe Vila Brasil, Community.")
await canal.send(embed=embed) # Enviar a mensagem embed
await interaction.response.send_message('Aviso enviado com sucesso!', ephemeral=True)
else:
print('Canal não encontrado.')
await interaction.response.send_message('Canal não encontrado.', ephemeral=True)
#rest of the code
首先了解应用命令(斜杠命令)可以定义在两个不同的作用域中:
请注意,您的
/aviso
命令是全局命令,当您使用
guild = discord.Object(id=1285939558738825306)
await bot.tree.sync(guild=guild)
您只是同步来自公会的命令
1285939558738825306
。
如果要同步全局命令,则必须为 guild 参数指定 None
:
@bot.event
async def on_ready():
# Sincroniza apenas para o guild especificado
# Substitua pelo seu guild ID
guild = discord.Object(id=1285939558738825306)
await bot.tree.sync(guild=guild)
# Sincroniza os comandos globais
await bot.tree.sync(guild=None)
print(f'Logged in as {bot.user}!')