Main.py

mail@pastecode.io avatar
unknown
python
a month ago
1.9 kB
7
Indexable
Never
import discord
from discord.ext import commands
from config import TEMP_VOICE_CHANNELS, TEMP_ROLE_ID  

# DEFS
TOKEN = ''
GUILD_ID = 
WATCH_CHANNEL_ID = 

intents = discord.Intents.default()
intents.message_content = True
intents.voice_states = True
intents.guilds = True


class MyBot(commands.Bot):
    def __init__(self):
        super().__init__(command_prefix="!", intents=intents)
        self.synced = False

    async def on_ready(self):
        print(f'{self.user} has connected to Discord!')

        # Sync / cmds
        if not self.synced:
            guild = discord.Object(id=GUILD_ID)
            self.tree.copy_global_to(guild=guild)
            commands = await self.tree.sync(guild=guild)
            print(f'Synced {len(commands)} commands with Discord for the guild.')
            self.synced = True

        # Test / cmds for main.py
        @self.tree.command(name='test', description="Test command from main.py")
        async def test_command(interaction: discord.Interaction):
            await interaction.response.send_message("Test worked from main.py!")

        #Manual run
        guild = self.get_guild(GUILD_ID)
        if guild:
            try:
                synced_commands = await self.tree.sync(guild=guild)
                print(f'Manually synced {len(synced_commands)} commands with Discord for the guild {guild.name}.')
            except Exception as e:
                print(f'Error syncing commands for {guild.name}: {e}')

bot = MyBot()

@bot.command()
async def sync(ctx):
    try:
        synced_commands = await bot.tree.sync(guild=ctx.guild)
        await ctx.send(f'Synced {len(synced_commands)} commands.')
    except Exception as e:
        await ctx.send(f'Failed to sync commands: {e}')
        print(f'Failed to sync commands: {e}')

bot.run(TOKEN)
Leave a Comment