Reputation: 155
I've got a Problem. I am writing a discord Bot with "Discord.py". I'm using "Python 3.8.1" on my "Raspberry Pi 3B".
I have got a "load" and a "unload" function in my main file. They do work as they should. But I have Cogs, for example the easies one: "ping". I can load "ping", but the command doesn't work (in every Cog): "Ignoring exception in command None: discord.ext.commands.errors.CommandNotFound: Command "ping" is not found"
I have no Idea where the Problem could be. The Code seems to be right - according to others. I watched YouTube Videos on it, but no answer... I will try to use another python version for example "3.7.x"...
My bot.py:
import discord
from discord.ext import commands
import os
client = commands.Bot(command_prefix = "/")
@client.event
async def on_ready():
print(f'\n\nBot is ready!\nName: {client.user.name}\nID: {client.user.id}\n ---------\n')
return
@client.command()
async def load(ctx, extension):
client.load_extension(f'cogs.{extension}') #loads the extension in the "cogs" folder
await ctx.send(f'Loaded "{extension}"')
print(f'Loaded "{extension}"')
return
@client.command()
async def unload(ctx, extension):
client.unload_extension(f'cogs.{extension}') #unloads the extension in the "cogs" folder
await ctx.send(f'Unloaded "{extension}"')
print(f'Unoaded "{extension}"')
return
print('\n')
for filename in os.listdir('./cogs'): #loads all files (*.py)
if filename.endswith('.py'):
client.load_extension(f'cogs.{filename[:-3]}') #loads the file without ".py" for example: cogs.ping
print(f'Loaded {filename[:-3]}')
client.run('MY TOKEN')
My ping.py:
import discord
from discord.ext import commands
class Ping(commands.Cog):
def __init__(self, client):
self.client = client
@commands.command()
async def ping(self, ctx):
await ctx.send(f'pong!\n{round(client.latency * 1000)}ms')
return
def setup(client):
client.add_cog(Ping(client))
Do you find any mistake??
Upvotes: 2
Views: 4479
Reputation: 500
You have to put the function ping
in the class's scope, not inside the constructor.
import discord
from discord.ext import commands
class Ping(commands.Cog):
def __init__(self, client):
self.client = client
@commands.command()
async def ping(self, ctx): # This was inside '__init__' before
await ctx.send(f'pong!\n{round(self.client.latency * 1000)}ms')
return
def setup(client):
client.add_cog(Ping(client))
Upvotes: 2