Skip to content

tux.cogs.utility.poll

Classes:

Name Description
Poll

Classes

Poll(bot: Tux)

Bases: Cog

Methods:

Name Description
is_pollbanned

Check if a user is poll banned.

poll

Create a poll with a title and options.

Source code in tux/cogs/utility/poll.py
Python
def __init__(self, bot: Tux) -> None:
    self.bot = bot
    self.db = DatabaseController()

Functions

is_pollbanned(guild_id: int, user_id: int) -> bool async

Check if a user is poll banned.

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 poll banned.

    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.
    """

    ban_cases = await self.db.case.find_many(where={"case_type": CaseType.POLLBAN})
    unban_cases = await self.db.case.find_many(where={"case_type": CaseType.POLLUNBAN})

    ban_count = sum(case.case_user_id == user_id for case in ban_cases)
    unban_count = sum(case.case_user_id == user_id for case in unban_cases)

    return (
        ban_count > unban_count
    )  # TODO: this implementation is flawed, if someone bans and unbans the same user multiple times, this will not work as expected
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")