update(command|poll): first release

This commit is contained in:
Romain J 2019-12-16 23:06:25 +01:00
parent 348a78e4b3
commit d9427d1863
4 changed files with 37 additions and 31 deletions

View file

@ -10,7 +10,7 @@ from discord.ext import commands
from bot import TuxBot from bot import TuxBot
from .utils.lang import Texts from .utils.lang import Texts
from .utils.extra import commandExtra, groupExtra from .utils.extra import commandExtra
from tcp_latency import measure_latency from tcp_latency import measure_latency
@ -29,7 +29,7 @@ class Basics(commands.Cog):
@commandExtra(name='ping', @commandExtra(name='ping',
category='basics', category='basics',
description=Texts('commands').get('basics.ping')) description=Texts('commands').get('basics._ping'))
async def _ping(self, ctx: commands.Context): async def _ping(self, ctx: commands.Context):
start = time.perf_counter() start = time.perf_counter()
await ctx.trigger_typing() await ctx.trigger_typing()
@ -67,9 +67,9 @@ class Basics(commands.Cog):
return total, file_amount return total, file_amount
@commands.command(name='info', aliases=['about'], @commandExtra(name='info', aliases=['about'],
category='basics', category='basics',
description=Texts('commands').get('basics.info')) description=Texts('commands').get('basics._info'))
async def _info(self, ctx: commands.Context): async def _info(self, ctx: commands.Context):
proc = psutil.Process() proc = psutil.Process()
lines, files = self.fetch_info() lines, files = self.fetch_info()
@ -150,9 +150,9 @@ class Basics(commands.Cog):
"""---------------------------------------------------------------------""" """---------------------------------------------------------------------"""
@commands.command(name='credits', aliases=['contributors', 'authors'], @commandExtra(name='credits', aliases=['contributors', 'authors'],
category='basics', category='basics',
description=Texts('commands').get('basics.credits')) description=Texts('commands').get('basics._credits'))
async def _credits(self, ctx: commands.Context): async def _credits(self, ctx: commands.Context):
e = discord.Embed( e = discord.Embed(
title=Texts('basics', ctx).get('Contributors'), title=Texts('basics', ctx).get('Contributors'),

View file

@ -8,6 +8,7 @@ from yarl import URL
from bot import TuxBot from bot import TuxBot
from .utils.lang import Texts from .utils.lang import Texts
from .utils.models import Poll, Responses from .utils.models import Poll, Responses
from .utils.extra import groupExtra
from .utils import emotes as utils_emotes from .utils import emotes as utils_emotes
@ -49,31 +50,28 @@ class Polls(commands.Cog):
pass pass
choice = utils_emotes.get_index(pld.emoji.name) choice = utils_emotes.get_index(pld.emoji.name)
print(choice) responses = self.bot.database.session.query(Responses) \
response = self.bot.database.session.query(Poll) \
.filter( .filter(
Responses.poll_id == poll.id, Responses.poll_id == poll.id,
Responses.user == pld.user_id, Responses.user == pld.user_id,
Responses.choice == choice Responses.choice == choice
) )
if response.count() != 0: if responses.count() != 0:
print("--pre delete--") response = responses.first()
response = response.one()
self.bot.database.session.delete(response) self.bot.database.session.delete(response)
print("--post delete--") self.bot.database.session.commit()
else: else:
print("--pre add--")
response = Responses( response = Responses(
user=pld.user_id, user=pld.user_id,
poll_id=poll.id, poll_id=poll.id,
choice=choice choice=choice
) )
self.bot.database.session.add(response) 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)
"""---------------------------------------------------------------------""" """---------------------------------------------------------------------"""
async def create_poll(self, ctx: commands.Context, poll: str, anonymous): async def create_poll(self, ctx: commands.Context, poll: str, anonymous):
@ -89,7 +87,7 @@ class Polls(commands.Cog):
e = discord.Embed(description=f"**{question}**") e = discord.Embed(description=f"**{question}**")
e.set_author( e.set_author(
name=self.bot.user if anonymous else ctx.author, name=ctx.author,
icon_url="https://cdn.gnous.eu/tuxbot/survey1.png" icon_url="https://cdn.gnous.eu/tuxbot/survey1.png"
) )
for i, response in enumerate(responses): for i, response in enumerate(responses):
@ -97,7 +95,7 @@ class Polls(commands.Cog):
name=f"{emotes[i]} __{response.capitalize()}__", name=f"{emotes[i]} __{response.capitalize()}__",
value="**0** vote" 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.channel_id = stmt.channel.id
poll_row.message_id = stmt.id poll_row.message_id = stmt.id
@ -135,12 +133,19 @@ class Polls(commands.Cog):
content = json.loads(poll.content) \ content = json.loads(poll.content) \
if isinstance(poll.content, str) \ if isinstance(poll.content, str) \
else poll.content else poll.content
responses = json.loads(poll.responses) \ raw_responses = self.bot.database.session\
if isinstance(poll.responses, str) \ .query(Responses)\
else poll.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')): for i, field in enumerate(content.get('fields')):
responders = len(responses.get(str(i + 1))) responders = responses.get(i, 0)
chart_options.get('data') \ chart_options.get('data') \
.get('labels') \ .get('labels') \
.append(field.get('name')[5:].replace('__', '')) .append(field.get('name')[5:].replace('__', ''))
@ -174,12 +179,16 @@ class Polls(commands.Cog):
poll.content = json.dumps(content) poll.content = json.dumps(content)
self.bot.database.session.commit() 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): async def _poll(self, ctx: commands.Context):
if ctx.invoked_subcommand is None: 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): async def _poll_create(self, ctx: commands.Context, *, poll: str):
is_anonymous = '--anonyme' in poll is_anonymous = '--anonyme' in poll
poll = poll.replace('--anonyme', '') poll = poll.replace('--anonyme', '')

View file

@ -1,7 +1,6 @@
from . import Base from . import Base
from sqlalchemy import Column, Integer, BigInteger, JSON, ForeignKey, Boolean from sqlalchemy import Column, Integer, BigInteger, JSON, ForeignKey, Boolean
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from werkzeug.security import generate_password_hash, check_password_hash
class Poll(Base): class Poll(Base):

View file

@ -6,5 +6,3 @@ psycopg2
configparser configparser
psutil psutil
tcp_latency tcp_latency
i18n
werkzeug