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"]), ) ) # =========================================================================================================================