Discord.py Slash commands in cogs not syncing

I am making a discord.py bot that utilizes slash commands aswell as text based commands. I am using cogs to order them properly but the slash commands do not want to sync so I cannot use them.

Folder discord_bot -> main.py, cogs(folder); cogs -> embed.py, etc

This is main.py where I am syncing up the bot and calling in tree.sync stuff.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import os
import discord
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
intents.presences = True
intents.voice_states = True
bot = commands.Bot(command_prefix='$', intents=intents)
async def load_extensions():
for filename in os.listdir("./cogs"):
if filename.endswith(".py"):
cog_name = f"cogs.{filename[:-3]}"
try:
await bot.load_extension(cog_name)
print(f"Loaded extension: {cog_name}")
except Exception as e:
print(f"Failed to load extension {cog_name}: {e}")
@bot.event
async def on_ready():
print(f'We have logged in as {bot.user}')
try:
# Print all commands in the command tree before syncing
print("Commands before sync:")
for command in bot.tree.get_commands():
print(f"- {command.name}")
print("Attempting to sync slash commands...")
synced = await bot.tree.sync()
print(f"Synced {len(synced)} command(s)")
# Print all commands in the command tree after syncing
print("Registered slash commands after sync:")
for command in bot.tree.get_commands():
print(f"- {command.name}")
except Exception as e:
print(f"Error during sync: {e}")
await bot.change_presence(status=discord.Status.dnd, activity=discord.Game('Watching over youn Prefix: $'))
await load_extensions()
try:
token = os.getenv('DISCORD_TOKEN')
if token == "":
raise Exception("Please add your token to the Secrets pane.")
bot.run(token)
except discord.HTTPException as e:
if e.status == 429:
print("The Discord servers denied the connection for making too many requests")
print("Get help from /questions/66724687/in-discord-py-how-to-solve-the-error-for-toomanyrequests")
else:
raise e
</code>
<code>import os import discord from discord.ext import commands from dotenv import load_dotenv load_dotenv() intents = discord.Intents.default() intents.message_content = True intents.members = True intents.presences = True intents.voice_states = True bot = commands.Bot(command_prefix='$', intents=intents) async def load_extensions(): for filename in os.listdir("./cogs"): if filename.endswith(".py"): cog_name = f"cogs.{filename[:-3]}" try: await bot.load_extension(cog_name) print(f"Loaded extension: {cog_name}") except Exception as e: print(f"Failed to load extension {cog_name}: {e}") @bot.event async def on_ready(): print(f'We have logged in as {bot.user}') try: # Print all commands in the command tree before syncing print("Commands before sync:") for command in bot.tree.get_commands(): print(f"- {command.name}") print("Attempting to sync slash commands...") synced = await bot.tree.sync() print(f"Synced {len(synced)} command(s)") # Print all commands in the command tree after syncing print("Registered slash commands after sync:") for command in bot.tree.get_commands(): print(f"- {command.name}") except Exception as e: print(f"Error during sync: {e}") await bot.change_presence(status=discord.Status.dnd, activity=discord.Game('Watching over youn Prefix: $')) await load_extensions() try: token = os.getenv('DISCORD_TOKEN') if token == "": raise Exception("Please add your token to the Secrets pane.") bot.run(token) except discord.HTTPException as e: if e.status == 429: print("The Discord servers denied the connection for making too many requests") print("Get help from /questions/66724687/in-discord-py-how-to-solve-the-error-for-toomanyrequests") else: raise e </code>
import os
import discord
from discord.ext import commands
from dotenv import load_dotenv

load_dotenv()

intents = discord.Intents.default()
intents.message_content = True
intents.members = True
intents.presences = True
intents.voice_states = True

bot = commands.Bot(command_prefix='$', intents=intents)

async def load_extensions():
    for filename in os.listdir("./cogs"):
        if filename.endswith(".py"):
            cog_name = f"cogs.{filename[:-3]}"
            try:
                await bot.load_extension(cog_name)
                print(f"Loaded extension: {cog_name}")
            except Exception as e:
                print(f"Failed to load extension {cog_name}: {e}")

