2016-08-14 13:46:47 +12:00
|
|
|
from discord.ext import commands
|
2016-08-14 17:18:08 +12:00
|
|
|
import discord
|
2016-08-14 13:53:01 +12:00
|
|
|
|
2016-08-14 13:46:47 +12:00
|
|
|
from .utils import config
|
|
|
|
from .utils import checks
|
2016-08-14 13:53:01 +12:00
|
|
|
|
2016-08-14 14:50:18 +12:00
|
|
|
import aiohttp
|
2016-08-14 13:46:47 +12:00
|
|
|
import re
|
|
|
|
import json
|
2016-08-14 15:27:12 +12:00
|
|
|
import pendulum
|
2016-08-14 13:46:47 +12:00
|
|
|
|
2016-08-14 17:10:51 +12:00
|
|
|
|
2016-08-14 14:33:12 +12:00
|
|
|
def setup(bot):
|
|
|
|
bot.add_cog(Strawpoll(bot))
|
|
|
|
|
2016-08-14 17:10:51 +12:00
|
|
|
|
2016-08-14 13:46:47 +12:00
|
|
|
getter = re.compile(r'`(?!`)(.*?)`')
|
|
|
|
multi = re.compile(r'```(.*?)```', re.DOTALL)
|
|
|
|
|
2016-08-14 14:24:19 +12:00
|
|
|
|
2016-08-14 13:46:47 +12:00
|
|
|
class Strawpoll:
|
|
|
|
"""This class is used to create new strawpoll """
|
2016-08-14 17:10:51 +12:00
|
|
|
|
2016-08-14 13:46:47 +12:00
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
|
|
|
self.url = 'https://strawpoll.me/api/v2/polls'
|
|
|
|
self.headers = {'User-Agent': 'Bonfire/1.0.0',
|
|
|
|
'Content-Type': 'application/json'}
|
2016-08-14 14:57:34 +12:00
|
|
|
self.session = aiohttp.ClientSession()
|
2016-08-14 13:46:47 +12:00
|
|
|
|
2016-08-14 17:10:51 +12:00
|
|
|
@commands.group(aliases=['strawpoll', 'poll', 'polls'], pass_context=True, invoke_without_command=True)
|
2016-08-15 14:10:12 +12:00
|
|
|
@checks.custom_perms(send_messages=True)
|
2016-08-14 17:41:05 +12:00
|
|
|
async def strawpolls(self, ctx, poll_id: str = None):
|
2016-08-14 13:46:47 +12:00
|
|
|
"""This command can be used to show a strawpoll setup on this server"""
|
2016-08-15 14:10:12 +12:00
|
|
|
all_polls = config.get_content('strawpolls') or {}
|
2016-08-14 15:29:36 +12:00
|
|
|
server_polls = all_polls.get(ctx.message.server.id) or {}
|
2016-08-14 13:46:47 +12:00
|
|
|
if not server_polls:
|
|
|
|
await self.bot.say("There are currently no strawpolls running on this server!")
|
|
|
|
return
|
|
|
|
if not poll_id:
|
2016-08-14 17:10:51 +12:00
|
|
|
fmt = "\n".join(
|
2016-08-14 17:41:05 +12:00
|
|
|
"{}: https://strawpoll.me/{}".format(data['title'], _id) for _id, data in server_polls.items())
|
2016-08-14 13:46:47 +12:00
|
|
|
await self.bot.say("```\n{}```".format(fmt))
|
2016-08-14 17:41:05 +12:00
|
|
|
elif poll_id in server_polls.keys():
|
|
|
|
poll = server_polls[poll_id]
|
2016-08-14 17:10:51 +12:00
|
|
|
|
2016-08-14 17:41:05 +12:00
|
|
|
async with self.session.get("{}/{}".format(self.url, poll_id)) as response:
|
2016-08-14 13:46:47 +12:00
|
|
|
data = await response.json()
|
2016-08-14 17:10:51 +12:00
|
|
|
|
|
|
|
fmt_options = "\n\t".join(
|
2016-08-14 17:14:41 +12:00
|
|
|
"{}: {}".format(r, data['votes'][i]) for i, r in enumerate(data['options']))
|
2016-08-14 17:18:08 +12:00
|
|
|
author = discord.utils.get(self.bot.get_all_members(), id=poll['author'])
|
2016-08-14 17:19:18 +12:00
|
|
|
created_ago = (pendulum.utcnow() - pendulum.parse(poll['date'])).in_words()
|
2016-08-14 17:41:05 +12:00
|
|
|
link = "https://strawpoll.me/{}".format(poll_id)
|
2016-08-14 17:10:51 +12:00
|
|
|
fmt = "Link: {}\nTitle: {}\nAuthor: {}\nCreated: {}\nOptions:\n\t{}".format(link, data['title'],
|
|
|
|
author.display_name,
|
|
|
|
created_ago, fmt_options)
|
2016-08-14 13:46:47 +12:00
|
|
|
await self.bot.say("```\n{}```".format(fmt))
|
2016-08-14 17:10:51 +12:00
|
|
|
|
2016-08-14 13:46:47 +12:00
|
|
|
@strawpolls.command(name='create', aliases=['setup', 'add'], pass_context=True)
|
2016-08-15 14:10:12 +12:00
|
|
|
@checks.custom_perms(kick_members=True)
|
2016-08-14 13:46:47 +12:00
|
|
|
async def create_strawpoll(self, ctx, title, *, options):
|
|
|
|
"""This command is used to setup a new strawpoll
|
|
|
|
The format needs to be: poll create "title here" all options here
|
|
|
|
Options need to be separated by using either one ` around each option
|
|
|
|
Or use a code block (3 ` around the options), each option on it's own line"""
|
|
|
|
match_single = getter.findall(options)
|
|
|
|
match_multi = multi.findall(options)
|
|
|
|
if match_single:
|
|
|
|
options = match_single
|
2016-08-14 15:18:43 +12:00
|
|
|
options = [option for option in options if option]
|
2016-08-14 13:46:47 +12:00
|
|
|
elif match_multi:
|
|
|
|
options = match_multi[0].splitlines()
|
2016-08-14 15:18:43 +12:00
|
|
|
options = [option for option in options if option]
|
2016-08-14 13:46:47 +12:00
|
|
|
else:
|
2016-08-14 17:10:51 +12:00
|
|
|
await self.bot.say(
|
|
|
|
"Please provide options for a new strawpoll! Use {}help if you do not know the format".format(
|
|
|
|
ctx.prefix))
|
2016-08-14 13:46:47 +12:00
|
|
|
return
|
|
|
|
payload = {'title': title,
|
2016-08-14 17:10:51 +12:00
|
|
|
'options': options}
|
2016-08-14 13:46:47 +12:00
|
|
|
async with self.session.post(self.url, data=json.dumps(payload), headers=self.headers) as response:
|
|
|
|
data = await response.json()
|
2016-08-14 17:10:51 +12:00
|
|
|
|
2016-08-15 14:10:12 +12:00
|
|
|
all_polls = config.get_content('strawpolls') or {}
|
2016-08-14 15:29:36 +12:00
|
|
|
server_polls = all_polls.get(ctx.message.server.id) or {}
|
2016-08-14 17:10:51 +12:00
|
|
|
server_polls[data['id']] = {'author': ctx.message.author.id, 'date': str(pendulum.utcnow()), 'title': title}
|
2016-08-14 15:27:12 +12:00
|
|
|
all_polls[ctx.message.server.id] = server_polls
|
2016-08-15 14:10:12 +12:00
|
|
|
config.save_content('strawpolls', all_polls)
|
2016-08-14 13:48:08 +12:00
|
|
|
|
2016-08-14 17:10:51 +12:00
|
|
|
await self.bot.say("Link for your new strawpoll: https://strawpoll.me/{}".format(data['id']))
|
2016-08-14 17:41:05 +12:00
|
|
|
|
|
|
|
@strawpolls.command(name='delete', aliases=['remove', 'stop'], pass_context=True)
|
2016-08-15 14:10:12 +12:00
|
|
|
@checks.custom_perms(kick_members=True)
|
2016-08-14 17:41:05 +12:00
|
|
|
async def remove_strawpoll(self, ctx, poll_id: str = None):
|
|
|
|
"""This command can be used to delete one of the existing strawpolls
|
|
|
|
If you don't provide an ID it will print the list of polls available"""
|
|
|
|
|
2016-08-15 14:10:12 +12:00
|
|
|
all_polls = config.get_content('strawpolls') or {}
|
2016-08-14 17:41:05 +12:00
|
|
|
server_polls = all_polls.get(ctx.message.server.id) or {}
|
|
|
|
|
|
|
|
if poll_id:
|
|
|
|
poll = server_polls.get(poll_id)
|
|
|
|
if not poll:
|
2016-08-14 17:46:58 +12:00
|
|
|
fmt = "\n".join("{}: {}".format(data['title'], _poll_id) for _poll_id, data in server_polls.items())
|
2016-08-14 17:41:05 +12:00
|
|
|
await self.bot.say(
|
|
|
|
"There is no poll setup with that ID! Here is a list of the current polls```\n{}```".format(fmt))
|
|
|
|
else:
|
|
|
|
del server_polls[poll_id]
|
|
|
|
all_polls[ctx.message.server.id] = server_polls
|
2016-08-15 14:10:12 +12:00
|
|
|
config.save_content('strawpolls', all_polls)
|
2016-08-14 17:42:57 +12:00
|
|
|
await self.bot.say("I have just removed the poll with the ID {}".format(poll_id))
|
2016-08-14 17:41:05 +12:00
|
|
|
else:
|
2016-08-14 17:46:58 +12:00
|
|
|
fmt = "\n".join("{}: {}".format(data['title'], _poll_id) for _poll_id, data in server_polls.items())
|
2016-08-14 17:41:05 +12:00
|
|
|
await self.bot.say("Here is a list of the polls on this server:\n```\n{}```".format(fmt))
|