yunikeil
@yunikeil

При выгрузке Cog, nextcord.ui.View продолжает работать (Bot.add_view() не используется) Как это исправить?

После выгрузки Cog'а, продолжает функционировать view (кнопки/выпадающие меню продолжают работать). Хотел выяснить, как это исправить.
Моя основная идея была в том, чтобы перезагрузить "Cog" cogs.AuthPayWT, чтобы обновить код, который обрабатывает клики не перезапуская бота полностью или не редактируя исходное сообщение.
Также не работает метод Bot.remove_view() после создание persistent или я его неправильно использую. Если не сложно, я бы хотел увидеть пример кода файла-кога, в котором реализована выгрузка View.
Заранее спасибо)

Пример файла Cog без использования persistent (ui.View() после Bot.unload_extension() продолжает работать)
spoiler

import nextcord
from nextcord.ext import commands, tasks
from nextcord.ext.commands import Bot, Cog, Context


class Confirm(nextcord.ui.View):
    def __init__(self):
        super().__init__()
        self.value = None
        
    @nextcord.ui.button(label="Confirm", style=nextcord.ButtonStyle.green)
    async def confirm(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
        await interaction.response.send_message("Confirming", ephemeral=True)
        self.value = True

    @nextcord.ui.button(label="Cancel", style=nextcord.ButtonStyle.grey)
    async def cancel(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
        await interaction.response.send_message("Cancelling", ephemeral=True)
        self.value = False


class Helpers(Cog):
    def __init__(self, bot: Bot) -> None:
        self.bot = bot

    def __del__(self):
        ...
    
    def cog_unload(self):
        ...

    @commands.command()
    async def msg(self, ctx: Context):
        if ctx.author.id not in self.bot.OWNERS:
            return
        await ctx.send(content="ok!", view=Confirm())
        
    


def setup(bot: Bot) -> None:
    print("Helpers.py loaded")
    bot.add_cog(Helpers(bot))



Пример файла Cog с использования persistent (точно также продолжает работать, также не удаляется из Bot.persistent_views)
spoiler

import nextcord
from nextcord.ext import commands, tasks
from nextcord.ext.commands import Bot, Cog, Context


class Confirm(nextcord.ui.View):
    def __init__(self):
        super().__init__()
        self.timeout = None
        self.value = None
        
    @nextcord.ui.button(label="Confirm", style=nextcord.ButtonStyle.green, custom_id="pres_buttn_1")
    async def confirm(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
        await interaction.response.send_message("Confirming", ephemeral=True)
        self.value = True

    @nextcord.ui.button(label="Cancel", style=nextcord.ButtonStyle.grey, custom_id="pres_buttn_2")
    async def cancel(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
        await interaction.response.send_message("Cancelling", ephemeral=True)
        self.value = False


class Helpers(Cog):
    def __init__(self, bot: Bot) -> None:
        self.bot = bot

    def __del__(self):
        ...
    
    def cog_unload(self):
        self.bot.DATA['StafPosition:StafSelect:persistent'] = False
        print("StafPosition:StafSelect:persistent UN")
        # Какой вариант тут будет правильным? (ни один их них не работает)
        #self.bot.remove_view(self.bot.persistent_views[0])
        #self.bot.remove_view(Confirm())

    @Cog.listener()
    async def on_ready(self):
        if not self.bot.DATA['StafPosition:StafSelect:persistent']:
            self.bot.add_view(Confirm())
            print("StafPosition:StafSelect:persistent OK")
            self.bot.DATA['StafPosition:StafSelect:persistent'] = True

    @commands.command()
    async def msg(self, ctx: Context):
        if ctx.author.id not in self.bot.OWNERS:
            return
        await ctx.send(content="ok!", view=Confirm())
       

def setup(bot: Bot) -> None:
    print("Helpers.py loaded")
    bot.add_cog(Helpers(bot))


Файл main.py
spoiler

import os
import dill
import asyncio
from abc import ABC
from urllib.parse import urlencode
from traceback import format_exception

import aeval
import nextcord
from nextcord.ext import commands, tasks

import configuration


class Bot(commands.Bot, ABC):
    def __init__(self, **options):
        super().__init__(command_prefix='>',
                         help_command=None,
                         intents=nextcord.Intents.all(),
                         **options)
        self.DATA: dict = {
            'views': None,
            'bot-started': False,
            'StafPosition:StafSelect:persistent': False
        }
        self.OWNERS: list[int] = []
        self.EVAL_OWNER: list[int] = []
        self.config: object = configuration

    async def on_ready(self):
        if not self.DATA['bot-started']:
            #application_info = await self.application_info()
            #self.OWNERS.append(application_info.owner.id)
            #self.EVAL_OWNER.append(application_info.owner.id)
            self.DATA['bot-started'] = True
        print(f"Logged in as {self.user} (ID: {self.user.id})\n------")

    async def on_command_error(self, ctx, error):
        if isinstance(error, nextcord.ext.commands.CommandNotFound):
            return
        raise error
    ...


bot = Bot()


@bot.command()
async def cog_load(ctx: commands.Context, cog: str):
    if ctx.author.id not in bot.OWNERS:
        return
    try:
        bot.load_extension(f"cogs.{cog}")
    except BaseException as ex:
        await ctx.channel.send(f"Exception:\n```bash\n{ex}\n```")
    else:
        await ctx.channel.send(f"```cog.{cog} loaded!```")


@bot.command()
async def cog_unload(ctx: commands.Context, cog: str):
    if ctx.author.id not in bot.OWNERS:
        return
    try:
        bot.unload_extension(f"cogs.{cog}")
    except BaseException as ex:
        await ctx.channel.send(f"Exception:\n```bash\n{ex}\n```")
    else:
        await ctx.channel.send(f"```Cog cog.{cog} unloaded!```")


@bot.command()
async def cog_reload(ctx: commands.Context, cog: str):
    if ctx.author.id not in bot.OWNERS:
        return
    try:
        bot.unload_extension(f"cogs.{cog}")
        await asyncio.sleep(1)
        bot.load_extension(f"cogs.{cog}")
    except BaseException as ex:
        await ctx.channel.send(f"Exception:\n```bash\n{ex}\n```")
    else:
        await ctx.channel.send(f"```Cog cog.{cog} reloaded!```")


@bot.command()
async def eval(ctx, *, content):
    if ctx.author.id not in bot.EVAL_OWNER:
        return
    standart_args = {
        "nextcord": nextcord,
        "commands": commands,
        "bot": bot,
        "ctx": ctx,
        "asyncio": asyncio,
    }

    try:
        await aeval.aeval(content, standart_args, {})
    except Exception as ex:
        result = "".join(format_exception(ex, ex, ex.__traceback__))
        await ctx.channel.send(f"Exception:\n```bash\n{result.replace('```', '`')}\n```")


if __name__ == "__main__":
    cogs_add_on_start: list[str] = ["StafPosition"]
    if cogs_add_on_start:
        [bot.load_extension(f"cogs.{cog}") for cog in cogs_add_on_start]
    bot.run(bot.config.discord_token)

  • Вопрос задан
  • 111 просмотров
Решения вопроса 1
yunikeil
@yunikeil Автор вопроса
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

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

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