'discord.py lots of "cog" errors
trying to make a global banning system with cogs but it gives me cog errors and im not too good with cog. the code is from an old github repo by the way so i dont take credit for this.
bot.py:
import discord
import os
import json
from discord.ext import commands
client = commands.Bot(command_prefix = ".")
client.remove_command("help")
@client.event
async def on_ready():
print("Global Banning System is online!")
for filename in os.listdir("cogs"):
if filename.endswith(".py"):
client.load_extension(f"cogs.gbs")
client.run(token)
and i also have another file in a new folder ("cogs") that is called "gbs.py". gbs.py:
import discord
from discord.ext import commands
class gbs(commands.Cog):
def __init__(self, client):
self.client = client
@commands.command()
@commands.has_role(roleid) # Staff's Role
async def gban(self, ctx, member: discord.User):
channel = self.client.get_channel(channelid) # Logs
auth = ctx.author
await ctx.send("Ban Issue successfuly opened. Now, please type the ban reason!")
reason = await self.client.wait_for('message', timeout = 600)
embed = discord.Embed(title = f"Global Banned {member.name}", colour = 0xFF0000)
embed.add_field(name = "Reason:", value = reason.content, inline = False)
embed.add_field(name = "User ID:", value = f"`{member.id}`",)
embed.add_field(name = "Staff Member:", value = f"{auth.name}")
embed.set_thumbnail(url = member.avatar_url)
await ctx.send(embed = embed)
await ctx.send(f"**{member.name}** has been globbaly banned!")
for guild in self.gbs.guilds:
await guild.ban(member)
def setup(client):
gbs.add_cog(gbs(client))
This is the errors that it gives me.
Traceback (most recent call last):
File "C:\Users\user-01\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\cog.py", line 723, in _load_from_module_spec
setup(self)
File "C:\Users\user-01\Desktop\Global Banning System - Discord Bot\cogs\gbs.py", line 28, in setup
gbs.add_cog(gbs(client))
AttributeError: type object 'gbs' has no attribute 'add_cog'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\user-01\Desktop\Global Banning System - Discord Bot\bot.py", line 15, in <module>
client.load_extension(f"cogs.gbs")
File "C:\Users\user-01\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\cog.py", line 783, in load_extension
self._load_from_module_spec(spec, name)
File "C:\Users\user-01\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\cog.py", line 728, in _load_from_module_spec
raise errors.ExtensionFailed(key, e) from e
discord.errors.ExtensionFailed: Extension 'cogs.gbs' raised an error: AttributeError: type object 'gbs' has no attribute 'add_cog'
Solution 1:[1]
You add a cog class to your bot not to your gbs. So replace gbs with client to solve the problem.
def setup(client):
# instead of gbs.add_cog...
client.add_cog(gbs(client)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DoctorFuchs |
