feat(command|sondage): add charts

This commit is contained in:
Romain J 2019-10-08 18:54:05 +02:00
parent ad4fb2fa89
commit 25f5c5e1f6
2 changed files with 27 additions and 3 deletions

View file

@ -4,6 +4,7 @@ from typing import Union
import discord import discord
import bcrypt import bcrypt
from discord.ext import commands from discord.ext import commands
from yarl import URL
from bot import TuxBot from bot import TuxBot
from .utils.lang import Texts from .utils.lang import Texts
@ -46,6 +47,19 @@ class Polls(commands.Cog):
channel: discord.TextChannel = self.bot.get_channel(poll.channel_id) channel: discord.TextChannel = self.bot.get_channel(poll.channel_id)
message: discord.Message = await channel.fetch_message(poll.message_id) message: discord.Message = await channel.fetch_message(poll.message_id)
chart_base_url = "https://quickchart.io/chart?backgroundColor=white&c="
chart_options = {
'type': 'pie',
'data': {
'labels': [],
'datasets': [
{
'data': []
}
]
}
}
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
@ -55,6 +69,14 @@ class Polls(commands.Cog):
for i, field in enumerate(content.get('fields')): for i, field in enumerate(content.get('fields')):
responders = len(responses.get(str(i + 1))) responders = len(responses.get(str(i + 1)))
chart_options.get('data') \
.get('labels') \
.append(field.get('name')[5:].replace('__', ''))
chart_options.get('data') \
.get('datasets')[0] \
.get('data') \
.append(responders)
if responders <= 1: if responders <= 1:
field['value'] = f"**{responders}** vote" field['value'] = f"**{responders}** vote"
else: else:
@ -65,6 +87,8 @@ class Polls(commands.Cog):
name=content.get('author').get('name'), name=content.get('author').get('name'),
icon_url=content.get('author').get('icon_url') icon_url=content.get('author').get('icon_url')
) )
chart_url = URL(chart_base_url + json.dumps(chart_options))
e.set_thumbnail(url=chart_url)
for field in content.get('fields'): for field in content.get('fields'):
e.add_field( e.add_field(
name=field.get('name'), name=field.get('name'),

View file

@ -1,11 +1,11 @@
{ {
"280805240977227776": [ "280805240977227776": [
"b! " "b!-"
], ],
"303633056944881686": [ "303633056944881686": [
"b! " "b!-"
], ],
"336642139381301249": [ "336642139381301249": [
"b! " "b!-"
] ]
} }