tux.cogs.utility.poll
¶
Classes:
Name | Description |
---|---|
Poll | |
Classes¶
Poll(bot: Tux)
¶
Bases: Cog
Methods:
Name | Description |
---|---|
is_pollbanned | Check if a user is currently poll banned. |
poll | Create a poll with a title and options. |
Source code in tux/cogs/utility/poll.py
Functions¶
is_pollbanned(guild_id: int, user_id: int) -> bool
async
¶
Check if a user is currently poll banned. The user is considered poll banned if their latest relevant case (POLLBAN or POLLUNBAN) is a POLLBAN.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
guild_id | int | The ID of the guild to check in. | required |
user_id | int | The ID of the user to check. | required |
Returns:
Type | Description |
---|---|
bool | True if the user is poll banned, False otherwise. |
Source code in tux/cogs/utility/poll.py
Python
async def is_pollbanned(self, guild_id: int, user_id: int) -> bool:
"""
Check if a user is currently poll banned.
The user is considered poll banned if their latest relevant case (POLLBAN or POLLUNBAN) is a POLLBAN.
Parameters
----------
guild_id : int
The ID of the guild to check in.
user_id : int
The ID of the user to check.
Returns
-------
bool
True if the user is poll banned, False otherwise.
"""
latest_case = await self.db.case.get_latest_case_by_user(
guild_id=guild_id,
user_id=user_id,
case_types=[CaseType.POLLBAN, CaseType.POLLUNBAN],
)
# If no relevant cases exist, the user is not poll banned.
return latest_case.case_type == CaseType.POLLBAN if latest_case else False
poll(interaction: discord.Interaction, title: str, options: str) -> None
async
¶
Create a poll with a title and options.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interaction | Interaction | The discord interaction object. | required |
title | str | The title of the poll. | required |
options | str | The options for the poll, separated by commas. | required |
Source code in tux/cogs/utility/poll.py
Python
@app_commands.command(name="poll", description="Creates a poll.")
@app_commands.describe(title="Title of the poll", options="Poll options, comma separated")
async def poll(self, interaction: discord.Interaction, title: str, options: str) -> None:
"""
Create a poll with a title and options.
Parameters
----------
interaction : discord.Interaction
The discord interaction object.
title : str
The title of the poll.
options : str
The options for the poll, separated by commas.
"""
if interaction.guild_id is None:
await interaction.response.send_message("This command can only be used in a server.", ephemeral=True)
return
# Split the options by comma
options_list = options.split(",")
# Remove any leading or trailing whitespaces from the options
options_list = [option.strip() for option in options_list]
if await self.is_pollbanned(interaction.guild_id, interaction.user.id):
embed = EmbedCreator.create_embed(
bot=self.bot,
embed_type=EmbedCreator.ERROR,
user_name=interaction.user.name,
user_display_avatar=interaction.user.display_avatar.url,
title="Poll Banned",
description="You are poll banned and cannot create a poll.",
)
await interaction.response.send_message(embed=embed, ephemeral=True)
return
# Check if the options count is between 2-9
if len(options_list) < 2 or len(options_list) > 9:
embed = EmbedCreator.create_embed(
bot=self.bot,
embed_type=EmbedCreator.ERROR,
user_name=interaction.user.name,
user_display_avatar=interaction.user.display_avatar.url,
title="Invalid options count",
description=f"Poll options count needs to be between 2-9, you provided {len(options_list)} options.",
)
await interaction.response.send_message(embed=embed, ephemeral=True, delete_after=30)
return
# Create the description for the poll embed
description = "\n".join(
[f"{num + 1}\u20e3 {option}" for num, option in enumerate(options_list)],
)
embed = EmbedCreator.create_embed(
bot=self.bot,
embed_type=EmbedCreator.POLL,
user_name=interaction.user.name,
user_display_avatar=interaction.user.display_avatar.url,
title=title,
description=description,
)
await interaction.response.send_message(embed=embed)
# We can use await interaction.original_response() to get the message object
message = await interaction.original_response()
for num in range(len(options_list)):
# Add the number emoji reaction to the message
await message.add_reaction(f"{num + 1}\u20e3")