Reputation: 1
so i was trying to create my first discord bot, with no experience in Python whatsoever just to learn stuff by doing so and stumbled on an Error (Title) which was answered quiet a lot, but didn't really help me.
Im just gonna share my code:
import discord
from discord.ext import commands
import random
client = commands.Bot(command_prefix="=", intents=discord.Intents.all())
@client.event
async def on_ready():
print("Bot is connected to Discord")
@commands.command()
async def ping(ctx):
await ctx.send("pong")
client.add_command(ping)
@client.command()
async def magic8ball(ctx, *, question):
with open("Discordbot_rank\magicball.txt", "r") as f:
random_responses = f.readlines()
response = random.choice(random_responses)
await ctx.send(response)
client.add_command(magic8ball)
client.run(Token)
I tried to run this multiple times, first without the client.add_command line, but both methods for registering a command didn't work for me. I also turned all options for privileged gateway intents in the discord developer portal on. What am i doing wrong?
Upvotes: 0
Views: 1508
Reputation: 626
Because you have an incorrect decorator for your command ping
. It should be @client.command()
in your case.
Also, in most of the cases, you don't need to call client.add_command(...)
since you use the command()
decorator shortcut already.
Only when you have cogs, you will use @commands.command()
decorator. Otherwise, all commands in your main py file should have decorator @client.command()
in your case.
A little bit of suggestion, since you already uses commands.Bot
, you could rename your client
variable to bot
since it is a little bit ambiguous.
Upvotes: 1
Reputation: 165
You have to use Slash commands cause user commands are no longer available
Example code using Pycord library
import discord
bot = discord.Bot()
@bot.slash_command()
async def hello(ctx, name: str = None):
name = name or ctx.author.name
await ctx.respond(f"Hello {name}!")
bot.run("token")
Upvotes: 0