feat(commands|Custom>alias;locale): add alias registering and locale as custom for each user
This commit is contained in:
parent
98b82e680e
commit
72fabf89b9
12 changed files with 278 additions and 8 deletions
|
@ -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,
|
||||
)
|
||||
|
||||
|
|
19
tuxbot/cogs/Custom/__init__.py
Normal file
19
tuxbot/cogs/Custom/__init__.py
Normal file
|
@ -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))
|
10
tuxbot/cogs/Custom/config.py
Normal file
10
tuxbot/cogs/Custom/config.py
Normal file
|
@ -0,0 +1,10 @@
|
|||
from structured_config import Structure
|
||||
|
||||
HAS_MODELS = False
|
||||
|
||||
|
||||
class CustomConfig(Structure):
|
||||
pass
|
||||
|
||||
|
||||
extra = {}
|
128
tuxbot/cogs/Custom/custom.py
Normal file
128
tuxbot/cogs/Custom/custom.py
Normal file
|
@ -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)
|
||||
)
|
25
tuxbot/cogs/Custom/functions/converters.py
Normal file
25
tuxbot/cogs/Custom/functions/converters.py
Normal file
|
@ -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
|
27
tuxbot/cogs/Custom/locales/en-US.po
Normal file
27
tuxbot/cogs/Custom/locales/en-US.po
Normal file
|
@ -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 ""
|
28
tuxbot/cogs/Custom/locales/fr-FR.po
Normal file
28
tuxbot/cogs/Custom/locales/fr-FR.po
Normal file
|
@ -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 : "
|
27
tuxbot/cogs/Custom/locales/messages.pot
Normal file
27
tuxbot/cogs/Custom/locales/messages.pot
Normal file
|
@ -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 <EMAIL@ADDRESS>, 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 <EMAIL@ADDRESS>\n"
|
||||
"Language-Team: LANGUAGE <LL@li.org>\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 ""
|
0
tuxbot/cogs/Custom/models/__init__.py
Normal file
0
tuxbot/cogs/Custom/models/__init__.py
Normal file
|
@ -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(
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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]
|
||||
|
|
Loading…
Reference in a new issue