From c2493ecadb193fda2f58c2cca9d53c5407efbeaf Mon Sep 17 00:00:00 2001 From: EliteDaMyth1337 <33662389+EliteDaMyth1337@users.noreply.github.com> Date: Tue, 14 Nov 2017 20:31:01 +0530 Subject: [PATCH 1/3] Update utils.py --- cogs/utils.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/cogs/utils.py b/cogs/utils.py index fa071e64..c542491d 100644 --- a/cogs/utils.py +++ b/cogs/utils.py @@ -1021,6 +1021,64 @@ async def update(self, ctx): else: return await ctx.send('This command is disabled as the user have not starred ') + @commands.command(pass_context=True) + async def rpoll(self, ctx, *, msg): + """Create a poll using reactions. >help rpoll for more information. + [p]rpoll | | - Create a poll. You may use as many answers as you want, placing a pipe | symbol in between them. + Example: + [p]rpoll What is your favorite anime? | Steins;Gate | Naruto | Attack on Titan | Shrek + You can also use the "time" flag to set the amount of time in seconds the poll will last for. + Example: + [p]rpoll What time is it? | HAMMER TIME! | SHOWTIME! | time=10 + """ + await ctx.message.delete() + options = msg.split(" | ") + time = [x for x in options if x.startswith("time=")] + if time: + time = time[0] + if time: + options.remove(time) + if len(options) <= 1: + raise commands.errors.MissingRequiredArgument + if len(options) >= 11: + return await ctx.send(self.bot.bot_prefix + "You must have 9 options or less.") + if time: + time = int(time.strip("time=")) + else: + time = 30 + emoji = ['1⃣', '2⃣', '3⃣', '4⃣', '5⃣', '6⃣', '7⃣', '8⃣', '9⃣'] + to_react = [] + confirmation_msg = "**{}?**:\n\n".format(options[0].rstrip("?")) + for idx, option in enumerate(options[1:]): + confirmation_msg += "{} - {}\n".format(emoji[idx], option) + to_react.append(emoji[idx]) + confirmation_msg += "\n\nYou have {} seconds to vote!".format(time) + poll_msg = await ctx.send(confirmation_msg) + for emote in to_react: + await poll_msg.add_reaction(emote) + await asyncio.sleep(time) + async for message in ctx.message.channel.history(): + if message.id == poll_msg.id: + poll_msg = message + results = {} + for reaction in poll_msg.reactions: + if reaction.emoji in to_react: + results[reaction.emoji] = reaction.count - 1 + end_msg = "The poll is over. The results:\n\n" + for result in results: + end_msg += "{} {} - {} votes\n".format(result, options[emoji.index(result)+1], results[result]) + top_result = max(results, key=lambda key: results[key]) + if len([x for x in results if results[x] == results[top_result]]) > 1: + top_results = [] + for key, value in results.items(): + if value == results[top_result]: + top_results.append(options[emoji.index(key)+1]) + end_msg += "\nThe victory is tied between: {}".format(", ".join(top_results)) + else: + top_result = options[emoji.index(top_result)+1] + end_msg += "\n{} is the winner!".format(top_result) + await ctx.send(end_msg) + @commands.command() async def cc(self, ctx, option, name='None', content=None): git = self.bot.get_cog('Git') From b5f740a3e7e5e4983e6c335c1006cce100125ba0 Mon Sep 17 00:00:00 2001 From: EliteDaMyth1337 <33662389+EliteDaMyth1337@users.noreply.github.com> Date: Tue, 14 Nov 2017 20:35:14 +0530 Subject: [PATCH 2/3] Update utils.py --- cogs/utils.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cogs/utils.py b/cogs/utils.py index c542491d..026c148a 100644 --- a/cogs/utils.py +++ b/cogs/utils.py @@ -1023,13 +1023,13 @@ async def update(self, ctx): @commands.command(pass_context=True) async def rpoll(self, ctx, *, msg): - """Create a poll using reactions. >help rpoll for more information. - [p]rpoll | | - Create a poll. You may use as many answers as you want, placing a pipe | symbol in between them. + """Create a poll using reactions. {p}help rpoll for more information. + {p}rpoll | | - Create a poll. You may use as many answers as you want, placing a pipe | symbol in between them. Example: - [p]rpoll What is your favorite anime? | Steins;Gate | Naruto | Attack on Titan | Shrek + {p}rpoll What is your favorite anime? | Steins;Gate | Naruto | Attack on Titan | Shrek You can also use the "time" flag to set the amount of time in seconds the poll will last for. Example: - [p]rpoll What time is it? | HAMMER TIME! | SHOWTIME! | time=10 + {p}rpoll What time is it? | HAMMER TIME! | SHOWTIME! | time=10 """ await ctx.message.delete() options = msg.split(" | ") From 289483e9ae55256ee83468744e939765a1072c20 Mon Sep 17 00:00:00 2001 From: fourjr Date: Tue, 14 Nov 2017 23:14:21 +0800 Subject: [PATCH 3/3] change it to args --- cogs/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cogs/utils.py b/cogs/utils.py index 026c148a..f6dc0ad1 100644 --- a/cogs/utils.py +++ b/cogs/utils.py @@ -1022,7 +1022,7 @@ async def update(self, ctx): return await ctx.send('This command is disabled as the user have not starred ') @commands.command(pass_context=True) - async def rpoll(self, ctx, *, msg): + async def rpoll(self, ctx, *, args): """Create a poll using reactions. {p}help rpoll for more information. {p}rpoll | | - Create a poll. You may use as many answers as you want, placing a pipe | symbol in between them. Example: @@ -1032,7 +1032,7 @@ async def rpoll(self, ctx, *, msg): {p}rpoll What time is it? | HAMMER TIME! | SHOWTIME! | time=10 """ await ctx.message.delete() - options = msg.split(" | ") + options = args.split(" | ") time = [x for x in options if x.startswith("time=")] if time: time = time[0]