Nishil Sheth
Nishil Sheth

Reputation: 165

I am having an error in loading my cogs in discord.py

I am having issues in loading my cogs.

I am trying to connect 'fun.py' with a class called 'Fun' to my bot or 'main.py'

Here is my code

#------importing packages


import keep_alive
import os
import discord
from discord.ext import commands
import random
import redditeasy
import math
from PIL import Image
from io import BytesIO 
import asyncio


bot = commands.Bot(command_prefix='-')
my_token = os.environ['Token']

#------Running the bot
bot.load_extension('Fun')
keep_alive.keep_alive()
bot.run(my_token)

In fun.py

#-----Importing Stuff
import keep_alive
import discord
from discord.ext import commands
from PIL import Image
from io import BytesIO
import redditeasy
import random


class Fun(self, bot):
    def __init__(self):
        self.bot = bot


    #Memes
    @commands.command()
    async def meme(self, ctx):

        post = redditeasy.AsyncSubreddit(subreddit = 'dankmemes',                              client_id='Id here',client_secret = 'secret here',user_agent = 'memes')

        postoutput = await post.get_post()

        em2 = discord.Embed(title = f'{postoutput.title}')
        url = postoutput.content
  
        em2.set_image(url = url)
        await ctx.send(embed = em2)

    #Naruto_Memes
    @commands.command(aliases = ['Nmeme', 'NMEME', 'nm', 'NM', 'Nm'])
    async def nmeme(self, ctx):
        post = redditeasy.AsyncSubreddit(subreddit = 'narutomemes',                client_id='Id here',client_secret = 'secret here',user_agent = 'memes')

        postoutput = await post.get_post()

        em3 = discord.Embed(title = f'{postoutput.title}')
        url = postoutput.content
        em3.set_image(url = url)
        await ctx.send(embed = em3)



    @commands.command(aliases=['8ball', '8b'])
    async def _8ball(ctx,*,question,):

        responses = [
        'It is Certain.', 'It is decidedly so.', 'Without a doubt.',
        'Yes definitely.', 'You may rely on it.', 'As I see it, yes.',
        'Most likely.', 'Outlook good.', 'Yes.', 'Signs point to yes.',
        'Reply hazy, try again.', 'Ask again later.',
        'Better not tell you now.', 'Cannot predict now.']

        await ctx.send(f'Question: {question} Answer: {random.choice(responses)}')


    #Slap
    @bot.command()
    async def slap(ctx, slaped : discord.Member = None,):

        if slaped == None:
            await ctx.send('Please specify a person to slap!')

        slapped = Image.open('Slap_img.jpg')

        asset1 = ctx.message.author.avatar_url_as(size = 256)
        data1 = BytesIO(await asset1.read())
        pfp1 = Image.open(data1)

        asset2 = slaped.avatar_url_as(size = 256)
        data2 = BytesIO(await asset2.read())
        pfp2 = Image.open(data2)

        slapped.paste(pfp1, (680, 90))
        slapped.paste(pfp2, (320, 234))

        slapped.save('Slapped_final_img.jpg')

        await ctx.send(file = discord.File('Slapped_final_img.jpg'))


    #Profile
    @bot.command(aliases = ['prof', 'whois'])
    async def profile(ctx, member : discord.Member):

        topRole = member.top_role

        profile_em = discord.Embed(title = 'Profile', colour = discord.Colour.green())
        profile_em.add_field(name = 'Name', value = member.mention)
        profile_em.add_field(name = 'ID', value = member.id)
        profile_em.add_field(name = 'Top Role', value = topRole.mention)

        profile_em.set_thumbnail(url = member.avatar_url)
        profile_em.set_footer(icon_url = ctx.message.author.avatar_url, text = f'Requsted by {ctx.message.author}')

        await ctx.send(embed = profile_em)


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

keep_alive.keep_alive()

My Files

I am Getting the Following Error:

Traceback (most recent call last):
  File "main.py", line 342, in <module>
    bot.load_extension('Fun')
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 676, in load_extension
    raise errors.ExtensionNotFound(name)
discord.ext.commands.errors.ExtensionNotFound: Extension 'Fun' could not be loaded.

Is there any way to fix this? I am new to discord.py, so please explain me a bit thouroughly.

I checked multiple IDEs, multiple Youtube Videos and some forums as well. I also looked through the Discord.py Documentation.

I just cannot seem to find a solution.

Upvotes: 0

Views: 766

Answers (1)

Will Da Silva
Will Da Silva

Reputation: 7040

You need to load the extension using the name which matches the filename, i.e. bot.load_extension('fun').

As for the "self is not defined" error, that is because you declared your class as a subclass of self, which is not defined. Instead, do the following:


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

    ...

Upvotes: 1

Related Questions