From d9427d1863bd7282412cfda19b56c1c9d3a65ce0 Mon Sep 17 00:00:00 2001 From: Romain J Date: Mon, 16 Dec 2019 23:06:25 +0100 Subject: [PATCH] update(command|poll): first release --- cogs/basics.py | 16 ++++++------- cogs/poll.py | 47 +++++++++++++++++++++++---------------- cogs/utils/models/poll.py | 1 - requirements.txt | 4 +--- 4 files changed, 37 insertions(+), 31 deletions(-) diff --git a/cogs/basics.py b/cogs/basics.py index e1b3c95..ced1a86 100644 --- a/cogs/basics.py +++ b/cogs/basics.py @@ -10,7 +10,7 @@ from discord.ext import commands from bot import TuxBot from .utils.lang import Texts -from .utils.extra import commandExtra, groupExtra +from .utils.extra import commandExtra from tcp_latency import measure_latency @@ -29,7 +29,7 @@ class Basics(commands.Cog): @commandExtra(name='ping', category='basics', - description=Texts('commands').get('basics.ping')) + description=Texts('commands').get('basics._ping')) async def _ping(self, ctx: commands.Context): start = time.perf_counter() await ctx.trigger_typing() @@ -67,9 +67,9 @@ class Basics(commands.Cog): return total, file_amount - @commands.command(name='info', aliases=['about'], - category='basics', - description=Texts('commands').get('basics.info')) + @commandExtra(name='info', aliases=['about'], + category='basics', + description=Texts('commands').get('basics._info')) async def _info(self, ctx: commands.Context): proc = psutil.Process() lines, files = self.fetch_info() @@ -150,9 +150,9 @@ class Basics(commands.Cog): """---------------------------------------------------------------------""" - @commands.command(name='credits', aliases=['contributors', 'authors'], - category='basics', - description=Texts('commands').get('basics.credits')) + @commandExtra(name='credits', aliases=['contributors', 'authors'], + category='basics', + description=Texts('commands').get('basics._credits')) async def _credits(self, ctx: commands.Context): e = discord.Embed( title=Texts('basics', ctx).get('Contributors'), diff --git a/cogs/poll.py b/cogs/poll.py index 0dbe43e..1904a63 100644 --- a/cogs/poll.py +++ b/cogs/poll.py @@ -8,6 +8,7 @@ from yarl import URL from bot import TuxBot from .utils.lang import Texts from .utils.models import Poll, Responses +from .utils.extra import groupExtra from .utils import emotes as utils_emotes @@ -49,30 +50,27 @@ class Polls(commands.Cog): pass choice = utils_emotes.get_index(pld.emoji.name) - print(choice) - - response = self.bot.database.session.query(Poll) \ + responses = self.bot.database.session.query(Responses) \ .filter( Responses.poll_id == poll.id, Responses.user == pld.user_id, Responses.choice == choice ) - if response.count() != 0: - print("--pre delete--") - response = response.one() + if responses.count() != 0: + response = responses.first() self.bot.database.session.delete(response) - print("--post delete--") + self.bot.database.session.commit() else: - print("--pre add--") response = Responses( user=pld.user_id, poll_id=poll.id, choice=choice ) self.bot.database.session.add(response) - print("--post add--") - self.bot.database.session.commit() + self.bot.database.session.commit() + + await self.update_poll(poll.id) """---------------------------------------------------------------------""" @@ -89,7 +87,7 @@ class Polls(commands.Cog): e = discord.Embed(description=f"**{question}**") e.set_author( - name=self.bot.user if anonymous else ctx.author, + name=ctx.author, icon_url="https://cdn.gnous.eu/tuxbot/survey1.png" ) for i, response in enumerate(responses): @@ -97,7 +95,7 @@ class Polls(commands.Cog): name=f"{emotes[i]} __{response.capitalize()}__", value="**0** vote" ) - e.set_footer(text=f"ID: {poll_row.id}") + e.set_footer(text=f"ID: #{poll_row.id}") poll_row.channel_id = stmt.channel.id poll_row.message_id = stmt.id @@ -135,12 +133,19 @@ class Polls(commands.Cog): content = json.loads(poll.content) \ if isinstance(poll.content, str) \ else poll.content - responses = json.loads(poll.responses) \ - if isinstance(poll.responses, str) \ - else poll.responses + raw_responses = self.bot.database.session\ + .query(Responses)\ + .filter(Responses.poll_id == poll_id) + responses = {} + + for response in raw_responses.all(): + if responses.get(response.choice): + responses[response.choice] += 1 + else: + responses[response.choice] = 1 for i, field in enumerate(content.get('fields')): - responders = len(responses.get(str(i + 1))) + responders = responses.get(i, 0) chart_options.get('data') \ .get('labels') \ .append(field.get('name')[5:].replace('__', '')) @@ -174,12 +179,16 @@ class Polls(commands.Cog): poll.content = json.dumps(content) self.bot.database.session.commit() - @commands.group(name='sondage', aliases=['poll']) + @groupExtra(name='sondage', aliases=['poll'], + category='poll', + description=Texts('commands').get('poll._poll')) async def _poll(self, ctx: commands.Context): if ctx.invoked_subcommand is None: - ... + pass - @_poll.group(name='create', aliases=['new', 'nouveau']) + @_poll.group(name='create', aliases=['new', 'nouveau'], + category='poll', + description=Texts('commands').get('poll._poll_create')) async def _poll_create(self, ctx: commands.Context, *, poll: str): is_anonymous = '--anonyme' in poll poll = poll.replace('--anonyme', '') diff --git a/cogs/utils/models/poll.py b/cogs/utils/models/poll.py index 72e5f67..f173063 100644 --- a/cogs/utils/models/poll.py +++ b/cogs/utils/models/poll.py @@ -1,7 +1,6 @@ from . import Base from sqlalchemy import Column, Integer, BigInteger, JSON, ForeignKey, Boolean from sqlalchemy.orm import relationship -from werkzeug.security import generate_password_hash, check_password_hash class Poll(Base): diff --git a/requirements.txt b/requirements.txt index a288e29..3414aa6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,4 @@ sqlalchemy psycopg2 configparser psutil -tcp_latency -i18n -werkzeug \ No newline at end of file +tcp_latency \ No newline at end of file