Abijah
Abijah

Reputation: 546

Combining commands and events

I am building a discord bot that I want to be able to interact both with commands and emojis. Here is the code so far:

import os
import discord
from dotenv import load_dotenv
from discord.ext import commands

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')

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

client = discord.Client()

@bot.command(name='repeat', help='help me to understand bots')
async def test(ctx, *args):
    for arg in args:
        await ctx.send(arg)


@client.event
async def on_raw_reaction_add(payload):
    print("someone reacted to something")
    print(payload)


bot.run(TOKEN)
client.run(TOKEN)

My intuition is that the last two lines are where this goes wrong, although I don't understand the run(TOKEN) feature to have a good understanding of why this happens, or how to fix it.

Upvotes: 1

Views: 1199

Answers (1)

Tristan Hanks
Tristan Hanks

Reputation: 46

To further develop this you need to fully understand what you've done and have a plan for what you want to do. I will first direct you to the discord python documentation Where you can see everything discord.py has to offer. This should probably be your first stop when you have questions.

I see you have import discord and from discord.ext import commands... discord.py has two ways to approach a bot, which you have:

client = discord.Client() hails from import discord, and bot = commands.Bot() hails from import discord.ext.

You need to choose whether to use discord.Client() or discord.Bot() because the use cases are entirely different. I suggest the latter, as it will allow your bot to do more, in a cleaner fashion.

Your next step would be to change any @ decorators to whichever variable you choose and keep that consistent throughout your code. Then, you need to remove whichever .run(TOKEN) you won't use. So, if I were you, I would read the documentation which I linked to you so you understand more than a YouTube tutorial would teach.

As for a revision to your code, it would look like this:

import os
import discord
from dotenv import load_dotenv
from discord.ext import commands

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')

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

@bot.command(name='repeat', help='help me to understand bots')
async def test(ctx, *args):
    for arg in args:
        await ctx.send(arg)

@bot.event
async def on_raw_reaction_add(payload):
    print("someone reacted to something")
    print(payload)

bot.run(TOKEN)

Upvotes: 2

Related Questions