НЕ запускается cog webhook disnake

Рейтинг: 0Ответов: 1Опубликовано: 07.04.2023
> Traceback (most recent call last):
  File "C:\Users\fixik\AppData\Local\Programs\Python\Python311\Lib\site-packages\disnake\ext\commands\common_bot_base.py", line 447, in _load_from_module_spec
    setup = lib.setup
            ^^^^^^^^^
AttributeError: module 'cogs.event_bot.webhook' has no attribute 'setup'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\micro_universe_2.0\bot.py", line 111, in <module>
    bot.load_extension(f'cogs.{folder}.{file[:-3]}')
  File "C:\Users\fixik\AppData\Local\Programs\Python\Python311\Lib\site-packages\disnake\ext\commands\common_bot_base.py", line 512, in load_extension
    self._load_from_module_spec(spec, name)
  File "C:\Users\fixik\AppData\Local\Programs\Python\Python311\Lib\site-packages\disnake\ext\commands\common_bot_base.py", line 450, in _load_from_module_spec
    raise errors.NoEntryPointError(key)
disnake.ext.commands.errors.NoEntryPointError: Extension 'cogs.event_bot.webhook' has no 'setup' function.
import disnake
import aiohttp
from disnake import Webhook
from disnake.ext import commands


class Webhook(commands.Cog):

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

    @commands.slash_command(name="webhook", description="ggg")
    async def webhook(self, inter, url, text):
        async with aiohttp.ClientSession() as webhook:
            webhook = Webhook.from_url(url=url, session=webhook)
            await webhook.send(text)
            await inter.send('gg', ephemeral=True)




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

Ответы

▲ 1Принят

Создайте класс и поместите в него код:

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

В этом классе можно создавать все нужные Вам команды:

    @commands.slash_command(name="webhook", description="ggg")
    async def webhook(self, inter, url, text):
        async with aiohttp.ClientSession() as webhook:
            webhook = Webhook.from_url(url=url, session=webhook)
            await webhook.send(text)
            await inter.send('gg', ephemeral=True)

И далее обязательно добавьте данный класс в Cog's:

async def setup(bot):
    await bot.add_cog(Name(bot))

Полный код:

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

    @commands.slash_command(name="webhook", description="ggg")
    async def webhook(self, inter, url, text):
        async with aiohttp.ClientSession() as webhook:
            webhook = Webhook.from_url(url=url, session=webhook)
            await webhook.send(text)
            await inter.send('gg', ephemeral=True)

async def setup(bot):
    await bot.add_cog(Name(bot))

Как сделать вывод Embed'a (по просьбе из комментариев):

    @commands.slash_command(name="webhook", description="ggg")
    async def webhook(self, inter, url, text):
        embed = disnake.Embed(
            title="Title",
            description="Description",
            color=disnake.Colour.yellow(),
            timestamp=datetime.datetime.now(),
            )
        await inter.send(embed=embed, ephemeral=True)

Подробнее про Embed в disnake: клик