-4

I just started studying Python this week and I don't understand what I'm doing wrong. My discord bot is online, but every time I try to type a command in Discord it pings back to VS Code as

"discord.ext.commands.errors.CommandNotFound: Command "roll" is not found Ignoring exception in command None:

It's not just roll command, it doesn't seem to be registering any commands. What should I do?

import discord
from discord.ext import commands
import random

TOKEN = 'token hidden'

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

@bot.event
async def on_ready():
    print(f'We have logged in as {bot.user}')

@bot.command()
async def ping(ctx):
    await ctx.send('Pong!')

@client.command()
async def roll(ctx, dice: str):
    """Rolls a dice in NdN format."""
    try:
        rolls, limit = map(int, dice.split('d'))
    except Exception:
        await ctx.send('Format has to be in NdN!')
        return

    result = ', '.join(str(random.randint(1, limit)) for r in range(rolls))
    await ctx.send(result)

bot.run(TOKEN)
8
  • it should be @bot instead of @client Commented Apr 20 at 23:34
  • it actually was bot at first, then it didn't work and someone recommended it be changed to client... so unfortunately neither of them are working Commented Apr 21 at 0:11
  • when I use @bot then it works for me - but it needs full command like !roll 4d6 - if you don't use parameter then it also may not work. it may raise error but discord may hide it. Commented Apr 21 at 0:12
  • discord may catch some errors and hides some - and it may need to change settings in logging to get all details. See: Setting Up Logging Commented Apr 21 at 0:14
  • 1
    What do you mean when you say that when it was @bot, it "didn't work"? Could be more specific? Commented Apr 21 at 4:14

2 Answers 2

0

It has to be @bot instead of @client. And this is main problem.

Other problem is that you have to run it with parameter like !roll 4d6.

If you run without parameter then it may also raise error (but it is different error).

In some situations dicord can catch errors and hides them and it may need to change settings for logging - see Setting Up Logging

You would add default value to dice - ie. empty string - and check if user added parameter.

@bot.command()
async def roll(ctx, dice: str = ""):  # emtpy string as default value for `dice`
    """Rolls a dice in NdN format."""

    if not dice:
        await ctx.send('It needs parameter. Format has to be in NdN!')
        return

    try:
        rolls, limit = map(int, dice.split('d'))
    except Exception:
        await ctx.send('Format has to be in NdN!')
        return

    result = ', '.join(str(random.randint(1, limit)) for r in range(rolls))
    await ctx.send(result)

Full working code used for tests:

I had to add indents to run it correctly with discord.py
(but nextcord may run without indents).

import discord
from discord.ext import commands
#import nextcord as discord
#from nextcord.ext import commands
import random
import logging
#import os

print("Discord version:", discord.__version__)  # discord.py: 2.5.2 , nextcord: 3.1.0

TOKEN = 'token hidden'
#TOKEN = os.getenv('DISCORD_TOKEN')

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

bot = commands.Bot(command_prefix='!', intents=intents) #, log_level=logging.DEBUG)

@bot.event
async def on_ready():
    print(f'We have logged in as {bot.user}')

@bot.command()
async def ping(ctx):
    await ctx.send('Pong!')

@bot.command()
async def roll(ctx, dice: str = ""):
    """Rolls a dice in NdN format."""

    if not dice:
        await ctx.send('It needs parameter. Format has to be in NdN!')
        return

    try:
        rolls, limit = map(int, dice.split('d'))
    except Exception:
        await ctx.send('Format has to be in NdN!')
        return

    result = ', '.join(str(random.randint(1, limit)) for r in range(rolls))
    await ctx.send(result)

bot.run(TOKEN)
Sign up to request clarification or add additional context in comments.

3 Comments

update: bot online and answering commands, but won't show online status at all. Strange
what "online status" do you means? It can be problem with discord servers, not with code.
If I first run discord application and later bot then it doesn't show "green dot" in application but if I first run bot and later discord application (after full closing) then I can see "green dot" - so as for me problem is in discord application.
-1

I'm guessing your issue is with the Intents, Try this change here

intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)

and you can look at how to set up the direct messaging to your bot here to make the intents available.

https://discordjs.guide/popular-topics/intents.html#privileged-intents

1 Comment

it would give error TypeError: BotBase.__init__() missing 1 required keyword-only argument: 'intents' but OP has different error message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.