@bot.event
async def on_ready():
    print(f'We have logged in as {bot.user}')
    try:
        # Print all commands in the command tree before syncing
        print("Commands before sync:")
        for command in bot.tree.get_commands():
            print(f"- {command.name}")

        print("Attempting to sync slash commands...")
        synced = await bot.tree.sync()
        print(f"Synced {len(synced)} command(s)")

        # Print all commands in the command tree after syncing
        print("Registered slash commands after sync:")
        for command in bot.tree.get_commands():
            print(f"- {command.name}")

    except Exception as e:
        print(f"Error during sync: {e}")

    await bot.change_presence(status=discord.Status.dnd, activity=discord.Game('Watching over youn Prefix: $'))
    await load_extensions()

try:
    token = os.getenv('DISCORD_TOKEN')
    if token == "":
        raise Exception("Please add your token to the Secrets pane.")
    bot.run(token)
except discord.HTTPException as e:
    if e.status == 429:
        print("The Discord servers denied the connection for making too many requests")
        print("Get help from /questions/66724687/in-discord-py-how-to-solve-the-error-for-toomanyrequests")
    else:
        raise e

This is embed.py. Structure where I am using slash commands.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from discord.ext import commands
import discord
from discord import app_commands
import random
class Embeds(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.Cog.listener()
async def on_ready(self):
#sync slash commands when cogs is loaded
await self.bot.tree.sync()
print(f"Slash commands synced for {self.__class__.__name__}")
@commands.command(help="Send an embed to a channel. Format: $embed [title] [description] [color hex, type random / r to randomize it] [channel]n Note: Use "" to seperate title and description ")
async def embed(self, ctx, title: str, description: str, color: str, channel: discord.TextChannel):
if channel.permissions_for(ctx.author).send_messages:
try:
if color.lower() == "r" or color.lower() == "random":
color_int = discord.Color.from_rgb(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
else:
color_int = discord.Color(int(color, 16))
embed = discord.Embed(title=title, description=description, color=color_int)
send_channel = channel or ctx.channel
await send_channel.send(embed=embed)
await ctx.send(f"Embed sent to {send_channel.mention}.")
except discord.Forbidden:
await ctx.send("Cannot send message in the specified channel. Please check if the bot has desired permissions.")
else:
await ctx.send("You don't have permissions to use this command.")
@app_commands.command(name="embed", description="Embed generator")
@app_commands.describe(
title = "Title",
description = "Description",
color = "Hex color code or 'random' to randomize it",
channel = "Where to send embed"
)
async def embed_slash(self, interaction: discord.interactions, title: str, description: str, color: str, channel: discord.TextChannel):
if color.lower() == "random":
color_int = discord.Color.from_rgb(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
else:
color_int = color
embed = discord.Embed(title=title, description=description, color=color_int)
await channel.send(embed=embed)
await interaction.response.send_message(f"Embed sent successfully in {channel.mention}", ephemeral = True)
async def setup(bot):
await bot.add_cog(Embeds(bot))
</code>
<code>from discord.ext import commands import discord from discord import app_commands import random class Embeds(commands.Cog): def __init__(self, bot): self.bot = bot @commands.Cog.listener() async def on_ready(self): #sync slash commands when cogs is loaded await self.bot.tree.sync() print(f"Slash commands synced for {self.__class__.__name__}") @commands.command(help="Send an embed to a channel. Format: $embed [title] [description] [color hex, type random / r to randomize it] [channel]n Note: Use "" to seperate title and description ") async def embed(self, ctx, title: str, description: str, color: str, channel: discord.TextChannel): if channel.permissions_for(ctx.author).send_messages: try: if color.lower() == "r" or color.lower() == "random": color_int = discord.Color.from_rgb(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) else: color_int = discord.Color(int(color, 16)) embed = discord.Embed(title=title, description=description, color=color_int) send_channel = channel or ctx.channel await send_channel.send(embed=embed) await ctx.send(f"Embed sent to {send_channel.mention}.") except discord.Forbidden: await ctx.send("Cannot send message in the specified channel. Please check if the bot has desired permissions.") else: await ctx.send("You don't have permissions to use this command.") @app_commands.command(name="embed", description="Embed generator") @app_commands.describe( title = "Title", description = "Description", color = "Hex color code or 'random' to randomize it", channel = "Where to send embed" ) async def embed_slash(self, interaction: discord.interactions, title: str, description: str, color: str, channel: discord.TextChannel): if color.lower() == "random": color_int = discord.Color.from_rgb(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) else: color_int = color embed = discord.Embed(title=title, description=description, color=color_int) await channel.send(embed=embed) await interaction.response.send_message(f"Embed sent successfully in {channel.mention}", ephemeral = True) async def setup(bot): await bot.add_cog(Embeds(bot)) </code>
from discord.ext import commands
import discord
from discord import app_commands
import random

class Embeds(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        
    @commands.Cog.listener()
    async def on_ready(self):
        #sync slash commands when cogs is loaded
        await self.bot.tree.sync()
        print(f"Slash commands synced for {self.__class__.__name__}")


    @commands.command(help="Send an embed to a channel. Format: $embed [title] [description] [color hex, type random / r to randomize it] [channel]n Note: Use "" to seperate title and description ")
    async def embed(self, ctx, title: str, description: str, color: str, channel: discord.TextChannel):
        if channel.permissions_for(ctx.author).send_messages:
            try:

                if color.lower() == "r" or color.lower() == "random":
                    color_int = discord.Color.from_rgb(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

                else:
                    color_int = discord.Color(int(color, 16))
                
                embed = discord.Embed(title=title, description=description, color=color_int)

                send_channel = channel or ctx.channel
                await send_channel.send(embed=embed)
                await ctx.send(f"Embed sent to {send_channel.mention}.")
            
            except discord.Forbidden:
                await ctx.send("Cannot send message in the specified channel. Please check if the bot has desired permissions.")
        else:
            await ctx.send("You don't have permissions to use this command.")
            
            
    @app_commands.command(name="embed", description="Embed generator")
    @app_commands.describe(
        title = "Title",
        description = "Description",
        color = "Hex color code or 'random' to randomize it",
        channel = "Where to send embed"
    )
    async def embed_slash(self, interaction: discord.interactions, title: str, description: str, color: str, channel: discord.TextChannel):
        if color.lower() == "random":
            color_int = discord.Color.from_rgb(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        else:
            color_int = color
        
        embed = discord.Embed(title=title, description=description, color=color_int)
        await channel.send(embed=embed)
        await interaction.response.send_message(f"Embed sent successfully in {channel.mention}", ephemeral = True)


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

This is coin_flip.py. Structure where I am NOT using slash commands.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from discord.ext import commands
import random
class coin_flip(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command(name="flip", help="Flip a coin.")
async def flip(self, ctx):
sides = ['heads', 'tails']
output_side = random.choice(sides)
await ctx.send(f"{ctx.author.mention} {output_side}")
async def setup(bot):
await bot.add_cog(coin_flip(bot))
</code>
<code>from discord.ext import commands import random class coin_flip(commands.Cog): def __init__(self, bot): self.bot = bot @commands.command(name="flip", help="Flip a coin.") async def flip(self, ctx): sides = ['heads', 'tails'] output_side = random.choice(sides) await ctx.send(f"{ctx.author.mention} {output_side}") async def setup(bot): await bot.add_cog(coin_flip(bot)) </code>
from discord.ext import commands
import random

class coin_flip(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command(name="flip", help="Flip a coin.")
    async def flip(self, ctx):
        sides = ['heads', 'tails']
        output_side = random.choice(sides)

        await ctx.send(f"{ctx.author.mention} {output_side}")


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

I am using the same structure for where I am using slash commands and same structure for where I am not using slash commands.

Interestingly if I just have text based commands in cogs and the slash commands in main.py then the commands work perfectly fine. But idk why tho.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật