28 lines
924 B
Python
28 lines
924 B
Python
|
|
import discord
|
||
|
|
import importlib
|
||
|
|
import os
|
||
|
|
|
||
|
|
class View(discord.ui.View):
|
||
|
|
|
||
|
|
def __init__(self, button_dir=None):
|
||
|
|
super().__init__(timeout=None)
|
||
|
|
self.button_dir = button_dir
|
||
|
|
self.buttons = []
|
||
|
|
self.get_buttons()
|
||
|
|
self.load_buttons()
|
||
|
|
|
||
|
|
def get_buttons(self):
|
||
|
|
if self.button_dir is None:
|
||
|
|
for filename in os.listdir(self.button_dir):
|
||
|
|
if filename.endswith(".py"):
|
||
|
|
class_name = filename[:-3]
|
||
|
|
module = self.button_dir.replace("./", "", 1)
|
||
|
|
module = module.replace("/", ".")
|
||
|
|
btn_module = importlib.import_module(f"{module}.{class_name}")
|
||
|
|
btn_class = getattr(btn_module, class_name)
|
||
|
|
self.buttons.append(btn_class())
|
||
|
|
return self.buttons
|
||
|
|
|
||
|
|
def load_buttons(self):
|
||
|
|
for btn in self.buttons:
|
||
|
|
self.add_item(btn)
|