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

Migrated poll cog to new extension format #245

Merged
merged 23 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
61b46e1
Fixed alignment in the rank card and improved trunacation
FirePlank Oct 25, 2023
b5a2e6a
Migrated poll cog to new extension format
FirePlank Oct 25, 2023
ff933c8
Migrated poll cog to new extension format
FirePlank Oct 26, 2023
fc181b3
Merge branch 'dev' of https://github.com/FirePlank/Discord-Bot into dev
FirePlank Oct 26, 2023
8d9c5e2
Fixed events and delete message after poll creation
FirePlank Oct 26, 2023
003e2c0
Update commands.py
FirePlank Oct 26, 2023
4de940b
Rename extension to polls
FirePlank Oct 26, 2023
a12979f
Added views.py
FirePlank Oct 26, 2023
0062559
linting tools
FirePlank Oct 26, 2023
de91f3b
Added poll_check to commands.py
FirePlank Oct 26, 2023
31991c2
slight fix
FirePlank Oct 26, 2023
62bfacb
Maded erros ephemeral, renamed variable and maintained wording consis…
FirePlank Oct 26, 2023
86fc558
linting tools
FirePlank Oct 26, 2023
81d26f2
Replaced try and except mess in show poll command with MessageTransfo…
FirePlank Oct 27, 2023
5a774c7
Did the suggested changes requested by Sylte
FirePlank Oct 27, 2023
7374de6
Fixed IgnorableException being sent to errors webhook
FirePlank Oct 27, 2023
cb4efd6
Did the suggested changes
FirePlank Oct 30, 2023
07ce16f
Did the suggested changes
FirePlank Oct 30, 2023
a38bc52
Added utils.py file
FirePlank Nov 1, 2023
981e843
Fixes
SylteA Nov 1, 2023
bfd14f3
Progress on new delete menu and updated button states
SylteA Nov 1, 2023
948c2b5
Update MessageTransformer
SylteA Nov 1, 2023
a70c7df
Fix
HETHAT Nov 2, 2023
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
151 changes: 0 additions & 151 deletions bot/cogs/poll.py

This file was deleted.

3 changes: 2 additions & 1 deletion bot/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ async def process_commands(self, message: discord.Message, /):

async def on_app_command_error(self, interaction: "InteractionType", error: app_commands.AppCommandError):
"""Handle errors in app commands."""
if isinstance(error, IgnorableException):

if isinstance(error.__cause__, IgnorableException):
FirePlank marked this conversation as resolved.
Show resolved Hide resolved
return

if interaction.command is None:
Expand Down
7 changes: 2 additions & 5 deletions bot/extensions/levelling/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,13 @@ def convert_int(integer):
xp_offset_x -= xp_text_size[2] - xp_text_size[0]
draw.text((xp_offset_x, xp_offset_y), text, font=self.small_font, fill="#fff")

if len(username) >= 15:
if len(username) >= 18:
# Truncating the name
username = username[:15] + "..."

text_bbox = draw.textbbox((0, 0), username, font=self.medium_font)
text_offset_x = bar_offset_x - 10
text_offset_y = bar_offset_y - (text_bbox[3] - text_bbox[1]) - 20
draw.text((text_offset_x, text_offset_y), username, font=self.medium_font, fill="#fff")
draw.text((bar_offset_x, text_offset_y), username, font=self.medium_font, fill="#fff")

# create copy of background
background = self.background.copy()
Expand Down Expand Up @@ -274,8 +273,6 @@ async def rank(self, interaction: core.InteractionType, member: discord.Member =

record = await LevellingUser.pool.fetchrow(query, interaction.guild.id, member.id)

log.info(record)

if record.total_xp is None:
if member.id == interaction.user.id:
return await interaction.response.send_message(
Expand Down
9 changes: 9 additions & 0 deletions bot/extensions/polls/__init__.py
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))
97 changes: 97 additions & 0 deletions bot/extensions/polls/commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import discord
from discord import app_commands
from discord.ext import commands

from bot import core
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

@property
def reactions(self):
return {
FirePlank marked this conversation as resolved.
Show resolved Hide resolved
1: "1️⃣",
2: "2️⃣",
3: "3️⃣",
4: "4️⃣",
5: "5️⃣",
6: "6️⃣",
7: "7️⃣",
8: "8️⃣",
9: "9️⃣",
10: "🔟",
}

def poll_check(self, message: discord.Message):
if not message.embeds:
return False

embed = message.embeds[0]
if str(embed.footer.text).count("Poll by") == 1:
return message.author == self.bot.user

@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=CreatePollView())

@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 self.poll_check(message):
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 self.reactions.values() 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: commands.Bot):
await bot.add_cog(Polls(bot=bot))
42 changes: 42 additions & 0 deletions bot/extensions/polls/events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import discord
from discord.ext import commands

from bot import core


class PollEvents(commands.Cog):
"""Events for polls in discord."""

def __init__(self, bot: core.DiscordBot):
self.bot = bot
self.emojis = ["1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣", "9️⃣", "🔟"]

def poll_check(self, message: discord.Message):
if not message.embeds:
return False

embed = message.embeds[0]
if str(embed.footer.text).count("Poll by") == 1:
return message.author == self.bot.user

@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 self.poll_check(message):
return

if str(payload.emoji) not in self.emojis:
return

for reaction in message.reactions:
if str(reaction) not in self.emojis:
return

if str(reaction.emoji) != str(payload.emoji):
user = self.bot.get_user(payload.user_id)
await message.remove_reaction(reaction.emoji, user)
Loading
Loading