2021-01-19 14:30:25 +01:00
|
|
|
import logging
|
2021-05-16 23:21:27 +02:00
|
|
|
from typing import List
|
2021-01-19 14:30:25 +01:00
|
|
|
|
|
|
|
import discord
|
|
|
|
from discord.ext import commands
|
|
|
|
|
|
|
|
from tuxbot.cogs.Custom.functions.converters import AliasConvertor
|
|
|
|
from tuxbot.core.bot import Tux
|
2021-02-16 19:28:30 +01:00
|
|
|
from tuxbot.core.config import set_for_key, search_for, set_if_none
|
2021-01-19 14:30:25 +01:00
|
|
|
from tuxbot.core.config import Config
|
|
|
|
from tuxbot.core.i18n import (
|
|
|
|
Translator,
|
|
|
|
find_locale,
|
|
|
|
get_locale_name,
|
|
|
|
list_locales,
|
|
|
|
)
|
|
|
|
from tuxbot.core.utils.functions.extra import (
|
|
|
|
group_extra,
|
|
|
|
ContextPlus,
|
|
|
|
)
|
|
|
|
|
|
|
|
log = logging.getLogger("tuxbot.cogs.Custom")
|
|
|
|
_ = Translator("Custom", __file__)
|
|
|
|
|
|
|
|
|
2021-04-20 17:12:38 +02:00
|
|
|
class Custom(commands.Cog):
|
2021-01-19 14:30:25 +01:00
|
|
|
def __init__(self, bot: Tux):
|
|
|
|
self.bot = bot
|
|
|
|
|
|
|
|
async def cog_command_error(self, ctx, error):
|
|
|
|
if isinstance(error, commands.BadArgument):
|
2021-01-20 17:29:16 +01:00
|
|
|
await ctx.send(_(str(error), ctx, self.bot.config))
|
2021-01-19 14:30:25 +01:00
|
|
|
|
|
|
|
# =========================================================================
|
|
|
|
# =========================================================================
|
|
|
|
|
2021-02-16 19:28:30 +01:00
|
|
|
async def _get_aliases(self, ctx: ContextPlus) -> dict:
|
|
|
|
return search_for(self.bot.config.Users, ctx.author.id, "aliases")
|
|
|
|
|
2021-01-19 14:30:25 +01:00
|
|
|
async def _save_lang(self, ctx: ContextPlus, lang: str) -> None:
|
|
|
|
set_for_key(
|
|
|
|
self.bot.config.Users, ctx.author.id, Config.User, locale=lang
|
|
|
|
)
|
|
|
|
|
|
|
|
async def _save_alias(self, ctx: ContextPlus, alias: dict) -> None:
|
|
|
|
set_for_key(
|
|
|
|
self.bot.config.Users, ctx.author.id, Config.User, alias=alias
|
|
|
|
)
|
|
|
|
|
|
|
|
# =========================================================================
|
|
|
|
# =========================================================================
|
|
|
|
|
|
|
|
@group_extra(name="custom", aliases=["perso"], deletable=True)
|
|
|
|
@commands.guild_only()
|
|
|
|
async def _custom(self, ctx: ContextPlus):
|
|
|
|
"""Manage custom settings."""
|
|
|
|
|
|
|
|
@_custom.command(name="locale", aliases=["langue", "lang"])
|
|
|
|
async def _custom_locale(self, ctx: ContextPlus, lang: str):
|
|
|
|
try:
|
|
|
|
await self._save_lang(ctx, find_locale(lang.lower()))
|
|
|
|
await ctx.send(
|
|
|
|
_(
|
|
|
|
"Locale changed for you to {lang} successfully",
|
|
|
|
ctx,
|
|
|
|
self.bot.config,
|
|
|
|
).format(lang=f"`{get_locale_name(lang).lower()}`")
|
|
|
|
)
|
|
|
|
except NotImplementedError:
|
|
|
|
e = discord.Embed(
|
|
|
|
title=_("List of available locales: ", ctx, self.bot.config),
|
|
|
|
description=list_locales,
|
|
|
|
color=0x36393E,
|
|
|
|
)
|
|
|
|
|
|
|
|
await ctx.send(embed=e)
|
|
|
|
|
|
|
|
@_custom.command(name="alias", aliases=["aliases"])
|
|
|
|
async def _custom_alias(self, ctx: ContextPlus, *, alias: AliasConvertor):
|
2021-05-16 23:21:27 +02:00
|
|
|
args: List[str] = str(alias).split(" | ")
|
2021-01-19 14:30:25 +01:00
|
|
|
|
|
|
|
command = args[0]
|
2021-04-20 17:12:38 +02:00
|
|
|
custom = args[1]
|
2021-01-19 14:30:25 +01:00
|
|
|
|
|
|
|
user_aliases = await self._get_aliases(ctx)
|
|
|
|
|
2021-02-16 19:28:30 +01:00
|
|
|
if not user_aliases:
|
|
|
|
set_if_none(self.bot.config.Users, ctx.author.id, Config.User)
|
|
|
|
user_aliases = await self._get_aliases(ctx)
|
|
|
|
|
2021-04-20 17:12:38 +02:00
|
|
|
if custom in user_aliases.keys():
|
2021-01-19 14:30:25 +01:00
|
|
|
return await ctx.send(
|
|
|
|
_(
|
|
|
|
"The alias `{alias}` is already defined "
|
|
|
|
"for the command `{command}`",
|
|
|
|
ctx,
|
|
|
|
self.bot.config,
|
2021-04-20 17:12:38 +02:00
|
|
|
).format(alias=custom, command=user_aliases.get(custom))
|
2021-01-19 14:30:25 +01:00
|
|
|
)
|
|
|
|
|
2021-04-20 17:12:38 +02:00
|
|
|
user_aliases[custom] = command
|
2021-01-19 14:30:25 +01:00
|
|
|
|
|
|
|
await self._save_alias(ctx, user_aliases)
|
|
|
|
|
|
|
|
await ctx.send(
|
|
|
|
_(
|
|
|
|
"The alias `{alias}` for the command `{command}` "
|
|
|
|
"was successfully created",
|
|
|
|
ctx,
|
|
|
|
self.bot.config,
|
2021-04-20 17:12:38 +02:00
|
|
|
).format(alias=custom, command=command)
|
2021-01-19 14:30:25 +01:00
|
|
|
)
|