From 25f5c5e1f674d0d31b916dfde9612d1d8c57a0e9 Mon Sep 17 00:00:00 2001 From: Romain J Date: Tue, 8 Oct 2019 18:54:05 +0200 Subject: [PATCH] feat(command|sondage): add charts --- cogs/poll.py | 24 ++++++++++++++++++++++++ prefixes.json | 6 +++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/cogs/poll.py b/cogs/poll.py index d480f81..3314189 100644 --- a/cogs/poll.py +++ b/cogs/poll.py @@ -4,6 +4,7 @@ from typing import Union import discord import bcrypt from discord.ext import commands +from yarl import URL from bot import TuxBot from .utils.lang import Texts @@ -46,6 +47,19 @@ class Polls(commands.Cog): channel: discord.TextChannel = self.bot.get_channel(poll.channel_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) \ if isinstance(poll.content, str) \ else poll.content @@ -55,6 +69,14 @@ class Polls(commands.Cog): for i, field in enumerate(content.get('fields')): 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: field['value'] = f"**{responders}** vote" else: @@ -65,6 +87,8 @@ class Polls(commands.Cog): name=content.get('author').get('name'), 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'): e.add_field( name=field.get('name'), diff --git a/prefixes.json b/prefixes.json index 119d391..f682140 100644 --- a/prefixes.json +++ b/prefixes.json @@ -1,11 +1,11 @@ { "280805240977227776": [ - "b! " + "b!-" ], "303633056944881686": [ - "b! " + "b!-" ], "336642139381301249": [ - "b! " + "b!-" ] } \ No newline at end of file