2 Discord Bots with Py- Different commands?

I’m trying to run 2 discord bots from one py with different commands… I honestly don’t know if it’s possible but I’ve made it to the point of getting them both running so I was wondering if anyone could help to as why they can’t use different commands?

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

client = commands.Bot(command_prefix='!', intents=discord.Intents.all())
client2 = commands.Bot(command_prefix='.', intents=discord.Intents.all())

async def on_ready():
  # Slash Commands
  
  try:
    synced = await client.tree.sync()
    print("Bot1 is Live!")
    print(f"Synced {len(synced)} command(s)")
  except Exception as e:
    print(e)

  try:
    synced = await client2.tree.sync()
    print("Bot2 is Live!")
    print(f"Synced {len(synced)} command(s)")
  except Exception as e:
    print(e)
    
@client.tree.command(name="Bot1")
async def sayhibot1(interaction: discord.Interaction):
  await interaction.response.send_message(f'Hi {interaction.user.mention}! I am bot2', ephemeral=True)

@client2.tree.command(name="Bot2")
async def sayhibot2(interaction: discord.Interaction):
  await interaction.response.send_message(f'Hi {interaction.user.mention}! I am bot 1', ephemeral=True)

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.create_task(client.start(os.environ['Bot1_TOKEN']))
loop.create_task(client2.start(os.environ['Bot2_TOKEN']))
client.event(on_ready)
client2.event(on_ready)
try:
  loop.run_forever()
finally:
  loop.stop()

Your code is mostly right, you already set up separate command trees for each bot, you just need some improvements but the logic is alright

import os
import discord
from discord.ext import commands
import asyncio

intents = discord.Intents.all()

# You already use different prefix for each bot
client1 = commands.Bot(command_prefix='!', intents=intents)
client2 = commands.Bot(command_prefix='.', intents=intents)

# It's better if you create an on_ready event for each bot (so you don't get lost)
@client1.event
async def on_ready():
    print("Bot1 is Live!")
    try:
        synced = await client1.tree.sync()
        print(f"Bot1 synced {len(synced)} command(s)")
    except Exception as e:
        print(e)

@client1.tree.command(name="sayhi1")
async def sayhi_bot1(interaction: discord.Interaction):
    await interaction.response.send_message(f'Hi {interaction.user.mention}! I am bot1')

# Bot 2
@client2.event
async def on_ready():
    print("Bot2 is Live!")
    try:
        synced = await client2.tree.sync()
        print(f"Bot2 synced {len(synced)} command(s)")
    except Exception as e:
        print(e)

@client2.tree.command(name="sayhi2")
async def sayhi_bot2(interaction: discord.Interaction):
    await interaction.response.send_message(f'Hi {interaction.user.mention}! I am bot2')

# The logic for you to run both bots
loop = asyncio.get_event_loop()
loop.create_task(client1.start(os.environ['Bot1_TOKEN']))
loop.create_task(client2.start(os.environ['Bot2_TOKEN']))
try:
    loop.run_forever()
except KeyboardInterrupt:
    loop.stop()
finally:
    loop.close()
2 Likes

Haha thank you for saying that, I was honestly expecting someone to say what the hell are you doing :joy:

Thanks for the help!

1 Like