From 07c8f286a2f57c521c3e37bd07acf976fe8236fd Mon Sep 17 00:00:00 2001 From: Phxntxm Date: Sat, 13 Aug 2016 22:27:12 -0500 Subject: [PATCH] Added the ability to save strawpolls per server --- cogs/strawpoll.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/cogs/strawpoll.py b/cogs/strawpoll.py index 96f2973..3307eff 100644 --- a/cogs/strawpoll.py +++ b/cogs/strawpoll.py @@ -6,6 +6,7 @@ from .utils import checks import aiohttp import re import json +import pendulum def setup(bot): bot.add_cog(Strawpoll(bot)) @@ -41,10 +42,14 @@ class Strawpoll: fmt = "\n".join("{}: https://strawpoll.me/{}".format(title, id) for id, title in server_polls.items()) await self.bot.say("```\n{}```".format(fmt)) elif poll_id in server_polls.keys(): + poll = server_polls[poll_id] + async with self.session.get("{}/{}".format(self.url, str(poll_id)), headers=self.headers) as response: data = await response.json() + fmt_options = "\n\t".join("{}: {}".format(data['options'][i], data['votes'][i]) for i in range(data['options'])) - fmt = "Link: {}\nTitle: {}\nOptions:\n\t{}".format("https://strawpoll.me{}".format(str(poll_id)), data['title'], fmt_options) + fmt = "Link: {}\nTitle: {}\nAuthor: {}\nCreated: {}\nOptions:\n\t{}".format("https://strawpoll.me{}".format( + str(poll_id)), data['title'], poll['author'],(pendulum.parse(poll['date'])-pendulum.utcnow()).in_words(),fmt_options) await self.bot.say("```\n{}```".format(fmt)) @@ -70,5 +75,11 @@ class Strawpoll: 'options': options} async with self.session.post(self.url, data=json.dumps(payload), headers=self.headers) as response: data = await response.json() + + all_polls = config.getContent('strawpolls') or {} + server_polls = all_polls.get(ctx.message.server.id) + server_polls[data['id']] = {'author': ctx.message.author,'date': pendulum.utcnow()} + all_polls[ctx.message.server.id] = server_polls + config.saveContent('strawpolls',all_polls) await self.bot.say("Link for your new strawpoll: https://strawpoll.me/{}".format(data['id']))