-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #245 from FirePlank/dev
Migrated poll cog to new extension format
- Loading branch information
Showing
12 changed files
with
270 additions
and
176 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from bot.core import DiscordBot | ||
|
||
from .commands import Polls | ||
from .events import PollEvents | ||
|
||
|
||
async def setup(bot: DiscordBot) -> None: | ||
await bot.add_cog(Polls(bot=bot)) | ||
await bot.add_cog(PollEvents(bot=bot)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import discord | ||
from discord import app_commands | ||
from discord.ext import commands | ||
|
||
from bot import core | ||
from bot.extensions.polls.utils import emojis, poll_check | ||
from bot.extensions.polls.views import CreatePollView | ||
from utils.transformers import MessageTransformer | ||
|
||
|
||
class Polls(commands.GroupCog, group_name="poll"): | ||
def __init__(self, bot: core.DiscordBot): | ||
self.bot = bot | ||
|
||
self._create_poll_view = CreatePollView(timeout=None) | ||
self.bot.add_view(self._create_poll_view) | ||
|
||
@app_commands.command() | ||
@app_commands.describe(question="Your question") | ||
async def new(self, interaction: core.InteractionType, question: str): | ||
"""Create a new poll""" | ||
|
||
embed = discord.Embed( | ||
description=f"**{question}**\n\n", | ||
timestamp=discord.utils.utcnow(), | ||
color=discord.colour.Color.gold(), | ||
) | ||
embed.set_footer(text=f"Poll by {interaction.user.display_name}") | ||
await interaction.response.send_message(embed=embed, ephemeral=True, view=self._create_poll_view) | ||
|
||
@app_commands.command() | ||
async def show( | ||
self, | ||
interaction: core.InteractionType, | ||
message: app_commands.Transform[discord.Message, MessageTransformer], | ||
ephemeral: bool = True, | ||
): | ||
"""Show a poll result""" | ||
|
||
if not poll_check(message, self.bot.user): | ||
return await interaction.response.send_message("Please provide a valid poll message", ephemeral=True) | ||
|
||
poll_embed = message.embeds[0] | ||
reactions = message.reactions | ||
reactions_total = sum([reaction.count - 1 if str(reaction.emoji) in emojis else 0 for reaction in reactions]) | ||
|
||
options = [field.name for field in poll_embed.fields] | ||
desc = poll_embed.description.split("1️")[0] | ||
|
||
embed = discord.Embed( | ||
description=desc, | ||
timestamp=poll_embed.timestamp, | ||
color=discord.Color.gold(), | ||
) | ||
|
||
for i, option in enumerate(options): | ||
reaction_count = reactions[i].count - 1 | ||
indicator = "░" * 20 | ||
if reactions_total != 0: | ||
indicator = "█" * int(((reaction_count / reactions_total) * 100) / 5) + "░" * int( | ||
(((reactions_total - reaction_count) / reactions_total) * 100) / 5 | ||
) | ||
|
||
embed.add_field( | ||
name=option, | ||
value=f"{indicator} {int((reaction_count / (reactions_total or 1)*100))}%" | ||
f" (**{reaction_count} votes**)", | ||
inline=False, | ||
) | ||
|
||
embed.set_footer(text="Poll Result") | ||
return await interaction.response.send_message(embed=embed, ephemeral=ephemeral) | ||
|
||
|
||
async def setup(bot: core.DiscordBot): | ||
await bot.add_cog(Polls(bot=bot)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import discord | ||
from discord.ext import commands | ||
|
||
from bot import core | ||
from bot.extensions.polls.utils import emojis, poll_check | ||
|
||
|
||
class PollEvents(commands.Cog): | ||
"""Events for polls in discord.""" | ||
|
||
def __init__(self, bot: core.DiscordBot): | ||
self.bot = bot | ||
|
||
@commands.Cog.listener() | ||
async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent): | ||
channel: discord.TextChannel = self.bot.get_channel(payload.channel_id) | ||
message: discord.Message = await channel.fetch_message(payload.message_id) | ||
|
||
if payload.user_id == self.bot.user.id: | ||
return | ||
|
||
if not poll_check(message, self.bot.user): | ||
return | ||
|
||
if str(payload.emoji) not in emojis: | ||
return | ||
|
||
for reaction in message.reactions: | ||
if str(reaction) not in emojis: | ||
return | ||
|
||
if str(reaction.emoji) != str(payload.emoji): | ||
user = self.bot.get_user(payload.user_id) | ||
await message.remove_reaction(reaction.emoji, user) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import discord | ||
|
||
emojis = ["1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣", "8️⃣", "9️⃣", "🔟"] | ||
|
||
|
||
def poll_check(message: discord.Message, bot: discord.ClientUser): | ||
if not message.embeds: | ||
return False | ||
|
||
embed = message.embeds[0] | ||
if str(embed.footer.text).count("Poll by") == 1: | ||
return message.author == bot |
Oops, something went wrong.