Add support for images in starboard, limit starboard to admins, closes #18

This commit is contained in:
Zeva Rose 2021-07-01 11:43:33 -06:00
parent 3f3bbd9032
commit a59976544f

View file

@ -12,6 +12,14 @@ from jarvis.utils import build_embed
from jarvis.utils.db import DBManager from jarvis.utils.db import DBManager
from jarvis.utils.field import Field from jarvis.utils.field import Field
supported_images = [
"image/png",
"image/gif",
"image/jpeg",
"image/webp",
"image/svg",
]
class StarboardCog(commands.Cog): class StarboardCog(commands.Cog):
def __init__(self, bot): def __init__(self, bot):
@ -24,6 +32,7 @@ class StarboardCog(commands.Cog):
description="Lists all Starboards", description="Lists all Starboards",
guild_ids=[418094694325813248, 578757004059738142], guild_ids=[418094694325813248, 578757004059738142],
) )
@commands.has_permissions(administrator=True)
async def _list(self, ctx): async def _list(self, ctx):
starboards = [ starboards = [
x for x in self.db.jarvis.starboard.find({"guild": ctx.guild.id}) x for x in self.db.jarvis.starboard.find({"guild": ctx.guild.id})
@ -56,6 +65,7 @@ class StarboardCog(commands.Cog):
), ),
], ],
) )
@commands.has_permissions(administrator=True)
async def _create(self, ctx, name: str, target: TextChannel): async def _create(self, ctx, name: str, target: TextChannel):
if target not in ctx.guild.channels: if target not in ctx.guild.channels:
await ctx.send( await ctx.send(
@ -98,6 +108,7 @@ class StarboardCog(commands.Cog):
), ),
], ],
) )
@commands.has_permissions(administrator=True)
async def _delete(self, ctx, name: str): async def _delete(self, ctx, name: str):
deleted = self.db.jarvis.starboard.delete_one( deleted = self.db.jarvis.starboard.delete_one(
{ {
@ -131,6 +142,7 @@ class StarboardCog(commands.Cog):
), ),
], ],
) )
@commands.has_permissions(administrator=True)
async def _move(self, ctx, name: str, target: TextChannel): async def _move(self, ctx, name: str, target: TextChannel):
exists = self.db.jarvis.starboard.find_one( exists = self.db.jarvis.starboard.find_one(
{ {
@ -171,6 +183,7 @@ class StarboardCog(commands.Cog):
), ),
], ],
) )
@commands.has_permissions(administrator=True)
async def _rename(self, ctx, name: str, new: str): async def _rename(self, ctx, name: str, new: str):
old_exists = self.db.jarvis.starboard.find_one( old_exists = self.db.jarvis.starboard.find_one(
{"name": name, "guild": ctx.guild.id} {"name": name, "guild": ctx.guild.id}
@ -222,6 +235,7 @@ class StarboardCog(commands.Cog):
), ),
], ],
) )
@commands.has_permissions(administrator=True)
async def _star_add( async def _star_add(
self, self,
ctx: SlashContext, ctx: SlashContext,
@ -264,11 +278,22 @@ class StarboardCog(commands.Cog):
content = message.content content = message.content
attachments = message.attachments
image_url = None
if attachments:
for attachment in attachments:
if attachment.content_type in supported_images:
image_url = attachment.url
break
if not content and image_url:
content = "\u200b"
embed = build_embed( embed = build_embed(
title="Click Here to view context", title="Click Here to view context",
description=content, description=content,
fields=[], fields=[],
url=message.jump_url, url=message.jump_url,
timestamp=message.created_at,
) )
embed.set_author( embed.set_author(
name=message.author.name, name=message.author.name,
@ -278,6 +303,8 @@ class StarboardCog(commands.Cog):
embed.set_footer( embed.set_footer(
text=message.guild.name + " | " + message.channel.name text=message.guild.name + " | " + message.channel.name
) )
if image_url:
embed.set_image(url=image_url)
star = await target.send(embed=embed) star = await target.send(embed=embed)