From cfd59def7460c6d51a9ee55e7b6eac6e2e069662 Mon Sep 17 00:00:00 2001 From: Romain J Date: Thu, 12 Nov 2020 00:03:01 +0100 Subject: [PATCH] feat(commands:polls|Polls): start dev of poll command --- .idea/dictionaries/romain.xml | 3 ++ tuxbot/cogs/Admin/locales/messages.pot | 2 +- tuxbot/cogs/Admin/models/__init__.py | 2 +- tuxbot/cogs/Admin/models/alias.py | 4 +- .../cogs/Admin/models/{warn.py => warns.py} | 4 +- tuxbot/cogs/Polls/__init__.py | 19 +++++++ tuxbot/cogs/Polls/config.py | 10 ++++ tuxbot/cogs/Polls/locales/en-US.po | 18 +++++++ tuxbot/cogs/Polls/locales/fr-FR.po | 19 +++++++ tuxbot/cogs/Polls/locales/messages.pot | 18 +++++++ tuxbot/cogs/Polls/models/__init__.py | 2 + tuxbot/cogs/Polls/models/polls.py | 35 +++++++++++++ tuxbot/cogs/Polls/models/responses.py | 22 ++++++++ tuxbot/cogs/Polls/polls.py | 51 +++++++++++++++++++ tuxbot/cogs/Utils/locales/messages.pot | 2 +- tuxbot/core/bot.py | 5 +- tuxbot/core/utils/data_manager.py | 2 +- tuxbot/core/utils/functions/extra.py | 11 ++-- 18 files changed, 214 insertions(+), 15 deletions(-) rename tuxbot/cogs/Admin/models/{warn.py => warns.py} (87%) create mode 100644 tuxbot/cogs/Polls/__init__.py create mode 100644 tuxbot/cogs/Polls/config.py create mode 100644 tuxbot/cogs/Polls/locales/en-US.po create mode 100644 tuxbot/cogs/Polls/locales/fr-FR.po create mode 100644 tuxbot/cogs/Polls/locales/messages.pot create mode 100644 tuxbot/cogs/Polls/models/__init__.py create mode 100644 tuxbot/cogs/Polls/models/polls.py create mode 100644 tuxbot/cogs/Polls/models/responses.py create mode 100644 tuxbot/cogs/Polls/polls.py diff --git a/.idea/dictionaries/romain.xml b/.idea/dictionaries/romain.xml index 4def97a..698fc82 100644 --- a/.idea/dictionaries/romain.xml +++ b/.idea/dictionaries/romain.xml @@ -2,6 +2,7 @@ anglais + anonyme appdirs apres asctime @@ -30,6 +31,8 @@ rprint socketstats soit + sondage + sondages splt suivante systemd diff --git a/tuxbot/cogs/Admin/locales/messages.pot b/tuxbot/cogs/Admin/locales/messages.pot index 78bd0bf..f2b176e 100644 --- a/tuxbot/cogs/Admin/locales/messages.pot +++ b/tuxbot/cogs/Admin/locales/messages.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: Tuxbot-bot\n" "Report-Msgid-Bugs-To: rick@gnous.eu\n" -"POT-Creation-Date: 2020-11-11 02:44+0100\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" diff --git a/tuxbot/cogs/Admin/models/__init__.py b/tuxbot/cogs/Admin/models/__init__.py index f21a8b3..7b6f395 100644 --- a/tuxbot/cogs/Admin/models/__init__.py +++ b/tuxbot/cogs/Admin/models/__init__.py @@ -1,2 +1,2 @@ from .alias import * -from .warn import * +from .warns import * diff --git a/tuxbot/cogs/Admin/models/alias.py b/tuxbot/cogs/Admin/models/alias.py index bc1ba53..2ba0073 100644 --- a/tuxbot/cogs/Admin/models/alias.py +++ b/tuxbot/cogs/Admin/models/alias.py @@ -2,7 +2,7 @@ import tortoise from tortoise import fields -class AliasesModel(tortoise.Model): +class Alias(tortoise.Model): id = fields.BigIntField(pk=True) user_id = fields.BigIntField() alias = fields.TextField(max_length=255) @@ -14,7 +14,7 @@ class AliasesModel(tortoise.Model): def __str__(self): return ( - f" 1);\n" diff --git a/tuxbot/cogs/Polls/locales/messages.pot b/tuxbot/cogs/Polls/locales/messages.pot new file mode 100644 index 0000000..9c11528 --- /dev/null +++ b/tuxbot/cogs/Polls/locales/messages.pot @@ -0,0 +1,18 @@ +# 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-10-21 01:15+0200\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" diff --git a/tuxbot/cogs/Polls/models/__init__.py b/tuxbot/cogs/Polls/models/__init__.py new file mode 100644 index 0000000..e622fc8 --- /dev/null +++ b/tuxbot/cogs/Polls/models/__init__.py @@ -0,0 +1,2 @@ +from .polls import * +from .responses import * diff --git a/tuxbot/cogs/Polls/models/polls.py b/tuxbot/cogs/Polls/models/polls.py new file mode 100644 index 0000000..dfe82c4 --- /dev/null +++ b/tuxbot/cogs/Polls/models/polls.py @@ -0,0 +1,35 @@ +import tortoise +from tortoise import fields + + +class Poll(tortoise.Model): + id = fields.BigIntField(pk=True) + channel_id = fields.BigIntField() + message_id = fields.BigIntField() + author_id = fields.BigIntField() + + content = fields.JSONField() + is_anonymous = fields.BooleanField() + + available_choices = fields.IntField() + + choices: fields.ManyToManyRelation["Response"] = fields.ManyToManyField( + "models.Response", related_name="choices" + ) + + class Meta: + table = "polls" + + def __str__(self): + return ( + f"" + ) + + __repr__ = __str__ diff --git a/tuxbot/cogs/Polls/models/responses.py b/tuxbot/cogs/Polls/models/responses.py new file mode 100644 index 0000000..9f2834c --- /dev/null +++ b/tuxbot/cogs/Polls/models/responses.py @@ -0,0 +1,22 @@ +import tortoise +from tortoise import fields + + +class Response(tortoise.Model): + response_id = fields.BigIntField(pk=True) + poll = fields.ForeignKeyField("models.Poll") + user_id = fields.BigIntField() + + choice = fields.IntField() + + class Meta: + table = "responses" + + def __str__(self): + return ( + f"" + ) + + __repr__ = __str__ diff --git a/tuxbot/cogs/Polls/polls.py b/tuxbot/cogs/Polls/polls.py new file mode 100644 index 0000000..21332ab --- /dev/null +++ b/tuxbot/cogs/Polls/polls.py @@ -0,0 +1,51 @@ +import logging + +from discord.ext import commands + +from tuxbot.core.utils.functions.extra import ContextPlus, group_extra +from tuxbot.core.bot import Tux +from tuxbot.core.i18n import ( + Translator, +) + +log = logging.getLogger("tuxbot.cogs.Polls") +_ = Translator("Polls", __file__) + + +class Polls(commands.Cog, name="Polls"): + def __init__(self, bot: Tux): + self.bot = bot + + # ========================================================================= + # ========================================================================= + + @group_extra(name="polls", aliases=["poll", "sondages", "sondage"]) + async def _polls(self, ctx: ContextPlus, *, message): + if ctx.invoked_subcommand is None: + args: list = message.lower().split() + is_anonymous = False + + if "--anonymous" in args: + is_anonymous = True + args.remove("--anonymous") + elif "--anonyme" in args: + is_anonymous = True + args.remove("--anonyme") + + if args[-1] != "|": + args.append("|") + + delimiters = [i for i, val in enumerate(args) if val == "|"] + + question = " ".join(args[0 : delimiters[0]]).capitalize() + answers = [] + + for i in range(len(delimiters) - 1): + start = delimiters[i] + 1 + end = delimiters[i + 1] + + answers.append(" ".join(args[start:end]).capitalize()) + + await ctx.send( + f"{message=}\n{question=}\n{answers=}\n{is_anonymous=}" + ) diff --git a/tuxbot/cogs/Utils/locales/messages.pot b/tuxbot/cogs/Utils/locales/messages.pot index 3e3e5d8..31a4f39 100644 --- a/tuxbot/cogs/Utils/locales/messages.pot +++ b/tuxbot/cogs/Utils/locales/messages.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: Tuxbot-bot\n" "Report-Msgid-Bugs-To: rick@gnous.eu\n" -"POT-Creation-Date: 2020-11-11 02:44+0100\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" diff --git a/tuxbot/core/bot.py b/tuxbot/core/bot.py index c7c9bd9..ca7bbbc 100644 --- a/tuxbot/core/bot.py +++ b/tuxbot/core/bot.py @@ -43,6 +43,7 @@ packages: List[str] = [ "tuxbot.cogs.Logs", "tuxbot.cogs.Dev", "tuxbot.cogs.Utils", + "tuxbot.cogs.Polls", ] @@ -172,9 +173,9 @@ class Tux(commands.AutoShardedBot): ) for extension in packages: if extension in self.extensions: - status = f"[green]:heavy_check_mark: {extension}" + status = f"[green]:heavy_check_mark: {extension} " else: - status = f"[red]:heavy_multiplication_x: {extension}" + status = f"[red]:heavy_multiplication_x: {extension} " table.add_row(status) columns.add_renderable(table) diff --git a/tuxbot/core/utils/data_manager.py b/tuxbot/core/utils/data_manager.py index a28a824..ad8eafd 100644 --- a/tuxbot/core/utils/data_manager.py +++ b/tuxbot/core/utils/data_manager.py @@ -37,7 +37,7 @@ def logs_data_path(instance_name: str) -> Path: Path Generated path for Logs files. """ - return data_path(instance_name) / "Logs" + return data_path(instance_name) / "logs" def cogs_data_path(instance_name: str, cog_name: str = "") -> Path: diff --git a/tuxbot/core/utils/functions/extra.py b/tuxbot/core/utils/functions/extra.py index 1b79911..cd0d26a 100644 --- a/tuxbot/core/utils/functions/extra.py +++ b/tuxbot/core/utils/functions/extra.py @@ -1,15 +1,16 @@ import asyncio +import random import discord from discord import Embed -from discord.ext import commands, flags +from discord.ext import commands from rich.console import Console console = Console() -TOKEN_REPLACEMENT = "whoops, leaked token" -PASSWORD_REPLACEMENT = "whoops, leaked password" +TOKEN_REPLACEMENT = "\*" * random.randint(3, 15) +PASSWORD_REPLACEMENT = "\*" * random.randint(3, 15) class ContextPlus(commands.Context): @@ -88,7 +89,7 @@ class ContextPlus(commands.Context): ) -class CommandPLus(flags.FlagCommand): +class CommandPLus(commands.Command): def __init__(self, function, **kwargs): super().__init__(function, **kwargs) self.deletable = kwargs.pop("deletable", True) @@ -98,7 +99,7 @@ def command_extra(*args, **kwargs): return commands.command(*args, **kwargs, cls=CommandPLus) -class GroupPlus(flags.FlagGroup): +class GroupPlus(commands.Group): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.deletable = kwargs.pop("deletable", True)