From feda3036ab69c7f488bb54e17ab52a6227041a18 Mon Sep 17 00:00:00 2001 From: Zevaryx Date: Mon, 26 Jul 2021 19:10:31 -0600 Subject: [PATCH] Fix events, also close #59 --- jarvis/events/guild.py | 39 +++-- jarvis/events/member.py | 35 +++-- jarvis/events/message.py | 324 ++++++++++++++++++++++----------------- 3 files changed, 226 insertions(+), 172 deletions(-) diff --git a/jarvis/events/guild.py b/jarvis/events/guild.py index 15528ce..708ffae 100644 --- a/jarvis/events/guild.py +++ b/jarvis/events/guild.py @@ -2,25 +2,30 @@ import asyncio from discord.utils import find -from jarvis import jarvis from jarvis.db.types import Setting -@jarvis.event -async def on_guild_join(guild): - general = find(lambda x: x.name == "general", guild.channels) - if general and general.permissions_for(guild.me).send_messages: - await general.send( - "Allow me to introduce myself. I am J.A.R.V.I.S., a virtual " - + "artificial intelligence, and I'm here to assist you with a " - + "variety of tasks as best I can, " - + "24 hours a day, seven days a week." - ) - await asyncio.sleep(1) - await general.send("Importing all preferences from home interface...") +class GuildEventHandler(object): + def __init__(self, bot): + self.bot = bot + self.bot.add_listener(self.on_guild_join) - # Set some default settings - setting = Setting(guild=guild.id, setting="massmention", value=5) - setting.insert() + async def on_guild_join(self, guild): + general = find(lambda x: x.name == "general", guild.channels) + if general and general.permissions_for(guild.me).send_messages: + await general.send( + "Allow me to introduce myself. I am J.A.R.V.I.S., a virtual " + + "artificial intelligence, and I'm here to assist you with a " + + "variety of tasks as best I can, " + + "24 hours a day, seven days a week." + ) + await asyncio.sleep(1) + await general.send( + "Importing all preferences from home interface..." + ) - await general.send("Systems are now fully operational") + # Set some default settings + setting = Setting(guild=guild.id, setting="massmention", value=5) + setting.insert() + + await general.send("Systems are now fully operational") diff --git a/jarvis/events/member.py b/jarvis/events/member.py index d706f62..91a0094 100644 --- a/jarvis/events/member.py +++ b/jarvis/events/member.py @@ -1,20 +1,25 @@ from discord import Member -from jarvis import jarvis from jarvis.db.types import Mute, Setting -@jarvis.event -async def on_member_join(user: Member): - guild = user.guild - mutes = Mute.get_active(guild=guild.id) - if mutes and len(mutes) >= 1: - mute_role = Setting.get(guild=guild.id, setting="mute") - role = guild.get_role(mute_role.value) - await user.add_roles( - role, reason="User is muted still muted from prior mute" - ) - unverified = Setting.get(guild=guild.id, setting="unverified") - if unverified: - role = guild.get_role(unverified.value) - await user.add_roles(role, reason="User just joined and is unverified") +class MemberEventHandler(object): + def __init__(self, bot): + self.bot = bot + self.bot.add_listener(self.on_member_join) + + async def on_member_join(self, user: Member): + guild = user.guild + mutes = Mute.get_active(guild=guild.id) + if mutes and len(mutes) >= 1: + mute_role = Setting.get(guild=guild.id, setting="mute") + role = guild.get_role(mute_role.value) + await user.add_roles( + role, reason="User is muted still muted from prior mute" + ) + unverified = Setting.get(guild=guild.id, setting="unverified") + if unverified: + role = guild.get_role(unverified.value) + await user.add_roles( + role, reason="User just joined and is unverified" + ) diff --git a/jarvis/events/message.py b/jarvis/events/message.py index d148b57..df89fff 100644 --- a/jarvis/events/message.py +++ b/jarvis/events/message.py @@ -3,9 +3,8 @@ import re from discord import DMChannel, Message from discord.utils import find -from jarvis import jarvis from jarvis.config import get_config -from jarvis.db.types import Autopurge, Autoreact, Setting +from jarvis.db.types import Autopurge, Autoreact, Roleping, Setting, Warning from jarvis.utils import build_embed from jarvis.utils.field import Field @@ -15,57 +14,182 @@ invites = re.compile( ) -async def autopurge(message): - autopurge = Autopurge.get( - guild=message.guild.id, channel=message.channel.id - ) - if autopurge: - await message.delete(delay=autopurge.delay) +class MessageEventHandler(object): + def __init__(self, bot): + self.bot = bot + self.bot.add_listener(self.on_message) - -async def autoreact(message): - autoreact = Autoreact.get( - guild=message.guild.id, - channel=message.channel.id, - ) - if autoreact: - for reaction in autoreact.reactions: - await message.add_reaction(reaction) - - -async def checks(message): - # #tech - channel = find( - lambda x: x.id == 599068193339736096, message.channel_mentions - ) - if channel and message.author.id == 293795462752894976: - await channel.send( - content="https://cdn.discordapp.com/attachments/" - + "664621130044407838/805218508866453554/tech.gif" + async def autopurge(self, message: Message): + autopurge = Autopurge.get( + guild=message.guild.id, channel=message.channel.id ) - content = re.sub(r"\s+", "", message.content) - match = invites.search(content) - if match: - guild_invites = await message.guild.invites() - allowed = [x.code for x in guild_invites] + [ - "dbrand", - "VtgZntXcnZ", - ] - if match.group(1) not in allowed: - await message.delete() + if autopurge: + await message.delete(delay=autopurge.delay) + + async def autoreact(self, message: Message): + autoreact = Autoreact.get( + guild=message.guild.id, + channel=message.channel.id, + ) + if autoreact: + for reaction in autoreact.reactions: + await message.add_reaction(reaction) + + async def checks(self, message: Message): + # #tech + channel = find( + lambda x: x.id == 599068193339736096, message.channel_mentions + ) + if channel and message.author.id == 293795462752894976: + await channel.send( + content="https://cdn.discordapp.com/attachments/" + + "664621130044407838/805218508866453554/tech.gif" + ) + content = re.sub(r"\s+", "", message.content) + match = invites.search(content) + if match: + guild_invites = await message.guild.invites() + allowed = [x.code for x in guild_invites] + [ + "dbrand", + "VtgZntXcnZ", + ] + if match.group(1) not in allowed: + await message.delete() + warning = Warning( + active=True, + admin=get_config().client_id, + duration=24, + guild=message.guild.id, + reason="Sent an invite link", + user=message.author.id, + ) + warning.insert() + fields = [ + Field( + "Reason", + "Sent an invite link", + False, + ) + ] + embed = build_embed( + title="Warning", + description=f"{message.author.mention} has been warned", + fields=fields, + ) + embed.set_author( + name=message.author.nick + if message.author.nick + else message.author.name, + icon_url=message.author.avatar_url, + ) + embed.set_footer( + text=f"{message.author.name}#" + + f"{message.author.discriminator} " + + f"| {message.author.id}" + ) + await message.channel.send(embed=embed) + + async def massmention(self, message: Message): + massmention = Setting.get( + guild=message.guild.id, + setting="massmention", + ) + if ( + massmention.value > 0 + and len(message.mentions) + - (1 if message.author in message.mentions else 0) + > massmention.value + ): warning = Warning( active=True, admin=get_config().client_id, duration=24, guild=message.guild.id, - reason="Sent an invite link", + reason="Mass Mention", + user=message.author.id, + ) + warning.insert() + fields = [Field("Reason", "Mass Mention", False)] + embed = build_embed( + title="Warning", + description=f"{message.author.mention} has been warned", + fields=fields, + ) + embed.set_author( + name=message.author.nick + if message.author.nick + else message.author.name, + icon_url=message.author.avatar_url, + ) + embed.set_footer( + text=f"{message.author.name}#{message.author.discriminator} " + + f"| {message.author.id}" + ) + await message.channel.send(embed=embed) + + async def roleping(self, message: Message): + rolepings = Roleping.get_active(guild=message.guild.id) + + if not rolepings: + return + + # Get all role IDs involved with message + roles = [] + for mention in message.role_mentions: + roles.append(mention.id) + for mention in message.mentions: + for role in mention.roles: + roles.append(role.id) + + if not roles: + return + + # Get all roles that are rolepinged + roleping_ids = [r.role for r in rolepings] + + # Get roles in rolepings + role_in_rolepings = list(filter(lambda x: x in roleping_ids, roles)) + + # Check if the user has the role, so they are allowed to ping it + user_missing_role = any( + x.id not in roleping_ids for x in message.author.roles + ) + + # Admins can ping whoever + user_is_admin = message.author.guild_permissions.administrator + + # Check if user in a bypass list + user_has_bypass = False + for roleping in rolepings: + if message.author.id in roleping.bypass["users"]: + user_has_bypass = True + break + if any( + role.id in roleping.bypass["roles"] + for role in message.author.roles + ): + user_has_bypass = True + break + + if ( + role_in_rolepings + and user_missing_role + and not user_is_admin + and not user_has_bypass + ): + warning = Warning( + active=True, + admin=get_config().client_id, + duration=24, + guild=message.guild.id, + reason="Pinged a blocked role/user with a blocked role", user=message.author.id, ) warning.insert() fields = [ Field( "Reason", - "Sent an invite link", + "Pinged a blocked role/user with a blocked role", False, ) ] @@ -81,108 +205,28 @@ async def checks(message): icon_url=message.author.avatar_url, ) embed.set_footer( - text=f"{message.author.name}#" - + f"{message.author.discriminator} " + text=f"{message.author.name}#{message.author.discriminator} " + f"| {message.author.id}" ) await message.channel.send(embed=embed) + async def on_message(self, message: Message): + if ( + not isinstance(message.channel, DMChannel) + and not message.author.bot + ): + await self.autoreact(message) + await self.massmention(message) + await self.roleping(message) + await self.autopurge(message) + await self.checks(message) + await self.bot.process_commands(message) -async def massmention(message): - massmention = Setting.get( - guild=message.guild.id, - setting="massmention", - ) - if ( - massmention.value > 0 - and len(message.mentions) - - (1 if message.author in message.mentions else 0) - > massmention.value - ): - warning = Warning( - active=True, - admin=get_config().client_id, - duration=24, - guild=message.guild.id, - reason="Mass Mention", - user=message.author.id, - ) - warning.insert() - fields = [Field("Reason", "Mass Mention", False)] - embed = build_embed( - title="Warning", - description=f"{message.author.mention} has been warned", - fields=fields, - ) - embed.set_author( - name=message.author.nick - if message.author.nick - else message.author.name, - icon_url=message.author.avatar_url, - ) - embed.set_footer( - text=f"{message.author.name}#{message.author.discriminator} " - + f"| {message.author.id}" - ) - await message.channel.send(embed=embed) - - -async def roleping(message): - roleping = Setting.get(guild=message.guild.id, setting="roleping") - roles = [] - for mention in message.role_mentions: - roles.append(mention.id) - for mention in message.mentions: - for role in mention.roles: - roles.append(role.id) - if ( - roleping - and any(x in roleping.value for x in roles) - and not any(x.id in roleping.value for x in message.author.roles) - ): - warning = Warning( - active=True, - admin=get_config().client_id, - duration=24, - guild=message.guild.id, - reason="Pinged a blocked role/user with a blocked role", - user=message.author.id, - ) - warning.insert() - fields = [ - Field( - "Reason", - "Pinged a blocked role/user with a blocked role", - False, - ) - ] - embed = build_embed( - title="Warning", - description=f"{message.author.mention} has been warned", - fields=fields, - ) - embed.set_author( - name=message.author.nick - if message.author.nick - else message.author.name, - icon_url=message.author.avatar_url, - ) - embed.set_footer( - text=f"{message.author.name}#{message.author.discriminator} " - + f"| {message.author.id}" - ) - await message.channel.send(embed=embed) - - -@jarvis.event -async def on_message(message: Message): - if ( - not isinstance(message.channel, DMChannel) - and message.author.id != jarvis.user.id - ): - await autoreact(message) - await massmention(message) - await roleping(message) - await autopurge(message) - await checks(message) - await jarvis.process_commands(message) + async def on_message_edit(self, message: Message): + if ( + not isinstance(message.channel, DMChannel) + and not message.author.bot + ): + await self.massmention(message) + await self.roleping(message) + await self.checks(message)