LITERALLY THE WORST CODE I HAVE EVER WRITTEN BUT IT SOMEHOW WORKS I THINK

pull/9/head
sgoudham 4 years ago
parent 7f6422c614
commit 4db28b100d

@ -20,18 +20,24 @@ from discord import TextChannel, Embed, NotFound
from discord.ext.commands import Cog, group, bot_has_permissions, has_permissions from discord.ext.commands import Cog, group, bot_has_permissions, has_permissions
def is_url_spoiler(self, text, url):
spoilers = self.spoilers.findall(text)
for spoiler in spoilers:
if url in spoiler:
return True
return False
async def send_starboard_and_update_db(self, payload, action): async def send_starboard_and_update_db(self, payload, action):
"""Send the starboard embed and update database/cache""" """Send the starboard embed and update database/cache"""
if (starboard := self.bot.get_starboard(payload.guild_id)) and payload.emoji.name == "": if (starboard := self.bot.get_starboard_channel(payload.guild_id)) and payload.emoji.name == "":
message = await self.bot.get_channel(payload.channel_id).fetch_message(payload.message_id) message = await self.bot.get_channel(payload.channel_id).fetch_message(payload.message_id)
if action == "added": # Making sure the variables are right for each react event
check = not message.author.bot and payload.member.id != message.author.id user = payload.member.id if action == "added" else payload.user_id
else:
check = not message.author.bot
if check: if not message.author.bot and user != message.author.id:
channel = self.bot.get_channel(starboard) channel = self.bot.get_channel(starboard)
msg_id, stars = await self.bot.check_starboard_messages_cache(message.id, payload.guild_id) msg_id, stars = await self.bot.check_starboard_messages_cache(message.id, payload.guild_id)
new_stars = stars + 1 if action == "added" else stars - 1 new_stars = stars + 1 if action == "added" else stars - 1
@ -46,10 +52,18 @@ async def send_starboard_and_update_db(self, payload, action):
value=f"**Channel:** {message.channel.mention}\n[Jump To Message]({message.jump_url})", value=f"**Channel:** {message.channel.mention}\n[Jump To Message]({message.jump_url})",
inline=False) inline=False)
if len(message.attachments): if message.attachments:
embed.set_image(url=message.attachments[0].url) file = message.attachments[0]
spoiler = file.is_spoiler()
if not spoiler and file.url.lower().endswith(('png', 'jpeg', 'jpg', 'gif', 'webp')):
embed.set_image(url=file.url)
elif spoiler:
embed.add_field(name='Attachment', value=f'||[{file.filename}]({file.url})||', inline=False)
else:
embed.add_field(name='Attachment', value=f'[{file.filename}]({file.url})', inline=False)
if not stars: if not stars:
if new_stars >= self.bot.get_starboard_min_stars(payload.guild_id) and not msg_id:
star_message = await channel.send(embed=embed) star_message = await channel.send(embed=embed)
# Setup up pool connection # Setup up pool connection
@ -58,9 +72,14 @@ async def send_starboard_and_update_db(self, payload, action):
# Insert the starboard message in the database # Insert the starboard message in the database
try: try:
if new_stars >= self.bot.get_starboard_min_stars(payload.guild_id):
update_query = """INSERT INTO starboard_messages (root_message_id, guild_id, star_message_id) update_query = """INSERT INTO starboard_messages (root_message_id, guild_id, star_message_id)
VALUES ($1, $2, $3)""" VALUES ($1, $2, $3)"""
await conn.execute(update_query, message.id, payload.guild_id, star_message.id) await conn.execute(update_query, message.id, payload.guild_id, star_message.id)
else:
update_query = """INSERT INTO starboard_messages (root_message_id, guild_id)
VALUES ($1, $2)"""
await conn.execute(update_query, message.id, payload.guild_id)
# Catch errors # Catch errors
except asyncpg.PostgresError as e: except asyncpg.PostgresError as e:
@ -70,12 +89,18 @@ async def send_starboard_and_update_db(self, payload, action):
# Update cache # Update cache
else: else:
if new_stars >= self.bot.get_starboard_min_stars(payload.guild_id):
self.bot.cache_store_starboard_message(message.id, payload.guild_id, star_message.id) self.bot.cache_store_starboard_message(message.id, payload.guild_id, star_message.id)
else:
self.bot.cache_store_starboard_message(message.id, payload.guild_id, None)
# Release connection back to pool # Release connection back to pool
finally: finally:
await pool.release(conn) await pool.release(conn)
else:
if new_stars >= self.bot.get_starboard_min_stars(payload.guild_id) and not msg_id:
star_message = await channel.send(embed=embed)
else: else:
try: try:
star_message = await channel.fetch_message(msg_id) star_message = await channel.fetch_message(msg_id)
@ -89,6 +114,12 @@ async def send_starboard_and_update_db(self, payload, action):
# Update the stars that the message has in the database # Update the stars that the message has in the database
try: try:
if new_stars >= self.bot.get_starboard_min_stars(payload.guild_id):
update_query = """UPDATE starboard_messages
SET stars = $1, star_message_id = $2
WHERE root_message_id = $3 AND guild_id = $4"""
await conn.execute(update_query, new_stars, star_message.id, message.id, payload.guild_id)
else:
update_query = """UPDATE starboard_messages update_query = """UPDATE starboard_messages
SET stars = $1 SET stars = $1
WHERE root_message_id = $2 AND guild_id = $3""" WHERE root_message_id = $2 AND guild_id = $3"""
@ -102,6 +133,8 @@ async def send_starboard_and_update_db(self, payload, action):
# Update cache # Update cache
else: else:
if new_stars >= self.bot.get_starboard_min_stars(payload.guild_id):
self.bot.cache_store_starboard_message(message.id, payload.guild_id, star_message.id)
self.bot.update_starboard_message(message.id, payload.guild_id, new_stars) self.bot.update_starboard_message(message.id, payload.guild_id, new_stars)
# Release connection back to pool # Release connection back to pool
@ -134,7 +167,7 @@ async def get_starboard_from_db(self, ctx, action):
return None return None
elif (action == "update" or action == "delete") and not result: elif (action == "update" or action == "delete") and not result:
text = "**Starboard** Not Setup!" \ text = "**Starboard** Not Setup!" \
f"\nDo **{ctx.prefix}help modlogs** to find out more!" f"\nDo **{ctx.prefix}help starboard** to find out more!"
await self.bot.generate_embed(ctx, desc=text) await self.bot.generate_embed(ctx, desc=text)
return None return None
@ -160,6 +193,43 @@ class Starboard(Cog):
Starboard! Let the community star messages! Starboard! Let the community star messages!
""" """
@starboard.command(name="stars")
@bot_has_permissions(embed_links=True)
async def sb_min_stars(self, ctx, stars: int):
"""Update the minimum amount of stars needed for the message to appear on the starboard"""
if await get_starboard_from_db(self, ctx, "update") and stars > 0:
# Setup up pool connection
pool = self.bot.db
async with pool.acquire() as conn:
# Update the starboard min_stars in the database
try:
update_query = """UPDATE starboard SET min_stars = $1 WHERE guild_id = $2 RETURNING channel_id"""
await conn.execute(update_query, stars, ctx.guild.id)
# Catch errors
except asyncpg.PostgresError as e:
print(f"PostGres Error: Starboard Record Could Not Be Updated For Guild {ctx.guild.id}", e)
# Send confirmation that the channel has been updated
else:
star_channel = self.bot.get_starboard_channel(ctx.guild.id)
channel = self.bot.get_channel(star_channel)
text = "**Minimum Stars Updated!**" \
f"\nMessages will now need {stars} :star: to appear in {channel.mention}"
await self.bot.generate_embed(ctx, desc=text)
# Update cache
self.bot.update_starboard_min_stars(ctx.guild.id, stars)
# Release connection back to pool
finally:
await pool.release(conn)
elif stars <= 0:
await self.bot.generate_embed(ctx, desc="Minimum Stars Must Be Over or Equal to 1!")
@starboard.command(name="setup") @starboard.command(name="setup")
@bot_has_permissions(embed_links=True) @bot_has_permissions(embed_links=True)
async def sb_setup(self, ctx, starboard_channel: TextChannel): async def sb_setup(self, ctx, starboard_channel: TextChannel):
@ -224,7 +294,7 @@ class Starboard(Cog):
await self.bot.generate_embed(ctx, desc=text) await self.bot.generate_embed(ctx, desc=text)
# Update cache # Update cache
self.bot.update_starboard(ctx.guild.id, starboard_channel.id) self.bot.update_starboard_channel(ctx.guild.id, starboard_channel.id)
# Release connection back to pool # Release connection back to pool
finally: finally:

Loading…
Cancel
Save