Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add poll command #193

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions uqcsbot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ async def main():
"morse",
"past_exams",
"phonetics",
"poll",
"remindme",
"snailrace",
"starboard",
Expand Down
82 changes: 82 additions & 0 deletions uqcsbot/poll.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import discord
from discord import app_commands
from discord.ext import commands

from typing import Any

# The unicode points for each number emoji starting from 0 to 10
EMOJI_NUMBERS = [
"\u0030\u20E3",
"\u0031\u20E3",
"\u0032\u20E3",
"\u0033\u20E3",
"\u0034\u20E3",
"\u0035\u20E3",
"\u0036\u20E3",
"\u0037\u20E3",
"\u0038\u20E3",
"\u0039\u20E3",
"\U0001F51F",
]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imo, rather than unicode these could just be the the Discord emoji names one, two, three, etc.



class PollMenu(discord.ui.Modal, title="New Poll"):
"""The menu to create a new poll"""

poll_question: discord.ui.TextInput[Any] = discord.ui.TextInput(
label="A question of the poll",
placeholder="Your question goes here (e.g. What does UQCS stand for?)",
max_length=100,
)
poll_options: discord.ui.TextInput[Any] = discord.ui.TextInput(
label="The options for the poll (one per line)",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to include the max 10 in the dialog, as you don't find out til after you put more than 10

style=discord.TextStyle.long,
placeholder="Option 1 (e.g. UQ Computing Society)\nOption 2 (e.g. UQ Caveman Society)\nOption 3...",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment: I believe we actually refer to it as the UQ Cavepainting Society

max_length=3000,
)

async def on_submit(self, interaction: discord.Interaction):
options = [option.strip() for option in self.poll_options.value.split("\n")]
options = [option for option in options if option != ""]
if len(options) < 2:
await interaction.response.send_message(
"Not enough options. Make sure each option is on a new line.",
ephemeral=True,
)
return
if len(options) > 10:
await interaction.response.send_message(
"Too many options. You can have at most 10 options.", ephemeral=True
)
return
if any(len(option) > 75 for option in options):
await interaction.response.send_message(
"Your options are too long. Each option must be less than 75 characters.",
ephemeral=True,
)
return

options_string = "\n".join(
f"{EMOJI_NUMBERS[number]}: {option}"
for number, option in enumerate(options, start=1)
)
await interaction.response.send_message(
content=f"{self.poll_question}\n{options_string}"
)
message = await interaction.original_response()
print(message)
for i, _ in enumerate(options, start=1):
await message.add_reaction(EMOJI_NUMBERS[i])


class Poll(commands.Cog):
@app_commands.command(
name="poll",
description="Create a poll with multiple options. Run the command for the set-up wizard.",
)
async def start_poll_menu(self, interaction: discord.Interaction):
await interaction.response.send_modal(PollMenu())


async def setup(bot: commands.Bot):
await bot.add_cog(Poll(bot))
Loading