Search code examples
pythonpython-3.xdiscorddiscord.py

Discord.Py - How do I make my slash command work with my existing bot?


I have an existing discord bot that responds to messages in some of my channels. The works well for the purpose I need. I recently attempted to add a slash command but I can't get the two to work together. I can run the slash command by itself without my existing bot but once I add the two, they don't work. Can any one tell me how I can mix these two codes so that they can work together. Any help is greatly appreciated.

This is my current bot

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

intents = discord.Intents.default()
intents.message_content = True
bot = Bot("!", intents=intents)

@bot.event
async def on_ready():
    await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.playing, name="on my SweetCrispy"))
    print("Ready to roll & rock")

@bot.event
async def on_message(message):
    if message.channel.id == channel1:
        if message.author.id == bot.user.id:
            return
        else:
            if not "battle" in message.content:
                await message.delete()
                await message.channel.send("Are you not ready to battle?")
            else:
                message.channel.send("Let's get ready to battle?")
                await msg.add_reaction("🤩")
                

@bot.event
async def on_reaction_add(reaction, user):
    if reaction.message.channel.id == channel1:
        if reaction.emoji == "🤩" and reaction.count == 2:
            await channel1.chat.send((f"{user.mention} is battle ready"))

I would like to be able to add this to my existing bot:

import discord
from discord import app_commands
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio

class aclient(discord.Client):
    def init(self):
        super().init(intents = discord.Intents.default())
        self.synced = False #we use this so the bot doesn't sync commands more than once

    async def on_ready(self):
        await self.wait_until_ready()
        if not self.synced:
            await tree.sync()
            self.synced = True
        print(f"Ready & logged in as {self.user}.")

client = aclient()
tree = discord.app_commands.CommandTree(client)

@tree.command(name = 'Battle', description='Score Lookup')
async def self(interaction: discord.Interaction,name: str):
    await interaction.response.defer(ephemeral = True)
    await asyncio.sleep(1)
    await interaction.followup.send("custom message here")

I tried combining the two but I keep getting error messages.


Solution

  • I assume that you're only using @bot.event in the first code (because that is what you provided). All you have to do is change commands.Bot to discord.Client since @client.event still works and doesn't interfere with the app_commands.CommandTree.

    class aclient(discord.Client):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.synced = False #we use this so the bot doesn't sync commands more than once
    
        async def on_ready(self):
            await self.wait_until_ready()
            if not self.synced:
                await tree.sync()
                self.synced = True
            print(f"Ready & logged in as {self.user}.")
    
    activity = discord.Activity(type=discord.ActivityType.playing, name="on my SweetCrispy")
    intents = discord.Intents.default()
    intents.message_content = True
    client = aclient(activity=activity, intents=intents)
    tree = discord.app_commands.CommandTree(client)
    
    @client.event
    async def on_message(message):
    ...
    
    @client.event
    async def on_reaction_add(reaction, user):
    ...
    
    @tree.command(name = 'Battle', description='Score Lookup')
    async def battlecommand(interaction: discord.Interaction,name: str):
    ...