0

i have an error and i can't resolve it the error is the title i have already tested in other situations

import discord
from discord.ext import commands

client = discord.Client()

class utility(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.Cog.listener()
    async def on_ready(self):
        print("Cog utility pronto!")

    @commands.command()
    async def ping(self, ctx):
        await ctx.send(f"Pong! {round(client.latency * 1000)}ms")

    @commands.command()
    async def clear(ctx, amount=5):
        await ctx.channel.purge(limit=amount)
        await ctx.send(f"{amount}Messaggi eliminati")

def setup(client):
    client.add_cog(utility(client))

1 Answer 1

1

Commands in a cog are still methods of an instance of the cog. The first argument passed to them is always the instance

class utility(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.Cog.listener()
    async def on_ready(self):
        print("Cog utility pronto!")

    @commands.command()
    async def ping(self, ctx):
        await ctx.send(f"Pong! {round(self.client.latency * 1000)}ms")

    @commands.command()
    async def clear(self, ctx, amount=5):
        await ctx.channel.purge(limit=amount)
        await ctx.send(f"{amount}Messaggi eliminati")
Sign up to request clarification or add additional context in comments.

3 Comments

So when i have this error I need to add "self" in every Cog commands bracket right? but the .ping command does not work it says: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'client' is not defined
You need to access client through self: await ctx.send(f"Pong! {round(self.client.latency * 1000)}ms")
Patrick really thank now i have learner how to use "self" well

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.