Skip to content

add autoload for discord.ui.View #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions ModuBotDiscord/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import discord
from discord.ext import commands as discordCommands
from discord.ui import View
from ModuBotDiscord.commands import BaseCommand

from .config import DiscordConfig
Expand Down Expand Up @@ -35,6 +36,7 @@ def run(self):

async def setup_hook(self) -> None:
await self._register_all_commands()
await self._register_all_views()

synced_commands = await self.tree.sync()
self.logger.info(
Expand All @@ -55,3 +57,18 @@ async def _register_all_commands(self) -> None:
):
command_instance: BaseCommand = obj()
await command_instance.register(self)

async def _register_all_views(self) -> None:
for filename in os.listdir("views"):
if filename.endswith(".py") and filename != "__init__.py":
view = importlib.import_module(f"views.{filename[:-3]}")
for item in dir(view):
obj = getattr(view, item)
if (
isinstance(obj, type)
and issubclass(obj, View)
and obj is not View
):
view_instance: View = obj()
if view_instance.timeout is None:
self.add_view(view_instance)
Loading