diff --git a/tuxbot/cogs/Admin/admin.py b/tuxbot/cogs/Admin/admin.py index 3cd2429..70ec062 100644 --- a/tuxbot/cogs/Admin/admin.py +++ b/tuxbot/cogs/Admin/admin.py @@ -11,7 +11,7 @@ from tuxbot.core.i18n import ( Translator, find_locale, get_locale_name, - available_locales, + list_locales, ) from tuxbot.core.utils.functions.extra import ( group_extra, @@ -57,13 +57,9 @@ class Admin(commands.Cog, name="Admin"): @_lang.command(name="list", aliases=["liste", "all", "view"]) async def _lang_list(self, ctx: ContextPlus): - description = "" - for key, value in available_locales.items(): - description += f":flag_{key[-2:].lower()}: {value[0]}\n" - e = discord.Embed( title=_("List of available locales: ", ctx, self.bot.config), - description=description, + description=list_locales, color=0x36393E, ) diff --git a/tuxbot/cogs/Custom/__init__.py b/tuxbot/cogs/Custom/__init__.py new file mode 100644 index 0000000..2673d03 --- /dev/null +++ b/tuxbot/cogs/Custom/__init__.py @@ -0,0 +1,19 @@ +from collections import namedtuple + +from .custom import Custom +from .config import CustomConfig, HAS_MODELS +from ...core.bot import Tux + +VersionInfo = namedtuple("VersionInfo", "major minor micro release_level") +version_info = VersionInfo(major=1, minor=0, micro=0, release_level="alpha") + +__version__ = "v{}.{}.{}-{}".format( + version_info.major, + version_info.minor, + version_info.micro, + version_info.release_level, +).replace("\n", "") + + +def setup(bot: Tux): + bot.add_cog(Custom(bot)) diff --git a/tuxbot/cogs/Custom/config.py b/tuxbot/cogs/Custom/config.py new file mode 100644 index 0000000..2f8e6c4 --- /dev/null +++ b/tuxbot/cogs/Custom/config.py @@ -0,0 +1,10 @@ +from structured_config import Structure + +HAS_MODELS = False + + +class CustomConfig(Structure): + pass + + +extra = {} diff --git a/tuxbot/cogs/Custom/custom.py b/tuxbot/cogs/Custom/custom.py new file mode 100644 index 0000000..fea5b0c --- /dev/null +++ b/tuxbot/cogs/Custom/custom.py @@ -0,0 +1,128 @@ +import logging + +import discord +from discord.ext import commands + +from tuxbot.cogs.Custom.functions.converters import AliasConvertor +from tuxbot.core.bot import Tux +from tuxbot.core.config import set_for_key, search_for +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__) + + +class Custom(commands.Cog, name="Custom"): + def __init__(self, bot: Tux): + self.bot = bot + + async def cog_command_error(self, ctx, error): + if isinstance(error, commands.BadArgument): + await ctx.send( + _( + str(error), + ctx, + self.bot.config, + ) + ) + + # ========================================================================= + # ========================================================================= + + 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 _get_aliases(self, ctx: ContextPlus) -> dict: + return search_for(self.bot.config.Users, ctx.author.id, "aliases") + + 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): + args = alias.split(" | ") + + command = args[0] + alias = args[1] + + user_aliases = await self._get_aliases(ctx) + + if alias in user_aliases.keys(): + return await ctx.send( + _( + "The alias `{alias}` is already defined " + "for the command `{command}`", + ctx, + self.bot.config, + ).format(alias=alias, command=user_aliases.get(alias)) + ) + + if command in user_aliases.values(): + return await ctx.send( + _( + "There is already an alias for `{command}` " + "which is `{alias}`", + ctx, + self.bot.config, + ).format( + command=command, + alias=list(user_aliases.keys())[ + list(user_aliases.values()).index(command) + ], + ) + ) + + user_aliases[alias] = command + + await self._save_alias(ctx, user_aliases) + + await ctx.send( + _( + "The alias `{alias}` for the command `{command}` " + "was successfully created", + ctx, + self.bot.config, + ).format(alias=alias, command=command) + ) diff --git a/tuxbot/cogs/Custom/functions/converters.py b/tuxbot/cogs/Custom/functions/converters.py new file mode 100644 index 0000000..302f548 --- /dev/null +++ b/tuxbot/cogs/Custom/functions/converters.py @@ -0,0 +1,25 @@ +from discord.ext import commands +from jishaku.models import copy_context_with + + +class AliasConvertor(commands.Converter): + async def convert(self, ctx, argument): + args = argument.split(" | ") + + if len(args) <= 1: + raise commands.BadArgument( + "Alias must be like `[command] | [alias]`" + ) + + command_ctx = await copy_context_with( + ctx, content=ctx.prefix + args[0] + ) + alias_ctx = await copy_context_with(ctx, content=ctx.prefix + args[1]) + + if command_ctx.command is None: + raise commands.BadArgument(f"Unknown command `{args[0]}`") + + if alias_ctx.command is not None: + raise commands.BadArgument(f"Command `{args[1]}` already exists") + + return argument diff --git a/tuxbot/cogs/Custom/locales/en-US.po b/tuxbot/cogs/Custom/locales/en-US.po new file mode 100644 index 0000000..25a4a1e --- /dev/null +++ b/tuxbot/cogs/Custom/locales/en-US.po @@ -0,0 +1,27 @@ +# English translations for Tuxbot-bot package. +# Copyright (C) 2020 THE Tuxbot-bot'S COPYRIGHT HOLDER +# This file is distributed under the same license as the Tuxbot-bot package. +# Automatically generated, 2020. +# +msgid "" +msgstr "" +"Project-Id-Version: Tuxbot-bot\n" +"Report-Msgid-Bugs-To: rick@gnous.eu\n" +"POT-Creation-Date: 2020-11-11 02:40+0100\n" +"PO-Revision-Date: 2020-06-10 00:38+0200\n" +"Last-Translator: Automatically generated\n" +"Language-Team: none\n" +"Language: en_US\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" + +#: tuxbot/cogs/Admin/admin.py:50 +#, python-brace-format +msgid "Locale changed to {lang} successfully" +msgstr "" + +#: tuxbot/cogs/Admin/admin.py:65 +msgid "List of available locales: " +msgstr "" diff --git a/tuxbot/cogs/Custom/locales/fr-FR.po b/tuxbot/cogs/Custom/locales/fr-FR.po new file mode 100644 index 0000000..2b14077 --- /dev/null +++ b/tuxbot/cogs/Custom/locales/fr-FR.po @@ -0,0 +1,28 @@ +# French translations for Tuxbot-bot package +# Traductions françaises du paquet Tuxbot-bot. +# Copyright (C) 2020 THE Tuxbot-bot'S COPYRIGHT HOLDER +# This file is distributed under the same license as the Tuxbot-bot package. +# Automatically generated, 2020. +# +msgid "" +msgstr "" +"Project-Id-Version: Tuxbot-bot\n" +"Report-Msgid-Bugs-To: rick@gnous.eu\n" +"POT-Creation-Date: 2020-11-11 02:40+0100\n" +"PO-Revision-Date: 2020-06-10 00:38+0200\n" +"Last-Translator: Automatically generated\n" +"Language-Team: none\n" +"Language: fr\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n > 1);\n" + +#: tuxbot/cogs/Admin/admin.py:50 +#, fuzzy, python-brace-format +msgid "Locale changed to {lang} successfully" +msgstr "Langue changée pour {lang} avec succès" + +#: tuxbot/cogs/Admin/admin.py:65 +msgid "List of available locales: " +msgstr "Liste des langues disponibles : " diff --git a/tuxbot/cogs/Custom/locales/messages.pot b/tuxbot/cogs/Custom/locales/messages.pot new file mode 100644 index 0000000..f2b176e --- /dev/null +++ b/tuxbot/cogs/Custom/locales/messages.pot @@ -0,0 +1,27 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the Tuxbot-bot package. +# FIRST AUTHOR , YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: Tuxbot-bot\n" +"Report-Msgid-Bugs-To: rick@gnous.eu\n" +"POT-Creation-Date: 2020-11-11 16:42+0100\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=CHARSET\n" +"Content-Transfer-Encoding: 8bit\n" + +#: tuxbot/cogs/Admin/admin.py:50 +#, python-brace-format +msgid "Locale changed to {lang} successfully" +msgstr "" + +#: tuxbot/cogs/Admin/admin.py:65 +msgid "List of available locales: " +msgstr "" diff --git a/tuxbot/cogs/Custom/models/__init__.py b/tuxbot/cogs/Custom/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tuxbot/core/bot.py b/tuxbot/core/bot.py index ca7bbbc..c5dd805 100644 --- a/tuxbot/core/bot.py +++ b/tuxbot/core/bot.py @@ -44,6 +44,7 @@ packages: List[str] = [ "tuxbot.cogs.Dev", "tuxbot.cogs.Utils", "tuxbot.cogs.Polls", + "tuxbot.cogs.Custom", ] @@ -230,7 +231,7 @@ class Tux(commands.AutoShardedBot): ctx: ContextPlus = await self.get_context(message) - if ctx is None or ctx.valid is False: + if ctx is None or not ctx.valid: self.dispatch("message_without_command", message) else: if ctx.command in search_for( diff --git a/tuxbot/core/config.py b/tuxbot/core/config.py index e96baa0..d4e04a8 100644 --- a/tuxbot/core/config.py +++ b/tuxbot/core/config.py @@ -33,7 +33,7 @@ class Config(Structure): blacklisted: bool = BoolField(False) class User(Structure): - aliases: List[dict] = [] + aliases: dict = {} locale: str = StrField("") blacklisted: bool = BoolField(False) diff --git a/tuxbot/core/i18n.py b/tuxbot/core/i18n.py index 69add4a..9a8143e 100644 --- a/tuxbot/core/i18n.py +++ b/tuxbot/core/i18n.py @@ -29,6 +29,15 @@ def find_locale(locale: str) -> str: raise NotImplementedError("This locale isn't implemented") +def list_locales() -> str: + description = "" + + for key, value in available_locales.items(): + description += f":flag_{key[-2:].lower()}: {value[0]}\n" + + return description + + def get_locale_name(locale: str) -> str: """Return the name of this `locale`""" return available_locales.get(find_locale(locale))[0]