Search code examples
python-3.xdiscord.py

The bot's slash commands are not synchronized locally


There is the following code:

bot.py

import discord
from discord.ext import commands
import logging
import os
import asyncio
from config import settings

discord.utils.setup_logging(level=logging.INFO,root=True)
bot = commands.Bot(command_prefix = settings['prefix'],intents=discord.Intents.all()) # Так как мы указали префикс в settings, обращаемся к словарю с ключом prefix.
@bot.event
async def on_ready():
    synced = await bot.tree.sync(guild=discord.Object(id=1283589491298402314))
    print(f'Синхронизированно {len(synced)} команд!')
    print(bot.guilds)

async def load():
    for filename in os.listdir("./cogs"):
        if filename.endswith(".py"):
            if filename != '__init__.py':
                await bot.load_extension(f"cogs.{filename[:-3]}")

async def main():
    await load()
    await bot.start(settings['token'])


asyncio.run(main())

utuls.py

import discord
from discord.ext import commands
from discord import app_commands

class UtilsCommand(commands.Cog):
def __init__(self,bot:discord.Client):
self.bot=bot

    @app_commands.command(name='ping',description='Check ping')
    async def ping(self,interaction:discord.Interaction):
        await interaction.response.send_message(content=f"Pong! ({self.bot.latency})")

async def setup(bot):
await bot.add_cog(UtilsCommand(bot))

When deleting the fragment "guild=discord.Object(id=1283589491298402314)" the commands are synchronized globally, but I don't need this since I want to synchronize them locally.

A message appears in the console with information that 0 commands are synchronized. I can't find the syntax error, so I'm asking for help.

I was looking for an answer among similar articles, everywhere the code coincided with mine to achieve the goal, but for some reason it doesn’t work for me :/. There isn't even an error to...


Solution

  • You have to pass the guild also to the Cog:

    async def setup(bot):
        await bot.add_cog(UtilsCommand(bot), guild=discord.Object(id=1283589491298402314))