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 loadedn-----') @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
Advertisement
Answer
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
.