From 25b7adc3275d47400dba5118b8383210f863937f Mon Sep 17 00:00:00 2001 From: Luke Robles <98352913+lrobles-iterable@users.noreply.github.com> Date: Fri, 13 May 2022 19:10:03 -0700 Subject: [PATCH] Adding poll function --- app/cogs/poll.py | 64 +++++++++++++++++++++++++++++++++++++++++++++ app/help_methods.py | 5 ++++ helm/values.yaml | 2 +- 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 app/cogs/poll.py diff --git a/app/cogs/poll.py b/app/cogs/poll.py new file mode 100644 index 00000000..4313f524 --- /dev/null +++ b/app/cogs/poll.py @@ -0,0 +1,64 @@ +import discord +from discord.ext import commands + + +class QuickPoll(commands.Cog): + def __init__(self, bot): + self.bot = bot + + @commands.command(pass_context=True) + async def poll(self, ctx, question, *options: str): + if len(options) <= 1: + await ctx.send("You need more than one option to make a poll!") + return + if len(options) > 10: + await ctx.send("You cannot make a poll for more than 10 things!") + return + + if len(options) == 2 and options[0] == "yes" and options[1] == "no": + reactions = ["✅", "❌"] + else: + reactions = ["1⃣", "2⃣", "3⃣", "4⃣", "5⃣", "6⃣", "7⃣", "8⃣", "9⃣", "🔟"] + + description = [] + for x, option in enumerate(options): + description += "\n {} {}".format(reactions[x], option) + embed = discord.Embed(title=question, description="".join(description)) + react_message = await ctx.send(embed=embed) + for reaction in reactions[: len(options)]: + await react_message.add_reaction(reaction) + embed.set_footer(text="Poll ID: {}".format(react_message.id)) + await react_message.edit_message(embed=embed) + + @commands.command(pass_context=True) + async def tally(self, ctx, id=None): + poll_message = await ctx.channel.fetch_message(id) + embed = poll_message.embeds[0] + unformatted_options = [x.strip() for x in embed.description.split("\n")] + print(f"unformatted{unformatted_options}") + opt_dict = ( + {x[:2]: x[3:] for x in unformatted_options} + if unformatted_options[0][0] == "1" + else {x[:1]: x[2:] for x in unformatted_options} + ) + # check if we're using numbers for the poll, or x/checkmark, parse accordingly + voters = [ + self.bot.user.id + ] # add the bot's ID to the list of voters to exclude it's votes + + tally = {x: 0 for x in opt_dict.keys()} + for reaction in poll_message.reactions: + if reaction.emoji in opt_dict.keys(): + reactors = await reaction.users().flatten() + for reactor in reactors: + if reactor.id not in voters: + tally[reaction.emoji] += 1 + voters.append(reactor.id) + output = f"Results of the poll for '{embed.title}':\n" + "\n".join( + ["{}: {}".format(opt_dict[key], tally[key]) for key in tally.keys()] + ) + await ctx.send(output) + + +def setup(bot): + bot.add_cog(QuickPoll(bot)) diff --git a/app/help_methods.py b/app/help_methods.py index bba3e581..fcdabe9d 100755 --- a/app/help_methods.py +++ b/app/help_methods.py @@ -121,6 +121,10 @@ def get_help_message(method): "By default, will delete your last 20 messages. You can override this", " with your own number. \nUsage: !purge or !purge 15", ], + "poll": [ + "Creates a poll in the channel, allowing up to 10 options\n\n", + '\nUsage: !poll "What is your favorite pizza toppping?" cheese peppers pineapple', + ], "roles": [ "dale bot will PM you a message with all the roles you have on the server" ], @@ -226,6 +230,7 @@ def get_help_embed(bot): "icon", "info", "issue", + "poll", "stock", "tts", "vc", diff --git a/helm/values.yaml b/helm/values.yaml index b5bcf783..7f1bc450 100644 --- a/helm/values.yaml +++ b/helm/values.yaml @@ -6,7 +6,7 @@ replicaCount: 1 image: # Overrides the image tag whose default is the chart appVersion. - tag: "195" + tag: "196" repository: ldooks/dragon-bot pullPolicy: IfNotPresent