From e56268543c8c71ecc51ca4295c111f6adb63f4a2 Mon Sep 17 00:00:00 2001 From: profitroll Date: Tue, 17 Jan 2023 15:50:47 +0100 Subject: [PATCH] Migrated to cogs --- cogs/cogCategory.py | 35 ++++++++ cogs/cogChannel.py | 75 ++++++++++++++++++ cogs/cogLocale.py | 48 +++++++++++ cogs/cogUtility.py | 40 ++++++++++ yusarin.py | 189 ++++---------------------------------------- 5 files changed, 213 insertions(+), 174 deletions(-) create mode 100644 cogs/cogCategory.py create mode 100644 cogs/cogChannel.py create mode 100644 cogs/cogLocale.py create mode 100644 cogs/cogUtility.py diff --git a/cogs/cogCategory.py b/cogs/cogCategory.py new file mode 100644 index 0000000..700aab5 --- /dev/null +++ b/cogs/cogCategory.py @@ -0,0 +1,35 @@ +from discord import ApplicationContext, Option, SlashCommandGroup, CategoryChannel +from discord.ext import commands +from functions import getMsg, guildConfGet, guildConfReset, guildConfSet, loadJson, makeEmbed, strToColor + +#========================================================================================================================= +class CogCategory(commands.Cog): + + def __init__(self, client): + self.client = client + + category = SlashCommandGroup("category", "Commands related to parent channels category") + + @category.command(name="set", description="Select the voice channel that will be parent to private ones") + async def category_set(self, ctx: ApplicationContext, category: Option(CategoryChannel, "Parent Channel Category")): # type: ignore + config = loadJson("config.json") + if ctx.guild is not None: + guildConfSet(ctx.guild, "category", category.id) + await ctx.respond(embed=makeEmbed(title=getMsg("set_category_title", ctx.guild), description=getMsg("set_category_description", ctx.guild).format(category.name), color=strToColor(config["color_ok"]))) + if guildConfGet(ctx.guild, "channel") is None: + await ctx.respond(embed=makeEmbed(title=getMsg("hint_none_channel_title", ctx.guild), description=getMsg("hint_none_channel_description", ctx.guild), color=strToColor(config["color_warn"]))) + else: + await ctx.respond(embed=makeEmbed(title=getMsg("dm_title", ctx.guild), description=getMsg("dm_description", ctx.guild), color=strToColor(config["color_error"]))) + + @category.command(name="reset", description="Reset the currently selected parent channel category") + async def category_reset(self, ctx: ApplicationContext): # type: ignore + config = loadJson("config.json") + if ctx.guild is not None: + if guildConfGet(ctx.guild, "category") is not None: + guildConfReset(ctx.guild, "category") + await ctx.respond(embed=makeEmbed(title=getMsg("reset_category_title", ctx.guild), description=getMsg("reset_category_description", ctx.guild), color=strToColor(config["color_ok"]))) + else: + await ctx.respond(embed=makeEmbed(title=getMsg("hint_none_category_title", ctx.guild), description=getMsg("hint_none_category_description", ctx.guild), color=strToColor(config["color_warn"]))) + else: + await ctx.respond(embed=makeEmbed(title=getMsg("dm_title", ctx.guild), description=getMsg("dm_description", ctx.guild), color=strToColor(config["color_error"]))) +#========================================================================================================================= \ No newline at end of file diff --git a/cogs/cogChannel.py b/cogs/cogChannel.py new file mode 100644 index 0000000..91e74e9 --- /dev/null +++ b/cogs/cogChannel.py @@ -0,0 +1,75 @@ +from discord import ApplicationContext, SlashCommandGroup, VoiceChannel, Member, VoiceState, Cog, option, utils +from discord.ext import commands +from functions import appendLog, changeNomicPerms, createUserVoice, getMsg, guildConfGet, guildConfReset, guildConfSet, isUserVoice, isVoiceOfUser, loadJson, makeEmbed, removeUserVoice, strToColor + +#========================================================================================================================= +class CogChannel(commands.Cog): + + def __init__(self, client): + self.client = client + + @Cog.listener() + async def on_voice_state_update(self, member: Member, before: VoiceState, after: VoiceState): + + config = loadJson("config.json") + + vc_from = before.channel + vc_to = after.channel + + # If user left vc + if vc_to is None: + if isUserVoice(vc_from): + if isVoiceOfUser(vc_from, member): + await removeUserVoice(vc_from) + return + else: + if loadJson("config.json")["enable_nomic"]: + await changeNomicPerms("deny", vc_from, member) + + # If user joined vc + else: + if isUserVoice(vc_from): + if isVoiceOfUser(vc_from, member): + await removeUserVoice(vc_from) + else: + if loadJson("config.json")["enable_nomic"]: + await changeNomicPerms("deny", vc_from, member) + if isUserVoice(vc_to): + if loadJson("config.json")["enable_nomic"]: + await changeNomicPerms("allow", vc_to, member) + if vc_to.id == guildConfGet(vc_to.guild, "channel"): + if guildConfGet(vc_to.guild, "category") is not None: + voice_chan = await createUserVoice(vc_to, utils.get(vc_to.guild.categories, id=guildConfGet(vc_to.guild, "category")), member) + try: + await member.move_to(voice_chan) + except: + await removeUserVoice(voice_chan) + else: + appendLog(f"Category for guild {vc_to.guild} ({str(vc_to.guild.id)}) is not set", guild=vc_to.guild) + + channel = SlashCommandGroup("channel", "Commands related to parent voice channel") + + @channel.command(name="set", description="Select the voice channel that will be parent to private ones") + @option("channel", description="Parent Voice Channel") + async def channel_set(self, ctx: ApplicationContext, channel: VoiceChannel): + config = loadJson("config.json") + if ctx.guild is not None: + guildConfSet(ctx.guild, "channel", channel.id) + await ctx.respond(embed=makeEmbed(title=getMsg("set_channel_title", ctx.guild), description=getMsg("set_channel_description", ctx.guild).format(channel.name), color=strToColor(config["color_ok"]))) + if guildConfGet(ctx.guild, "category") is None: + await ctx.respond(embed=makeEmbed(title=getMsg("hint_none_category_title", ctx.guild), description=getMsg("hint_none_category_description", ctx.guild), color=strToColor(config["color_warn"]))) + else: + await ctx.respond(embed=makeEmbed(title=getMsg("dm_title", ctx.guild), description=getMsg("dm_description", ctx.guild), color=strToColor(config["color_error"]))) + + @channel.command(name="reset", description="Reset the currently selected parent voice channel") + async def channel_reset(self, ctx: ApplicationContext): + config = loadJson("config.json") + if ctx.guild is not None: + if guildConfGet(ctx.guild, "channel") is not None: + guildConfReset(ctx.guild, "channel") + await ctx.respond(embed=makeEmbed(title=getMsg("reset_channel_title", ctx.guild), description=getMsg("reset_channel_description", ctx.guild), color=strToColor(config["color_ok"]))) + else: + await ctx.respond(embed=makeEmbed(title=getMsg("hint_none_channel_title", ctx.guild), description=getMsg("hint_none_channel_description", ctx.guild), color=strToColor(config["color_warn"]))) + else: + await ctx.respond(embed=makeEmbed(title=getMsg("dm_title", ctx.guild), description=getMsg("dm_description", ctx.guild), color=strToColor(config["color_error"]))) +#========================================================================================================================= \ No newline at end of file diff --git a/cogs/cogLocale.py b/cogs/cogLocale.py new file mode 100644 index 0000000..e556fdd --- /dev/null +++ b/cogs/cogLocale.py @@ -0,0 +1,48 @@ +from os import listdir +from discord import ApplicationContext, Option, SlashCommandGroup +from discord.ext import commands +from functions import appendLog, getMsg, guildConfGet, guildConfReset, guildConfSet, loadJson, makeEmbed, strToColor + +#========================================================================================================================= +class CogLocale(commands.Cog): + + def __init__(self, client): + self.client = client + + locale = SlashCommandGroup("locale", "Commands related to bot's locale") + + valid_locales = [] + files_locales = listdir(f"locale/") + for entry in files_locales: + valid_locales.append(".".join(entry.split(".")[:-1])) + + @locale.command(name="set", description="Set bot's messages language") + async def locale_set(self, ctx: ApplicationContext, language: Option(str, "One of the languages in list", choices=valid_locales)): # type: ignore + config = loadJson("config.json") + if ctx.guild is not None: + if language+".json" in listdir(f"locale/"): + guildConfSet(ctx.guild, "locale", language) + appendLog(f"Server's locale is now set to {language}", ctx.guild) + await ctx.respond(embed=makeEmbed(title=getMsg("set_locale_title", ctx.guild), description=getMsg("set_locale_description", ctx.guild).format(getMsg("locale_name", ctx.guild)), color=strToColor(config["color_ok"]))) + else: + valid_locales = [] + files_locales = listdir(f"locale/") + for entry in files_locales: + valid_locales.append(entry.split(".")[:-1]) + await ctx.respond(embed=makeEmbed(title=getMsg("error_locale_title", ctx.guild), description=getMsg("error_locale_description", ctx.guild).format(", ".join(valid_locales)), color=strToColor(config["color_error"]))) + else: + await ctx.respond(embed=makeEmbed(title=getMsg("dm_title", ctx.guild), description=getMsg("dm_description", ctx.guild), color=strToColor(config["color_error"]))) + + @locale.command(name="reset", description="Reset the bot's language in this guild") + async def locale_reset(self, ctx: ApplicationContext): # type: ignore + config = loadJson("config.json") + if ctx.guild is not None: + if guildConfGet(ctx.guild, "locale") is not None: + guildConfReset(ctx.guild, "locale") + appendLog(f"Server's locale has been reset", ctx.guild) + await ctx.respond(embed=makeEmbed(title=getMsg("reset_locale_title", ctx.guild), description=getMsg("reset_locale_description", ctx.guild).format(getMsg("locale_name", ctx.guild)), color=strToColor(config["color_ok"]))) + else: + await ctx.respond(embed=makeEmbed(title=getMsg("hint_none_locale_title", ctx.guild), description=getMsg("hint_none_locale_description", ctx.guild).format(getMsg("locale_name", ctx.guild)), color=strToColor(config["color_warn"]))) + else: + await ctx.respond(embed=makeEmbed(title=getMsg("dm_title", ctx.guild), description=getMsg("dm_description", ctx.guild), color=strToColor(config["color_error"]))) +#========================================================================================================================= \ No newline at end of file diff --git a/cogs/cogUtility.py b/cogs/cogUtility.py new file mode 100644 index 0000000..a5dff61 --- /dev/null +++ b/cogs/cogUtility.py @@ -0,0 +1,40 @@ +from os import getpid, makedirs, system +from shutil import rmtree +from discord import ApplicationContext, slash_command, Cog, Guild +from discord.ext import commands +from functions import appendLog, getMsg, loadJson, makeEmbed, saveJson, strToColor + +#========================================================================================================================= +class CogUtility(commands.Cog): + + def __init__(self, client): + self.client = client + + @Cog.listener() + async def on_guild_join(self, guild: Guild): + + makedirs(f"guilds/{str(guild.id)}", exist_ok=True) + makedirs(f"guilds/{str(guild.id)}/channels", exist_ok=True) + saveJson({}, f"guilds/{str(guild.id)}/config.json") + + appendLog(f"Joined guild '{guild}' with id {str(guild.id)}") + + @Cog.listener() + async def on_guild_remove(self, guild: Guild): + + try: + rmtree(f"guilds/{str(guild.id)}") + except: + pass + + appendLog(f"Left guild '{guild}' with id {str(guild.id)}") + + @slash_command(name="shutdown", description="Restart the bot") + async def shutdown(self, ctx: ApplicationContext): + config = loadJson("config.json") + if ctx.author.id == config["owner"]: + await ctx.respond(embed=makeEmbed(description=getMsg("shutdown", ctx.guild).format(ctx.author), color=strToColor(config["color_default"]))) + system(f"kill -9 {str(getpid())}") + else: + await ctx.respond(embed=makeEmbed(title=getMsg("admin_title", ctx.guild), description=getMsg("admin_description", ctx.guild), color=strToColor(config["color_error"]))) +#========================================================================================================================= \ No newline at end of file diff --git a/yusarin.py b/yusarin.py index b16a12f..7aafc2d 100644 --- a/yusarin.py +++ b/yusarin.py @@ -1,11 +1,11 @@ try: from requests import get - from discord import ApplicationContext, Option, Intents, Bot, ActivityType, Activity + from discord import ApplicationContext, Intents, Bot, ActivityType, Activity, VoiceState except Exception as exp: print(f"Dependencies not installed. Make sure to run 'pip install -r requirements.txt' before first start") exit() -from os import getpid, system +from os import getpid from shutil import rmtree from functions import * @@ -28,6 +28,15 @@ if loadJson("config.json")["check_for_updates"]: intents = Intents().all() client = Bot(intents=intents) +from cogs.cogCategory import CogCategory +from cogs.cogChannel import CogChannel +from cogs.cogLocale import CogLocale +from cogs.cogUtility import CogUtility + +@client.slash_command(name="help", description="Get information about this server") +async def help(ctx: ApplicationContext): + await ctx.respond(embed=getHelpMessage(ctx, version)) + @client.event async def on_ready(): @@ -39,178 +48,10 @@ async def on_ready(): await clearTrash(client) -@client.event -async def on_guild_join(guild): - - makedirs(f"guilds/{str(guild.id)}", exist_ok=True) - makedirs(f"guilds/{str(guild.id)}/channels", exist_ok=True) - saveJson({}, f"guilds/{str(guild.id)}/config.json") - - appendLog(f"Joined guild '{guild}' with id {str(guild.id)}") - -@client.event -async def on_guild_remove(guild): - - try: - rmtree(f"guilds/{str(guild.id)}") - except: - pass - - appendLog(f"Left guild '{guild}' with id {str(guild.id)}") - -@client.event -async def on_voice_state_update(member, before, after): - - global debug - - config = loadJson("config.json") - - vc_from = before.channel - vc_to = after.channel - - # If user left vc - if vc_to is None: - if isUserVoice(vc_from): - if isVoiceOfUser(vc_from, member): - await removeUserVoice(vc_from) - return - else: - if loadJson("config.json")["enable_nomic"]: - await changeNomicPerms("deny", vc_from, member) - - # If user joined vc - else: - if isUserVoice(vc_from): - if isVoiceOfUser(vc_from, member): - await removeUserVoice(vc_from) - else: - if loadJson("config.json")["enable_nomic"]: - await changeNomicPerms("deny", vc_from, member) - if isUserVoice(vc_to): - if loadJson("config.json")["enable_nomic"]: - await changeNomicPerms("allow", vc_to, member) - if vc_to.id == guildConfGet(vc_to.guild, "channel"): - if guildConfGet(vc_to.guild, "category") is not None: - voice_chan = await createUserVoice(vc_to, utils.get(vc_to.guild.categories, id=guildConfGet(vc_to.guild, "category")), member) - try: - await member.move_to(voice_chan) - except: - await removeUserVoice(voice_chan) - else: - if debug: - appendLog(f"Category for guild {vc_to.guild} ({str(vc_to.guild.id)}) is not set", guild=vc_to.guild) - else: - appendLog(f"Category for guild {vc_to.guild} is not set", guild=vc_to.guild) - -#========================================================================================================================= -@client.slash_command(name="shutdown", description="Restart the bot") -async def shutdown(ctx: ApplicationContext): - config = loadJson("config.json") - if ctx.author.id == config["owner"]: - await ctx.respond(embed=makeEmbed(description=getMsg("shutdown", ctx.guild).format(ctx.author), color=strToColor(config["color_default"]))) - system(f"kill -9 {str(pid)}") - else: - await ctx.respond(embed=makeEmbed(title=getMsg("admin_title", ctx.guild), description=getMsg("admin_description", ctx.guild), color=strToColor(config["color_error"]))) -#========================================================================================================================= - -#========================================================================================================================= -@client.slash_command(name="help", description="Get information about this server") -async def help(ctx: ApplicationContext): - await ctx.respond(embed=getHelpMessage(ctx, version)) -#========================================================================================================================= - -#========================================================================================================================= -locale = client.create_group("locale", "Commands related to bot's locale") - -valid_locales = [] -files_locales = listdir(f"locale/") -for entry in files_locales: - valid_locales.append(".".join(entry.split(".")[:-1])) - -@locale.command(name="set", description="Set bot's messages language") -async def locale_set(ctx: ApplicationContext, language: Option(str, "One of the languages in list", choices=valid_locales)): # type: ignore - config = loadJson("config.json") - if ctx.guild is not None: - if language+".json" in listdir(f"locale/"): - guildConfSet(ctx.guild, "locale", language) - appendLog(f"Server's locale is now set to {language}", ctx.guild) - await ctx.respond(embed=makeEmbed(title=getMsg("set_locale_title", ctx.guild), description=getMsg("set_locale_description", ctx.guild).format(getMsg("locale_name", ctx.guild)), color=strToColor(config["color_ok"]))) - else: - valid_locales = [] - files_locales = listdir(f"locale/") - for entry in files_locales: - valid_locales.append(entry.split(".")[:-1]) - await ctx.respond(embed=makeEmbed(title=getMsg("error_locale_title", ctx.guild), description=getMsg("error_locale_description", ctx.guild).format(", ".join(valid_locales)), color=strToColor(config["color_error"]))) - else: - await ctx.respond(embed=makeEmbed(title=getMsg("dm_title", ctx.guild), description=getMsg("dm_description", ctx.guild), color=strToColor(config["color_error"]))) - -@locale.command(name="reset", description="Reset the bot's language in this guild") -async def locale_reset(ctx: ApplicationContext): # type: ignore - config = loadJson("config.json") - if ctx.guild is not None: - if guildConfGet(ctx.guild, "locale") is not None: - guildConfReset(ctx.guild, "locale") - appendLog(f"Server's locale has been reset", ctx.guild) - await ctx.respond(embed=makeEmbed(title=getMsg("reset_locale_title", ctx.guild), description=getMsg("reset_locale_description", ctx.guild).format(getMsg("locale_name", ctx.guild)), color=strToColor(config["color_ok"]))) - else: - await ctx.respond(embed=makeEmbed(title=getMsg("hint_none_locale_title", ctx.guild), description=getMsg("hint_none_locale_description", ctx.guild).format(getMsg("locale_name", ctx.guild)), color=strToColor(config["color_warn"]))) - else: - await ctx.respond(embed=makeEmbed(title=getMsg("dm_title", ctx.guild), description=getMsg("dm_description", ctx.guild), color=strToColor(config["color_error"]))) -#========================================================================================================================= - -#========================================================================================================================= -channel = client.create_group("channel", "Commands related to parent voice channel") - -@channel.command(name="set", description="Select the voice channel that will be parent to private ones") -async def channel_set(ctx: ApplicationContext, channel: Option(VoiceChannel, "Parent Voice Channel")): # type: ignore - config = loadJson("config.json") - if ctx.guild is not None: - guildConfSet(ctx.guild, "channel", channel.id) - await ctx.respond(embed=makeEmbed(title=getMsg("set_channel_title", ctx.guild), description=getMsg("set_channel_description", ctx.guild).format(channel.name), color=strToColor(config["color_ok"]))) - if guildConfGet(ctx.guild, "category") is None: - await ctx.respond(embed=makeEmbed(title=getMsg("hint_none_category_title", ctx.guild), description=getMsg("hint_none_category_description", ctx.guild), color=strToColor(config["color_warn"]))) - else: - await ctx.respond(embed=makeEmbed(title=getMsg("dm_title", ctx.guild), description=getMsg("dm_description", ctx.guild), color=strToColor(config["color_error"]))) - -@channel.command(name="reset", description="Reset the currently selected parent voice channel") -async def channel_reset(ctx: ApplicationContext): # type: ignore - config = loadJson("config.json") - if ctx.guild is not None: - if guildConfGet(ctx.guild, "channel") is not None: - guildConfReset(ctx.guild, "channel") - await ctx.respond(embed=makeEmbed(title=getMsg("reset_channel_title", ctx.guild), description=getMsg("reset_channel_description", ctx.guild), color=strToColor(config["color_ok"]))) - else: - await ctx.respond(embed=makeEmbed(title=getMsg("hint_none_channel_title", ctx.guild), description=getMsg("hint_none_channel_description", ctx.guild), color=strToColor(config["color_warn"]))) - else: - await ctx.respond(embed=makeEmbed(title=getMsg("dm_title", ctx.guild), description=getMsg("dm_description", ctx.guild), color=strToColor(config["color_error"]))) -#========================================================================================================================= - -#========================================================================================================================= -category = client.create_group("category", "Commands related to parent channels category") - -@category.command(name="set", description="Select the voice channel that will be parent to private ones") -async def category_set(ctx: ApplicationContext, category: Option(CategoryChannel, "Parent Channel Category")): # type: ignore - config = loadJson("config.json") - if ctx.guild is not None: - guildConfSet(ctx.guild, "category", category.id) - await ctx.respond(embed=makeEmbed(title=getMsg("set_category_title", ctx.guild), description=getMsg("set_category_description", ctx.guild).format(category.name), color=strToColor(config["color_ok"]))) - if guildConfGet(ctx.guild, "channel") is None: - await ctx.respond(embed=makeEmbed(title=getMsg("hint_none_channel_title", ctx.guild), description=getMsg("hint_none_channel_description", ctx.guild), color=strToColor(config["color_warn"]))) - else: - await ctx.respond(embed=makeEmbed(title=getMsg("dm_title", ctx.guild), description=getMsg("dm_description", ctx.guild), color=strToColor(config["color_error"]))) - -@category.command(name="reset", description="Reset the currently selected parent channel category") -async def category_reset(ctx: ApplicationContext): # type: ignore - config = loadJson("config.json") - if ctx.guild is not None: - if guildConfGet(ctx.guild, "category") is not None: - guildConfReset(ctx.guild, "category") - await ctx.respond(embed=makeEmbed(title=getMsg("reset_category_title", ctx.guild), description=getMsg("reset_category_description", ctx.guild), color=strToColor(config["color_ok"]))) - else: - await ctx.respond(embed=makeEmbed(title=getMsg("hint_none_category_title", ctx.guild), description=getMsg("hint_none_category_description", ctx.guild), color=strToColor(config["color_warn"]))) - else: - await ctx.respond(embed=makeEmbed(title=getMsg("dm_title", ctx.guild), description=getMsg("dm_description", ctx.guild), color=strToColor(config["color_error"]))) -#========================================================================================================================= +client.add_cog(CogCategory(client)) +client.add_cog(CogChannel(client)) +client.add_cog(CogLocale(client)) +client.add_cog(CogUtility(client)) appendLog(f"Trying to log in...") client.run(loadJson("config.json")["bot_token"]) \ No newline at end of file