Oblique
Oblique

Reputation: 560

Discord bot discord.py commands aren't working

I'm fairly new to making discord bots, and I'm trying to make a basic test command, where you say (prefix)test abc, and the bot says abc too. I get no errors, but when I type r!test abc, nothing happens. I get nothing in the terminal, either.

Here is my code.

import discord

from dotenv import load_dotenv
from discord.ext import commands

load_dotenv()


GUILD = os.getenv('DISCORD_GUILD') # Same thing but gets the server name
client = discord.Client()
bot = commands.Bot(command_prefix='r!')
TOKEN = 'TOKENGOESHERE'
on = "I'm up and running!"
print("Booting up...")
channel = client.get_channel(703869904264101969)

@client.event # idk what this means but you have to do it

async def on_ready(): # when the bot is ready

    for guild in client.guilds:
        if guild.name == GUILD:
            break

    print(f'{client.user} has connected to Discord! They have connected to the following server: ' #client.user is just the bot name
    f'{guild.name}(id: {guild.id})' # guild.name is just the server name


    )

    channel2 = client.get_channel(channelidishere)
    await channel2.send(on)
#everything works up until I get to here, when I run the command, nothing happens, not even some output in the terminal.

@commands.command()
async def test(ctx):
    await ctx.send("test")



client.run(TOKEN)

Thanks everyone!

Upvotes: 1

Views: 2075

Answers (1)

DaveStSomeWhere
DaveStSomeWhere

Reputation: 2540

The key issue is that you are not defining your decorators correctly. Since you are using commands you only need the bot = commands.Bot(command_prefix='r!') statement. The client = discord.Client() is not needed. Since it is bot = ..., all your decorators need to start with @bot. Also, your won't use client , you'll use bot like channel2 = bot.get_channel(channelidishere).

Removed the guild loop and replaced it with discord.utils.get to get the guild - guild = discord.utils.get(bot.guilds, name=GUILD). This requires another import - import discord

Try 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')  # Same thing but gets the server name
bot = commands.Bot(command_prefix='r!')
on = "I'm up and running!"
print("Booting up...")


@bot.event  # idk what this means but you have to do it
async def on_ready():  # when the bot is ready
    guild = discord.utils.get(bot.guilds, name=GUILD)
    print(
        f'{bot.user} has connected to Discord! They have connected to the following server: '  # client.user is just the bot name
        f'{guild.name}(id: {guild.id})')  # guild.name is just the server name
    channel2 = bot.get_channel(channelidishere)
    await channel2.send(on)


@bot.command()
async def test(ctx):
    await ctx.send("test")


bot.run(TOKEN)

Upvotes: 1

Related Questions