-
Notifications
You must be signed in to change notification settings - Fork 20
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
Add poll command #193
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,7 @@ async def main(): | |
"morse", | ||
"past_exams", | ||
"phonetics", | ||
"poll", | ||
"remindme", | ||
"snailrace", | ||
"starboard", | ||
|
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", | ||
] | ||
|
||
|
||
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)", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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...", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) |
There was a problem hiding this comment.
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.