bong
bong

Reputation: 63

Discord.py ping command doesn't work in a cog

My ping command doesn't work in a cog but works in my main.py file. Here's the code for the cog:

import discord
from discord.ext import commands


class Misc(commands.Cog):
    def __init__(self, bot):
        self.bot = bot


    @commands.Cog.listener()
    async def on_ready(self):
        print('Misc cog loaded\n-----')


    @commands.command()
    async def ping(self, ctx):
        await ctx.send(f'pong!\n{round(bot.latency * 1000)}ms')


def setup(bot):
    bot.add_cog(Misc(bot))

When I run the ping command I get this error:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'bot' is not defined

Upvotes: 0

Views: 975

Answers (1)

cgrom
cgrom

Reputation: 138

Simple solution, you added bot to your class object Misc as self.bot so when referring to bot.latency in that context you should actually be using self.bot.latency.

Upvotes: 2

Related Questions