231 lines
7.5 KiB
Python
231 lines
7.5 KiB
Python
import re
|
|
from datetime import datetime
|
|
|
|
from discord import TextChannel
|
|
from discord.ext import commands
|
|
from discord.utils import find
|
|
from discord_slash import SlashContext, cog_ext
|
|
from discord_slash.utils.manage_commands import create_option
|
|
|
|
import jarvis
|
|
from jarvis.config import get_config
|
|
from jarvis.data.unicode import emoji_list
|
|
from jarvis.utils.db import DBManager
|
|
|
|
|
|
class AutoReactCog(commands.Cog):
|
|
def __init__(self, bot):
|
|
self.bot = bot
|
|
config = get_config()
|
|
self.db = DBManager(config.mongo).mongo
|
|
self.custom_emote = re.compile(r"^<:\w+:(\d+)>$")
|
|
|
|
@cog_ext.cog_subcommand(
|
|
base="autoreact",
|
|
name="create",
|
|
description="Add an autoreact to a channel",
|
|
options=[
|
|
create_option(
|
|
name="channel",
|
|
description="Channel to monitor",
|
|
option_type=7,
|
|
required=True,
|
|
)
|
|
],
|
|
)
|
|
@commands.has_permissions(administrator=True)
|
|
async def _autoreact_create(self, ctx: SlashContext, channel: TextChannel):
|
|
if not isinstance(channel, TextChannel):
|
|
await ctx.send("Channel must be a text channel", hidden=True)
|
|
return
|
|
exists = self.db.jarvis.autoreact.find_one(
|
|
{"guild": ctx.guild.id, "channel": channel.id}
|
|
)
|
|
if exists:
|
|
await ctx.send(
|
|
f"Autoreact already exists for {channel.mention}.", hidden=True
|
|
)
|
|
return
|
|
|
|
autoreact = {
|
|
"guild": ctx.guild.id,
|
|
"channel": channel.id,
|
|
"reactions": [],
|
|
"admin": ctx.author.id,
|
|
"time": datetime.now(),
|
|
}
|
|
self.db.jarvis.autoreact.insert_one(autoreact)
|
|
await ctx.send(f"Autoreact created for {channel.mention}!")
|
|
|
|
@cog_ext.cog_subcommand(
|
|
base="autoreact",
|
|
name="delete",
|
|
description="Delete an autoreact from a channel",
|
|
options=[
|
|
create_option(
|
|
name="channel",
|
|
description="Channel to stop monitoring",
|
|
option_type=7,
|
|
required=True,
|
|
)
|
|
],
|
|
)
|
|
async def _autoreact_delete(self, ctx, channel: TextChannel):
|
|
exists = self.db.jarvis.autoreact.delete_one(
|
|
{"guild": channel.guild.id, "channel": channel.id}
|
|
)
|
|
if exists:
|
|
await ctx.send(f"Autoreact removed from {channel.mention}")
|
|
else:
|
|
await ctx.send(
|
|
f"Autoreact not found on {channel.mention}", hidden=True
|
|
)
|
|
|
|
@cog_ext.cog_subcommand(
|
|
base="autoreact",
|
|
name="add",
|
|
description="Add an autoreact emote to an existing autoreact",
|
|
options=[
|
|
create_option(
|
|
name="channel",
|
|
description="Autoreact channel to add emote to",
|
|
option_type=7,
|
|
required=True,
|
|
),
|
|
create_option(
|
|
name="emote",
|
|
description="Emote to add",
|
|
option_type=3,
|
|
required=True,
|
|
),
|
|
],
|
|
)
|
|
@commands.has_permissions(administrator=True)
|
|
async def _autoreact_add(self, ctx, channel: TextChannel, emote: str):
|
|
await ctx.defer()
|
|
custom_emoji = self.custom_emote.match(emote)
|
|
standard_emoji = emote in emoji_list
|
|
if not custom_emoji and not standard_emoji:
|
|
await ctx.send(
|
|
"Please use either an emote from this server"
|
|
+ " or a unicode emoji.",
|
|
hidden=True,
|
|
)
|
|
return
|
|
if custom_emoji:
|
|
emoji_id = int(custom_emoji.group(1))
|
|
if not find(lambda x: x.id == emoji_id, ctx.guild.emojis):
|
|
await ctx.send(
|
|
"Please use a custom emote from this server.", hidden=True
|
|
)
|
|
return
|
|
exists = self.db.jarvis.autoreact.find_one(
|
|
{"guild": ctx.guild.id, "channel": channel.id}
|
|
)
|
|
if not exists:
|
|
await ctx.send(
|
|
"Please create autoreact first with "
|
|
+ f"/autoreact create {channel.mention}"
|
|
)
|
|
return
|
|
if emote in exists["reactions"]:
|
|
await ctx.send(
|
|
f"Emote already added to {channel.mention} autoreactions.",
|
|
hidden=True,
|
|
)
|
|
return
|
|
if len(exists["reactions"]) >= 5:
|
|
await ctx.send(
|
|
"Max number of reactions hit. Remove a different one to add this one",
|
|
hidden=True,
|
|
)
|
|
return
|
|
exists["reactions"].append(emote)
|
|
self.db.jarvis.autoreact.update_one(
|
|
{"_id": exists["_id"]},
|
|
{"$set": {"reactions": exists["reactions"]}},
|
|
)
|
|
await ctx.send(f"Added {emote} to {channel.mention} autoreact.")
|
|
|
|
@cog_ext.cog_subcommand(
|
|
base="autoreact",
|
|
name="remove",
|
|
description="Remove an autoreact emote from an existing autoreact",
|
|
options=[
|
|
create_option(
|
|
name="channel",
|
|
description="Autoreact channel to remove emote from",
|
|
option_type=7,
|
|
required=True,
|
|
),
|
|
create_option(
|
|
name="emote",
|
|
description="Emote to remove",
|
|
option_type=3,
|
|
required=True,
|
|
),
|
|
],
|
|
)
|
|
@commands.has_permissions(administrator=True)
|
|
async def _autoreact_remove(self, ctx, channel: TextChannel, emote: str):
|
|
exists = self.db.jarvis.autoreact.find_one(
|
|
{"guild": ctx.guild.id, "channel": channel.id}
|
|
)
|
|
if not exists:
|
|
await ctx.send(
|
|
"Please create autoreact first with "
|
|
+ f"/autoreact create {channel.mention}",
|
|
hidden=True,
|
|
)
|
|
return
|
|
if emote not in exists["reactions"]:
|
|
await ctx.send(
|
|
f"{emote} not used in {channel.mention} autoreactions.",
|
|
hidden=True,
|
|
)
|
|
return
|
|
exists["reactions"].remove(emote)
|
|
self.db.jarvis.autoreact.update_one(
|
|
{"_id": exists["_id"]},
|
|
{"$set": {"reactions": exists["reactions"]}},
|
|
)
|
|
await ctx.send(f"Removed {emote} from {channel.mention} autoreact.")
|
|
|
|
@cog_ext.cog_subcommand(
|
|
base="autoreact",
|
|
name="list",
|
|
description="List all autoreacts on a channel",
|
|
options=[
|
|
create_option(
|
|
name="channel",
|
|
description="Autoreact channel to list",
|
|
option_type=7,
|
|
required=True,
|
|
),
|
|
],
|
|
)
|
|
@commands.has_permissions(administrator=True)
|
|
async def _autoreact_list(self, ctx, channel: TextChannel):
|
|
exists = self.db.jarvis.autoreact.find_one(
|
|
{"guild": ctx.guild.id, "channel": channel.id}
|
|
)
|
|
if not exists:
|
|
await ctx.send(
|
|
"Please create autoreact first with "
|
|
+ f"/autoreact create {channel.mention}",
|
|
hidden=True,
|
|
)
|
|
return
|
|
message = ""
|
|
if len(exists["reactions"]) > 0:
|
|
message = (
|
|
f"Current active autoreacts on {channel.mention}:\n"
|
|
+ "\n".join(exists["reactions"])
|
|
)
|
|
else:
|
|
message = f"No reactions set on {channel.mention}"
|
|
await ctx.send(message)
|
|
|
|
|
|
def setup(bot):
|
|
bot.add_cog(AutoReactCog(bot))
|