Skip to content

Commit

Permalink
Merge pull request #244 from SylteA/revert-243-dev
Browse files Browse the repository at this point in the history
Revert "Fixed alignment in the rank card and improved trunacation"
  • Loading branch information
SylteA authored Oct 25, 2023
2 parents ab9b6b3 + a971990 commit 036f993
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 249 deletions.
151 changes: 151 additions & 0 deletions bot/cogs/poll.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import discord
from discord.ext import commands


class Polls(commands.Cog):
def __init__(self, bot: commands.AutoShardedBot):
self.__bot = bot

@property
def reactions(self):
return {
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):
try:
embed = message.embeds[0]
except Exception:
return False
if str(embed.footer.text).count("Poll by") == 1:
return message.author == self.__bot.user
return False

@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

emojis = list(self.reactions.values())
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)

@commands.group()
async def poll(self, ctx):
"""Polls"""
if ctx.invoked_subcommand is None:
return await ctx.send_help(self.__bot.get_command("poll"))

@poll.command()
@commands.cooldown(1, 10, commands.BucketType.channel)
async def new(self, ctx, desc: str, *choices):
"""Create a new poll"""
await ctx.message.delete()

if len(choices) < 2:
ctx.command.reset_cooldown(ctx)
if len(choices) == 1:
return await ctx.send("Can't make a poll with only one choice")
return await ctx.send("You have to enter two or more choices to make a poll")

if len(choices) > 10:
ctx.command.reset_cooldown(ctx)
return await ctx.send("You can't make a poll with more than 10 choices")

embed = discord.Embed(
description=f"**{desc}**\n\n"
+ "\n\n".join(f"{str(self.reactions[i])} {choice}" for i, choice in enumerate(choices, 1)),
timestamp=discord.utils.utcnow(),
color=discord.colour.Color.gold(),
)
embed.set_footer(text=f"Poll by {str(ctx.author)}")
msg = await ctx.send(embed=embed)
for i in range(1, len(choices) + 1):
await msg.add_reaction(self.reactions[i])

@poll.command()
async def show(self, ctx, message: str):
"""Show a poll result"""
await ctx.message.delete()

try:
*_, channel_id, msg_id = message.split("/")

try:
channel = self.__bot.get_channel(int(channel_id))
message = await channel.fetch_message(int(msg_id))
except Exception:
return await ctx.send("Please provide the message ID/link for a valid poll")
except Exception:
try:
message = await ctx.channel.fetch_message(message)
except Exception:
return await ctx.send("Please provide the message ID/link for a valid poll")

if self.poll_check(message):
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 = list(
map(
lambda o: " ".join(o.split()[1:]),
poll_embed.description.split("1️")[1].split("\n\n"),
)
)
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 ctx.send(embed=embed)

return await ctx.send("Please provide the message ID/link for a valid poll")


async def setup(bot):
await bot.add_cog(Polls(bot))
7 changes: 5 additions & 2 deletions bot/extensions/levelling/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,14 @@ 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) >= 18:
if len(username) >= 15:
# 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((bar_offset_x, text_offset_y), username, font=self.medium_font, fill="#fff")
draw.text((text_offset_x, text_offset_y), username, font=self.medium_font, fill="#fff")

# create copy of background
background = self.background.copy()
Expand Down Expand Up @@ -273,6 +274,8 @@ 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: 0 additions & 9 deletions bot/extensions/poll/__init__.py

This file was deleted.

203 changes: 0 additions & 203 deletions bot/extensions/poll/commands.py

This file was deleted.

Loading

0 comments on commit 036f993

Please sign in to comment.