@UuuuuuuuuuuuuuuuNiX

Не работают кнопки в discord.py, что делать?

Запуская бота получаю ошибку от одного кога - [ERROR]: Extension 'cogs.code' raised an error: NameError: name 'Code' is not defined.

Сам код кога ниже:
import discord
from discord.ext import commands
from discord import app_commands
from discord import ui
from discord.ext.commands import Context
from text import *
from config import *
import sqlite3
import time

class Code(commands.Cog):
    def __init__(self, bot: commands.Bot):
        self.bot = bot
        self.db = db
        self.cur = self.db.cursor()    

    @Code.bot.command()
    @commands.is_owner()
    async def update(ctx):
      await Code.bot.tree.sync()
      await ctx.send("Обновил")

    class CodeButton(discord.ui.View):
        def __init__(self, inv: str):
            super().__init__()
            self.inv = inv
            self.add_item(discord.ui.Button(label="test", url=self.inv))

        @discord.ui.button(label="test2", style=discord.ButtonStyle.green)
        async def test2(self, interaction: discord.Interaction, button: discord.ui.Button):
            await interaction.response.send_message(self.inv, ephemeral=True)


    @self.bot.tree(name="money_code", description="test")
    async def reg(self, interaction: discord.Interaction):
        channel_id = 1135882475806150789
        channel = self.bot.get_channel(channel_id)
        #author = ctx.author
        #user_id = author.id
        #username = author.name
        #discriminator = author.discriminator
        class Form(ui.Modal, title="dsf"):
            db = self.db
            cur = db.cursor()
            _code_name = ui.TextInput(label="Название кода: ", min_length=2, max_length=40)
            _money = ui.TextInput(label="Сколько дает денег: ", min_length=1, max_length=25)
            async def on_submit(self, viewinteract: discord.Interaction):
                inv = await self.ctx.channel.create_invite()

                embed = discord.Embed(
                    title="Успешно!",
                    color=discord.Color.green(),
                    description=f"Код `{str(self._code_name)}`, создан!"
                )
                await viewinteract.response.send_message(embed=embed, view=CodeButton(str(inv)))
        await interaction.response.send_modal(Form())

async def setup(bot: commands.Bot):
    await bot.add_cog(Code(bot))
    print("Cog \"Code\" работает!")


Помогите!!!
  • Вопрос задан
  • 98 просмотров
Пригласить эксперта
Ответы на вопрос 1
Vindicar
@Vindicar
RTFM!
@Code.bot.command()
На момент выполнения этого декоратора класс ещё не закончил создаваться, а значит, не был присвоен имени Code.
Почитай документацию на либу, как правильно создавать коги и каким декоратором в них отмечать команды.
Для discord.py это @commands.command().
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы