discord.py ctx commands do not return anything

2.5k Views Asked by At

I'm trying to use bot commands in discord so I can start using the bot prefix. I tried using ctx, but from the code that I have written, when I use .ping, it returns nothing. Anyone got any idea why?

import discord
from discord.ext import commands


client = discord.Client()
bot = commands.Bot(command_prefix= ".")

@client.event
async def on_ready():
    print("Logged in")       #login message

@bot.command(pass_context = True)    #trying to get "test" to return from .ping
async def ping(ctx):
    await ctx.channel.send("test")

@client.event
async def on_message(message):
    if message.author == client.user:         #testing the bot actually responds to something
        return
    if message.content.startswith("Hello"):
        await message.channel.send("Hi")

client.run('#bot token here')
3

There are 3 best solutions below

2
MaciejkaG On

Just use simple ctx.send, it's common for commands,

@bot.command()
async def ping(ctx):
    await ctx.send("test")
1
Łukasz Kwieciński On

There are a lot of things wrong in your code:

  1. Use discord.Client or commands.Bot not both
  2. You didn't enable intents, so message.author is None, here's how:
intents = discord.Intents.default()
intents.members = True

bot = commands.Bot(..., intents=intents)

Also make sure to enable privileged intents in the developer portal

  1. You're running the client, not the bot, if you want the commands to work run the bot and get rid of client
  2. Change the decorator of the on_message and on_ready events and add process_commands at the end of on_message:
@bot.event
async def on_message(message):
    if message.author == bot.user:
        return

    if message.content.startswith("Hello"):
        await message.channel.send("Hi")

    await bot.process_commands(message)

Here's your fixed code:

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.members = True

bot = commands.Bot(command_prefix= ".", intents=intents)

@bot.event
async def on_ready():
    print("Logged in")


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


@bot.event
async def on_message(message):
    if message.author == bot.user:
        return

    if message.content.startswith("Hello"):
        await message.channel.send("Hi")

    await bot.process_commands(message)


bot.run('#bot token here')
0
Justin On

There are some bugs in your code that I have found.

  1. Don’t use both discord.Client() and commands.Bot()
  2. You need to enable intents. Go to the discord developers page then to your bot and enable both intents. Quick Link To The Developer Page On Discord

Turn the intents on

Then write this code:

intents = discord.Intents.all()

bot = commands.Bot(commands_prefix=“.”, intents=intents)

Make sure you are not using any client here.

There are even more stuff needed to fix. Since now we are using bot, change everything saying client to bot. You also have to add await bot.process_commands(message) to the end of the on_message function. Also change ctx.channel.send to just ctx.send

@bot.event
async def on_ready():
     print("Logged in")       #login message

@bot.command(pass_context = True)    #trying to get "test" to return from .ping
async def ping(ctx):
    await ctx.channel.send("test")

@bot.event
async def on_message(message):
    if message.author == bot.user:         #testing the bot actually responds to something
        return
    if message.content.startswith("Hello"):
        await message.channel.send("Hi")

    await bot.process_commands(message)

bot.run('#bot token here')