YusarinBot/cogs/cogLocale.py

106 lines
4.2 KiB
Python
Raw Permalink Normal View History

2023-01-17 16:50:47 +02:00
from os import listdir
2023-04-19 15:23:41 +03:00
2023-01-17 16:50:47 +02:00
from discord import ApplicationContext, Option, SlashCommandGroup
from discord.ext import commands
2023-04-19 15:23:41 +03:00
2023-04-19 15:21:39 +03:00
from functions import (
appendLog,
getMsg,
guildConfGet,
guildConfReset,
guildConfSet,
loadJson,
makeEmbed,
strToColor,
)
2023-01-17 16:50:47 +02:00
2023-04-19 15:21:39 +03:00
# =========================================================================================================================
class CogLocale(commands.Cog):
2023-01-17 16:50:47 +02:00
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")
2023-04-19 15:21:39 +03:00
async def locale_set(self, ctx: ApplicationContext, language: Option(str, "One of the languages in list", choices=valid_locales)): # type: ignore
2023-01-17 16:50:47 +02:00
config = loadJson("config.json")
if ctx.guild is not None:
2023-04-19 15:21:39 +03:00
if language + ".json" in listdir(f"locale/"):
2023-01-17 16:50:47 +02:00
guildConfSet(ctx.guild, "locale", language)
appendLog(f"Server's locale is now set to {language}", ctx.guild)
2023-04-19 15:21:39 +03:00
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"]),
)
)
2023-01-17 16:50:47 +02:00
else:
valid_locales = []
files_locales = listdir(f"locale/")
for entry in files_locales:
valid_locales.append(entry.split(".")[:-1])
2023-04-19 15:21:39 +03:00
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"]),
)
)
2023-01-17 16:50:47 +02:00
else:
2023-04-19 15:21:39 +03:00
await ctx.respond(
embed=makeEmbed(
title=getMsg("dm_title", ctx.guild),
description=getMsg("dm_description", ctx.guild),
color=strToColor(config["color_error"]),
)
)
2023-01-17 16:50:47 +02:00
@locale.command(name="reset", description="Reset the bot's language in this guild")
2023-04-19 15:21:39 +03:00
async def locale_reset(self, ctx: ApplicationContext): # type: ignore
2023-01-17 16:50:47 +02:00
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)
2023-04-19 15:21:39 +03:00
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"]),
)
)
2023-01-17 16:50:47 +02:00
else:
2023-04-19 15:21:39 +03:00
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"]),
)
)
2023-01-17 16:50:47 +02:00
else:
2023-04-19 15:21:39 +03:00
await ctx.respond(
embed=makeEmbed(
title=getMsg("dm_title", ctx.guild),
description=getMsg("dm_description", ctx.guild),
color=strToColor(config["color_error"]),
)
)
# =========================================================================================================================