diff --git a/jarvis/cogs/admin.py b/jarvis/cogs/admin.py index e8feb1a..0f77dbb 100644 --- a/jarvis/cogs/admin.py +++ b/jarvis/cogs/admin.py @@ -1,10 +1,10 @@ import jarvis +from datetime import datetime from discord import User from discord.ext import commands -from discord_slash import cog_ext, SlashContext +from discord_slash import cog_ext from discord_slash.utils.manage_commands import create_option, create_choice from jarvis.utils.db import DBManager -from datetime import datetime class AdminCog(commands.Cog): diff --git a/jarvis/cogs/ctc2.py b/jarvis/cogs/ctc2.py index be1f878..2302fcc 100644 --- a/jarvis/cogs/ctc2.py +++ b/jarvis/cogs/ctc2.py @@ -1,13 +1,9 @@ -import jarvis -import re import aiohttp -from jarvis.config import get_config -from jarvis.utils import build_embed -from jarvis.utils.db import DBManager -from jarvis.utils.field import Field -from jarvis.data.dbrand import shipping_lookup +import jarvis from discord.ext import commands from discord_slash import cog_ext +from jarvis.config import get_config +from jarvis.utils.db import DBManager class CTCCog(commands.Cog): diff --git a/jarvis/cogs/dbrand.py b/jarvis/cogs/dbrand.py index c2408a6..bbb9278 100644 --- a/jarvis/cogs/dbrand.py +++ b/jarvis/cogs/dbrand.py @@ -1,13 +1,13 @@ +import aiohttp import jarvis import re -import aiohttp -from jarvis.config import get_config -from jarvis.utils import build_embed -from jarvis.utils.field import Field -from jarvis.data.dbrand import shipping_lookup from discord.ext import commands from discord_slash import cog_ext from discord_slash.utils.manage_commands import create_option +from jarvis.config import get_config +from jarvis.data.dbrand import shipping_lookup +from jarvis.utils import build_embed +from jarvis.utils.field import Field class DbrandCog(commands.Cog): diff --git a/jarvis/cogs/dev.py b/jarvis/cogs/dev.py index f715afd..e41eecf 100644 --- a/jarvis/cogs/dev.py +++ b/jarvis/cogs/dev.py @@ -1,22 +1,21 @@ -import jarvis -import hashlib -import uuid -import re -import ulid import base64 -import subprocess import discord -import sys +import hashlib +import jarvis import os +import re +import subprocess +import sys import traceback -from time import time -from inspect import getsource +import ulid +import uuid +from bson import ObjectId from discord.ext import commands from discord_slash import cog_ext -from jarvis.utils import build_embed, convert_bytesize +from inspect import getsource +from jarvis.utils import build_embed, convert_bytesize, user_is_bot_admin from jarvis.utils.field import Field -from bson import ObjectId -from jarvis.utils import user_is_bot_admin +from time import time supported_hashes = { diff --git a/jarvis/cogs/image.py b/jarvis/cogs/image.py index 83d9980..5657bc2 100644 --- a/jarvis/cogs/image.py +++ b/jarvis/cogs/image.py @@ -1,13 +1,13 @@ -import jarvis -from discord import File -import cv2 import aiohttp -import re +import cv2 +import jarvis import numpy as np -from jarvis.utils import convert_bytesize, unconvert_bytesize, build_embed -from jarvis.utils.field import Field +import re +from discord import File from discord.ext import commands from io import BytesIO +from jarvis.utils import convert_bytesize, unconvert_bytesize, build_embed +from jarvis.utils.field import Field class ImageCog(commands.Cog): diff --git a/jarvis/cogs/jokes.py b/jarvis/cogs/jokes.py index 7a55795..6a0fbfd 100644 --- a/jarvis/cogs/jokes.py +++ b/jarvis/cogs/jokes.py @@ -1,15 +1,15 @@ -import jarvis import discord -from random import randint import html +import jarvis import re import traceback +from datetime import datetime +from discord.ext import commands +from discord_slash import cog_ext from jarvis.utils import build_embed from jarvis.utils.db import DBManager from jarvis.utils.field import Field -from discord.ext import commands -from discord_slash import cog_ext -from datetime import datetime +from random import randint class JokeCog(commands.Cog): diff --git a/jarvis/cogs/owner.py b/jarvis/cogs/owner.py index e1f57e9..30ff35b 100644 --- a/jarvis/cogs/owner.py +++ b/jarvis/cogs/owner.py @@ -1,5 +1,5 @@ -import jarvis import discord +import jarvis from discord import User from discord.ext import commands from jarvis.config import get_config, reload_config diff --git a/jarvis/cogs/starboard.py b/jarvis/cogs/starboard.py new file mode 100644 index 0000000..a29817c --- /dev/null +++ b/jarvis/cogs/starboard.py @@ -0,0 +1,303 @@ +import aiohttp +import jarvis +from datetime import datetime +import discord +from discord import TextChannel, Message +from discord.ext import commands +from discord.utils import find +from discord_slash import cog_ext, SlashContext +from discord_slash.utils.manage_commands import create_option +from jarvis.config import get_config +from jarvis.utils import build_embed +from jarvis.utils.db import DBManager +from jarvis.utils.field import Field + + +class StarboardCog(commands.Cog): + def __init__(self, bot): + self.bot = bot + self.db = DBManager(get_config().mongo).mongo + + @cog_ext.cog_subcommand( + base="starboard", + name="list", + description="Lists all Starboards", + guild_ids=[418094694325813248, 578757004059738142], + ) + async def _list(self, ctx): + starboards = [ + x for x in self.db.jarvis.starboard.find({"guild": ctx.guild.id}) + ] + if starboards != []: + message = "Available Starboards:\n" + for s in starboards: + message += f"{s['name']}: <#{s['target']}>\n" + await ctx.send(message) + else: + await ctx.send("No Starboards available.") + + @cog_ext.cog_subcommand( + base="starboard", + name="create", + description="Create a starboard", + guild_ids=[418094694325813248, 578757004059738142], + options=[ + create_option( + name="name", + description="Starboard name", + option_type=3, + required=True, + ), + create_option( + name="target", + description="Target channel", + option_type=7, + required=True, + ), + ], + ) + async def _create(self, ctx, name: str, target: TextChannel): + if target not in ctx.guild.channels: + await ctx.send( + "Target channel not in guild. Choose an existing channel." + ) + return + exists = self.db.jarvis.starboard.find_one( + {"name": name, "guild": ctx.guild.id} + ) + if exists: + await ctx.send( + f"Starboard {name} already exists at <@{exists['channel']}>!" + ) + return + + self.db.jarvis.starboard.insert_one( + { + "name": name, + "guild": ctx.guild.id, + "target": target.id, + "admin": ctx.author.id, + "time": datetime.now(), + } + ) + await ctx.send( + f"Starboard `{name}` created! Check it out at {target.mention}." + ) + + @cog_ext.cog_subcommand( + base="starboard", + name="delete", + description="Delete a starboard", + guild_ids=[418094694325813248, 578757004059738142], + options=[ + create_option( + name="name", + description="Starboard name", + option_type=3, + required=True, + ), + ], + ) + async def _delete(self, ctx, name: str): + deleted = self.db.jarvis.starboard.delete_one( + { + "name": name, + "guild": ctx.guild.id, + } + ) + if deleted: + self.db.jarvis.stars.delete_many({"starboard": name}) + await ctx.send(f"Starboard `{name}` deleted!") + else: + await ctx.send(f"Starboard `{name}` not found!") + + @cog_ext.cog_subcommand( + base="starboard", + name="move", + description="Move a starboard", + guild_ids=[418094694325813248, 578757004059738142], + options=[ + create_option( + name="name", + description="Starboard name", + option_type=3, + required=True, + ), + create_option( + name="target", + description="New Starboard channel", + option_type=7, + required=True, + ), + ], + ) + async def _move(self, ctx, name: str, target: TextChannel): + exists = self.db.jarvis.starboard.find_one( + { + "name": name, + "guild": ctx.guild.id, + } + ) + if not exists: + await ctx.send( + f"Good news! Starboard `{name}` doesn't exist!\n" + + f"Run `/starboard create {name} {target.mention}` " + + "to create it!" + ) + return + exists["target"] = target.id + self.db.jarvis.starboard.update_one( + {"_id": exists["_id"]}, {"$set": exists} + ) + await ctx.send(f"Starboard `{name}` moved to {target.mention}!") + + @cog_ext.cog_subcommand( + base="starboard", + name="rename", + description="Move a starboard", + guild_ids=[418094694325813248, 578757004059738142], + options=[ + create_option( + name="name", + description="Starboard name", + option_type=3, + required=True, + ), + create_option( + name="new", + description="New Starboard name", + option_type=3, + required=True, + ), + ], + ) + async def _rename(self, ctx, name: str, new: str): + old_exists = self.db.jarvis.starboard.find_one( + {"name": name, "guild": ctx.guild.id} + ) + if not old_exists: + await ctx.send( + f"Good news! Starboard `{name}` doesn't exist!\n" + + f"Run `/starboard create {new} ` " + + "to create your new channel!" + ) + return + new_exists = self.db.jarvis.starboard.find_one( + {"name": new, "guild": ctx.guild.id} + ) + if new_exists: + await ctx.send(f"Starboard `{new}` already exists!") + return + + old_exists["name"] = new + self.db.jarvis.starboard.update_one( + {"_id": old_exists["_id"]}, {"$set": old_exists} + ) + await ctx.send(f"Starboard `{name}` moved renamed to `{name}`!") + + @cog_ext.cog_subcommand( + base="star", + name="add", + description="Star a message", + guild_ids=[418094694325813248, 578757004059738142], + options=[ + create_option( + name="message", + description="Message to star", + option_type=3, + required=True, + ), + create_option( + name="starboard", + description="Starboard to send message to", + option_type=3, + required=True, + ), + create_option( + name="channel", + description="Channel that has the message, " + + "required if different than command message", + option_type=7, + required=False, + ), + ], + ) + async def _star_add( + self, + ctx: SlashContext, + message: str, + starboard: str, + channel: TextChannel = None, + ): + if not channel: + channel = ctx.channel + exists = self.db.jarvis.starboard.find_one( + {"name": starboard, "guild": ctx.guild.id} + ) + if not exists: + await ctx.send( + f"Starboard `{starboard}` does not exist! " + + "Please create it first" + ) + return + + message = await channel.fetch_message(int(message)) + guild_channels = await ctx.guild.fetch_channels() + target = find(lambda x: x.id == exists["target"], guild_channels) + + if not target: + await ctx.send("Could not find Starboard channel!") + return + + exists = self.db.jarvis.stars.find_one( + { + "message": message.id, + "channel": message.channel.id, + "guild": message.guild.id, + "starboard": starboard, + } + ) + + if exists: + await ctx.send(f"Message already sent to Starboard `{starboard}`!") + return + + content = message.content + + embed = build_embed( + title="Click Here to view context", + description=content, + fields=[], + url=message.jump_url, + ) + embed.set_author( + name=message.author.name, + url=message.jump_url, + icon_url=message.author.avatar_url, + ) + embed.set_footer( + text=message.guild.name + " | " + message.channel.name + ) + + star = await target.send(embed=embed) + + self.db.jarvis.stars.insert_one( + { + "message": message.id, + "channel": message.channel.id, + "guild": message.guild.id, + "starboard": starboard, + "admin": ctx.author.id, + "time": datetime.now(), + "star": star.id, + } + ) + + await ctx.send( + f"Message saved to Starboard `{starboard}`!\n" + + f"See it in {target.mention}" + ) + + +def setup(bot): + bot.add_cog(StarboardCog(bot)) diff --git a/jarvis/cogs/util.py b/jarvis/cogs/util.py index e7de18a..ddb3fe5 100644 --- a/jarvis/cogs/util.py +++ b/jarvis/cogs/util.py @@ -1,9 +1,9 @@ import jarvis +from discord.ext import commands +from discord_slash import cog_ext from jarvis import jarvis_self, config, logo from jarvis.utils import convert_bytesize, build_embed, get_repo_hash from jarvis.utils.field import Field -from discord.ext import commands -from discord_slash import cog_ext, SlashContext class UtilCog(commands.Cog): diff --git a/schema.yaml b/schema.yaml index 46939f6..f381454 100644 --- a/schema.yaml +++ b/schema.yaml @@ -70,8 +70,9 @@ jarvis: time: datetime starboard: + name: String, name of starboard guild: Guild ID - channel: Channel ID + target: Channel ID, target channel admin: User ID, admin who created time: Datetime