0

I'm developing a bot for a discord with some specific features. In some of them there is a command that is /aviso and /initiar and they are working, or they were... I made a modification to the /aviso command and now a synchronization error appears in the log and I don't know what to do to fix it, I have already entered several synchronization commands such as:

@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}") 

But this error still appears in the log:

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.

Can anyone give me some light on what to do to solve this problem?

I've already tried synchronizing the bot's commands so that the command works, but nothing works. I've already updated the discord.py library, if I add another command, for example, I can't get it to be recognized as an interaction command. I don't know what to do anymore.

Also, I tried to do manual synchronization using the channel 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}!')

I tried this other command as well to try to remove the old command and add the new one, but without success:

@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}!')

complete command:

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

2
  • Enter all the code relevant to your question, especially the part where you define your /aviso command. Commented Oct 14, 2024 at 23:09
  • I added the full /aviso command which is not working Commented Oct 14, 2024 at 23:47

1 Answer 1

0

First understand that application commands (slash commands) can be defined in two different scopes:

  1. Guild command: these are commands exclusive to a specific guild, which was informed at the time you defined the command;
  2. Global command: these commands are available on all guilds. When defined, you did not enter a guild.

Note that your /aviso command is a global command and when you use

guild = discord.Object(id=1285939558738825306) 
await bot.tree.sync(guild=guild)

you are only synchronizing commands from guild 1285939558738825306. If you want to synchronize global commands, you must specify None for the guild argument:

@bot.event
async def on_ready():
    # Sync your guild commands
    guild = discord.Object(id=1285939558738825306)
    await bot.tree.sync(guild=guild)

    # Sync global commands
    await bot.tree.sync(guild=None)

    print(f'Logged in as {bot.user}!')
Sign up to request clarification or add additional context in comments.

3 Comments

If you only want the command to work only on your guild, you'll have to define it as a Guild command. To make it work only on one channel of your guild, you have to go to the guild > integration settings, search for your bot and set the command to work only on the desired channel.
Even placing guild=none an error message still appears and the command does not work as it should, only the part for placing the message appears, it does not appear to choose the desired chat to send the message to.
If you want the user to enter the channel, create a new parameter channel: discord.TextChannel for the channel, just as you did for message. So, the function of your command would be something like async def warning(interaction: discord.Interaction, channel: discord.TextChannel, message: str)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.