【问题标题】:I am having an error in loading my cogs in discord.py我在 discord.py 中加载我的 cogs 时出错
【发布时间】:2021-06-14 02:33:39
【问题描述】:

我在加载我的 cogs 时遇到问题。

我正在尝试将“fun.py”与一个名为“Fun”的类连接到我的机器人或“main.py”

这是我的代码

#------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)

在 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()

我收到以下错误:

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.

有没有办法解决这个问题? 我是 discord.py 的新手,所以请详细解释一下。

我还查看了多个 IDE、多个 Youtube 视频和一些论坛。我还查看了 Discord.py 文档。

我似乎无法找到解决方案。

【问题讨论】:

  • 你试过小写吗? bot.load_extension('fun')。我已经解释了 here 扩展和 cogs 的工作原理
  • 当我使用 'fun' 时,它说 self 没有定义。当我使用“Fun”时,它显然给出了与我的问题中所示相同的错误。 @ŁukaszKwieciński

标签: python discord discord.py


【解决方案1】:

您需要使用与文件名匹配的名称来加载扩展名,即bot.load_extension('fun')

至于“self is not defined”的错误,那是因为你声明你的类是self的子类,self没有定义。相反,请执行以下操作:


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

    ...

【讨论】:

  • 非常感谢。这有帮助,但我还有另一个错误。
  • Traceback(最近一次调用最后):文件“main.py”,第 342 行,在 bot.load_extension('fun') 文件“/opt/virtualenvs/python3/lib/python3 .8/site-packages/discord/ext/commands/bot.py”,第 678 行,在 load_extension self._load_from_module_spec(spec, name) 文件“/opt/virtualenvs/python3/lib/python3.8/site-packages/ discord/ext/commands/bot.py",第 623 行,在 _load_from_module_spec 中引发错误。ExtensionFailed(key, e) from e discord.ext.commands.errors.ExtensionFailed: Extension 'fun' raise an error: TypeError: cogs must derived从齿轮
  • 确保你继承 Cog 像这样:class Fun(commands.Cog)
  • 谢谢。你们两个都帮了我。我真的很感激
猜你喜欢
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-30
  • 2021-04-19
  • 2021-04-19
  • 1970-01-01
相关资源
最近更新 更多