As my bot is getting bigger, I'm trying to implement cogs, however I have ran across a problem. I have my whole code set up and ready, but for some weird reason I keep getting this error:
Traceback (most recent call last):
File "C:\Users\Lauras\Desktop\Akagi Bot\main.py", line 107, in <module>
bot.add_cog("cogs.fun")
File "C:\Users\Lauras\AppData\Local\Programs\Python\Python36\lib\site-packages\discord\ext\commands\bot.py", line 477, in add_cog
raise TypeError('cogs must derive from Cog')
TypeError: cogs must derive from Cog
My code on main.py looks like this:
import discord
import asyncio
import typing
import random
import json
import oauth
from discord.ext
import commands
bot = commands.Bot(command_prefix = '~')
@bot.event
async def on_ready():
await bot.change_presence(activity = discord.Activity(name = 'with Kaga :3', type = 0))
print(discord.__version__)
print(f "{bot.user.name} - {bot.user.id}")
print('Akagi is ready to serve the Commander :3 !')
bot.add_cog("cogs.fun")
bot.run(oauth.bot_token)
And the "fun" cog is as follows:
import discord
from discord.ext
import commands
bot = commands.Bot(command_prefix = '~')
class FunCog:
def __init__(self, bot):
self.bot = bot
@commands.command()
async def hug(self, ctx):
await ctx.send('has been hugged by', file = discord.File('iloveyou.gif'))
pass
def setup(bot: commands.Bot):
bot.add_cog(FunCog(bot))
The name of the cog is automatically derived from the class name but can be overridden. See Meta Options.,All commands must now take a self parameter to allow usage of instance attributes that can be used to maintain state.,Just as we remove a cog by its name, we can also retrieve it by its name as well. This allows us to use a cog as an inter-command communication protocol to share data. For example:,Note that we reference the cog by name, which we can override through Meta Options. So if we ever want to remove the cog eventually, we would have to do the following.
class Greetings(commands.Cog):
def __init__(self, bot):
self.bot = bot
self._last_member = None
@commands.Cog.listener()
async def on_member_join(self, member):
channel = member.guild.system_channel
if channel is not None:
await channel.send(f 'Welcome {member.mention}.')
@commands.command()
async def hello(self, ctx, *, member: discord.Member = None):
""
"Says hello"
""
member = member or ctx.author
if self._last_member is None or self._last_member.id != member.id:
await ctx.send(f 'Hello {member.name}~')
else:
await ctx.send(f 'Hello {member.name}... This feels familiar.')
self._last_member = member
await bot.add_cog(Greetings(bot))
await bot.remove_cog('Greetings')
class Economy(commands.Cog):
...
async def withdraw_money(self, member, money):
# implementation here
...
async def deposit_money(self, member, money):
# implementation here
...
class Gambling(commands.Cog):
def __init__(self, bot):
self.bot = bot
def coinflip(self):
return random.randint(0, 1)
@commands.command()
async def gamble(self, ctx, money: int):
""
"Gambles some money."
""
economy = self.bot.get_cog('Economy')
if economy is not None:
await economy.withdraw_money(ctx.author, money)
if self.coinflip() == 1:
await economy.deposit_money(ctx.author, money * 1.5)
class MyCog(commands.Cog, name = 'My Cog'):
pass
>>> cog = bot.get_cog('Greetings') >>>
commands = cog.get_commands() >>>
print([c.name
for c in commands
])
GatewayNotFound – The gateway to connect to Discord is not found. Usually if this is thrown then there is a Discord API outage.,Represents a client connection that connects to Discord. This class is used to interact with the Discord WebSocket and API.,TypeError – The cog does not inherit from Cog.,This is only for the messages received from the client WebSocket. The voice WebSocket will not trigger this event.
@client.event
async def on_ready():
print('Ready!')
async
for guild in client.fetch_guilds(limit = 150):
print(guild.name)
guilds = await client.fetch_guilds(limit = 150).flatten() # guilds is now a list of Guild...
try:
loop.run_until_complete(start( * args, ** kwargs))
except KeyboardInterrupt:
loop.run_until_complete(close())
# cancel all tasks lingering
finally:
loop.close()
for guild in client.guilds:
for channel in guild.channels:
yield channel
for guild in client.guilds:
for member in guild.members:
yield member
how to make it that your discord bot can autorole in the discord.py rewrite , how to let only admins do a command in discord.py , how to send a message in a specific channel discord.py , discord.py get list of availbale commands
# bot file import os from discord.ext import commands bot = commands.Bot(command_prefix = '.') # I am assuming that you have a test.py cog in a cogs folder bot.load_extension('cogs.test') # this is good but you can make it better for filename in os.listdir('./cogs'): if filename.endswith('.py'): bot.load_extension(f 'cogs.{filename[:-3]}') else: print(f 'Unable to load {filename[:-3]}') bot.run(token)
from discord.ext
import commands
class Test_Cog(commands.Cog):
def __init__(self, bot):
self.bot = bot # defining bot as global
var in class
@commands.Cog.listener() # this is a decorator
for events / listeners
async def on_ready(self):
print('Bot is ready!.')
@commands.command() # this is
for making a command
async def ping(self, ctx):
await ctx.send(f 'Pong! {round(self.bot.latency * 1000)}')
def setup(bot): # a extension must have a setup
function
bot.add_cog(Test_Cog(bot)) # adding a cog
ListOfCogs = client.cogs # this is a dictionary! print(len(ListOfCogs)) for NameOfCog, TheClassOfCog in ListOfCogs.items(): # we can loop trough it! print(NameOfCog)