I'm making a discord bot using python and continue getting errors.
My code within the cog
import discord
from discord.ext import commands
from discord import Member
class ping(commands.cog):
def __init__(self, client):
self.client = client
self._last_member = None
@commands.cog.listener()
async def on_ready(self):
print("Ping.py is ready")
@commands.command()
async def ping(self, ctx, member:discord.Member):
bot_latency = round(self.client.latency * 1000)
member = member or ctx.author
await ctx.send(f"Pong! {bot_latency}ms {member.mention}")
async def setup(client):
await client.add_cog(ping(client))
My code within my main
import discord
from discord.ext import commands
import os
import asyncio
client = commands.Bot(description="Discord Bot", command_prefix="!", intents = discord.Intents.all())
@client.event
async def on_ready():
print("The bot is ready for testing!")
print("-----------------------------")
async def load():
for filename in os.listdir("./cogs"):
if filename.endswith(".py"):
await client.load_extension(f"cogs.{filename[:-3]}")
print(f"{filename[:-3]} is loaded!")
async def main():
async with client:
await load()
await client.start('BOTTOKEN')
asyncio.run(main())
The error that is showing up
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\ext\commands\bot.py", line 935, in _load_from_module_spec
spec.loader.exec_module(lib) # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap_external>", line 995, in exec_module
File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
File "c:\Users\user\vsPythonWorkspace\cogs\ping.py", line 5, in <module>
class ping(commands.cog):
File "c:\Users\user\vsPythonWorkspace\cogs\ping.py", line 10, in ping
@commands.cog.listener()
^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'discord.ext.commands.cog' has no attribute 'listener'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "c:\Users\user\vsPythonWorkspace\main.py", line 25, in <module>
asyncio.run(main())
File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 194, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 687, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "c:\Users\user\vsPythonWorkspace\main.py", line 22, in main
await load()
File "c:\Users\user\vsPythonWorkspace\main.py", line 17, in load
await client.load_extension(f"cogs.{filename[:-3]}")
File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\ext\commands\bot.py", line 1013, in load_extension
await self._load_from_module_spec(spec, name)
File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\ext\commands\bot.py", line 938, in _load_from_module_spec
raise errors.ExtensionFailed(key, e) from e
discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.ping' raised an error: AttributeError: module 'discord.ext.commands.cog' has no attribute 'listener'
I tried reinstalling the discord pip file along with completely redoing everything from scratch expecting to find syntax errors or unwritten variables, along with looking online for similar issues, but have been unable to find anything that matches. If someone could please assist me, i would appreciate it, i've coded with python in the past years ago, and dont remember much. Thank you.
It's not commands.cog.listener()
, commands.cog
is the file, you need the commands.Cog
class. So, commands.Cog.listener()
.