mirror of
https://github.com/Matthww/TuneBot.git
synced 2026-09-21 23:37:47 +00:00
Commit hoarding is a terrible practice
This commit is contained in:
@@ -22,51 +22,74 @@ from discord.ext.commands.errors import ExtensionNotFound
|
||||
from discord.ext.commands.errors import NoEntryPointError
|
||||
|
||||
from context import CustomContext
|
||||
from tunebot.plugins import FileSystemPluginLoader
|
||||
from tunebot.plugins import SimplePluginManager
|
||||
from tunebot.redis import GlobalRedisAutoJoin
|
||||
from tunebot.redis import GlobalRedisPlaylist
|
||||
from tunebot.redis import GlobalRedisPlaylistSource
|
||||
from tunebot.redis import GlobalRedisUtils
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from tunebot import PluginManagerBase
|
||||
from tunebot import GlobalPlaylist
|
||||
from tunebot import GlobalPlaylistSource
|
||||
from tunebot import GlobalAutoJoin
|
||||
from tunebot import PluginLoaderBase
|
||||
from tunebot import PluginManagerBase
|
||||
from tunebot import GlobalUtils
|
||||
|
||||
ColorDict = dict[str, "Color"]
|
||||
|
||||
|
||||
class TuneBot(commands.Bot):
|
||||
lavalink: lavalink.Client
|
||||
invite_link: str
|
||||
invite_link: str = ""
|
||||
initial_cog_names: list[str]
|
||||
colors: ColorDict
|
||||
|
||||
global_autojoin: "GlobalAutoJoin"
|
||||
global_playlist: "GlobalPlaylist"
|
||||
global_playlist_source: "GlobalPlaylistSource"
|
||||
global_utils: "GlobalUtils"
|
||||
|
||||
plugin_loader: "PluginLoaderBase"
|
||||
plugin_manager: "PluginManagerBase"
|
||||
|
||||
def __init__(self, config: Dict[Any, Any]):
|
||||
intents = discord.Intents(
|
||||
voice_states=True, guild_messages=True, guilds=True, messages=True
|
||||
voice_states=True,
|
||||
guild_messages=True,
|
||||
guilds=True,
|
||||
messages=True,
|
||||
members=True,
|
||||
)
|
||||
|
||||
self.rpc_is_help_message = True
|
||||
self.update_status.start()
|
||||
|
||||
self.config = config
|
||||
self.initial_cog_names: List[str] = self.config.get("cogs", [])
|
||||
self.colors: Dict[str, Color] = self.process_colours(config.get("colors", []))
|
||||
|
||||
self.initial_cog_names = self.config.get("cogs", [])
|
||||
self.colors = self.process_colours(config.get("colors", []))
|
||||
|
||||
self.redis_prefix = self.config["redis_prefix"]
|
||||
self._redis_client: Redis = aioredis.from_url(
|
||||
self.config["redis_url"], encoding="utf-8", decode_responses=True
|
||||
)
|
||||
|
||||
self.global_autojoin: GlobalAutoJoin = GlobalRedisAutoJoin(
|
||||
self._redis_client,
|
||||
self.redis_prefix,
|
||||
self.global_autojoin = GlobalRedisAutoJoin(
|
||||
self._redis_client, self.redis_prefix
|
||||
)
|
||||
self.global_playlist: GlobalPlaylist = GlobalRedisPlaylist(
|
||||
self._redis_client,
|
||||
self.redis_prefix,
|
||||
self.global_playlist = GlobalRedisPlaylist(
|
||||
self._redis_client, self.redis_prefix
|
||||
)
|
||||
self.global_playlist_source: GlobalPlaylistSource = GlobalRedisPlaylistSource(
|
||||
self._redis_client,
|
||||
self.redis_prefix,
|
||||
self.global_playlist_source = GlobalRedisPlaylistSource(
|
||||
self._redis_client, self.redis_prefix
|
||||
)
|
||||
self.global_utils = GlobalRedisUtils(self._redis_client, self.redis_prefix)
|
||||
|
||||
self.invite_link: str = ""
|
||||
self.plugin_loader = FileSystemPluginLoader(self)
|
||||
self.plugin_manager = SimplePluginManager(self)
|
||||
|
||||
slash_guilds = None
|
||||
if len(self.config["slash_command_guilds"]) > 0:
|
||||
@@ -79,30 +102,31 @@ class TuneBot(commands.Bot):
|
||||
case_insensitive=False,
|
||||
fetch_offline_members=False,
|
||||
intents=intents,
|
||||
slash_commands=True,
|
||||
slash_commands=False,
|
||||
slash_command_guilds=slash_guilds,
|
||||
)
|
||||
|
||||
self.loop.create_task(self.async_init())
|
||||
|
||||
async def async_init(self):
|
||||
await self.load_cogs(self.initial_cog_names)
|
||||
self.init_plugins()
|
||||
self.load_cogs(self.initial_cog_names)
|
||||
|
||||
async def prefix_callable(self, _, msg: Message) -> List[str]:
|
||||
return commands.when_mentioned_or(*self.config["prefixes"])(self, msg)
|
||||
|
||||
async def load_cogs(self, cog_names: Sequence[str]):
|
||||
def load_cogs(self, cog_names: Sequence[str]):
|
||||
for cog in cog_names:
|
||||
try:
|
||||
self.load_extension(cog)
|
||||
print(f"Succesfully loaded extension {cog}.")
|
||||
print(f"[✓] loaded extension: {cog}.")
|
||||
except (
|
||||
ExtensionNotFound,
|
||||
ExtensionAlreadyLoaded,
|
||||
NoEntryPointError,
|
||||
ExtensionFailed,
|
||||
) as e:
|
||||
print(f"Failed to load extension {cog}.\n\t{e}", file=sys.stderr)
|
||||
print(f"[x] failed loading extension: {cog}.\n\t{e}", file=sys.stderr)
|
||||
|
||||
async def on_ready(self):
|
||||
self.invite_link = f"https://discord.com/oauth2/authorize?client_id={self.user.id}&permissions=3230720&scope=bot%20applications.commands"
|
||||
@@ -117,7 +141,7 @@ class TuneBot(commands.Bot):
|
||||
ll["host"], ll["port"], ll["password"], ll["region"], ll["name"]
|
||||
)
|
||||
|
||||
def process_colours(self, colors: Dict[str, str]) -> Dict[str, Color]:
|
||||
def process_colours(self, colors: Dict[str, str]) -> ColorDict:
|
||||
colour_dict: Dict[str, Color] = {}
|
||||
for name, color in colors.items():
|
||||
colour_dict[name] = Color(int(color, 16))
|
||||
@@ -126,6 +150,19 @@ class TuneBot(commands.Bot):
|
||||
async def get_context(self, message: Message, *, cls=CustomContext):
|
||||
return await super().get_context(message, cls=cls)
|
||||
|
||||
def init_plugins(self):
|
||||
for plug_id, plug_conf in self.config["plugins"].items():
|
||||
if not plug_conf.get("enabled"):
|
||||
continue
|
||||
|
||||
try:
|
||||
plugin = self.plugin_loader.load_plugin(plug_conf)
|
||||
self.plugin_manager.enable_plugin(plug_id, plugin)
|
||||
print(f"[✓] loaded plugin: {plug_id}")
|
||||
except Exception as e:
|
||||
self.plugin_manager.remove_plugin(plug_id)
|
||||
print(f"[x] failed loading plugin: {plug_id}\n{e}")
|
||||
|
||||
@tasks.loop(seconds=30)
|
||||
async def update_status(self):
|
||||
await self.wait_until_ready()
|
||||
@@ -141,13 +178,6 @@ class TuneBot(commands.Bot):
|
||||
await self.change_presence(activity=activity)
|
||||
|
||||
|
||||
config_path = "config.json"
|
||||
if len(sys.argv) > 1:
|
||||
config_path = sys.argv[1]
|
||||
|
||||
config = json.load(open(config_path, "r", encoding="utf-8"))
|
||||
redis_prefix = config["redis_prefix"]
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
import uvloop
|
||||
@@ -157,5 +187,10 @@ if __name__ == "__main__":
|
||||
except ModuleNotFoundError:
|
||||
pass
|
||||
|
||||
config_path = "config.json"
|
||||
if len(sys.argv) > 1:
|
||||
config_path = sys.argv[1]
|
||||
|
||||
config = json.load(open(config_path, "r", encoding="utf-8"))
|
||||
token = config.pop("token")
|
||||
TuneBot(config).run(token, reconnect=True)
|
||||
|
||||
Reference in New Issue
Block a user