Fix events, also close #59

This commit is contained in:
Zeva Rose 2021-07-26 19:10:31 -06:00
parent 9a3c86815d
commit feda3036ab
3 changed files with 226 additions and 172 deletions

View file

@ -2,12 +2,15 @@ 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):
class GuildEventHandler(object):
def __init__(self, bot):
self.bot = bot
self.bot.add_listener(self.on_guild_join)
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(
@ -17,7 +20,9 @@ async def on_guild_join(guild):
+ "24 hours a day, seven days a week."
)
await asyncio.sleep(1)
await general.send("Importing all preferences from home interface...")
await general.send(
"Importing all preferences from home interface..."
)
# Set some default settings
setting = Setting(guild=guild.id, setting="massmention", value=5)

View file

@ -1,11 +1,14 @@
from discord import Member
from jarvis import jarvis
from jarvis.db.types import Mute, Setting
@jarvis.event
async def on_member_join(user: Member):
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:
@ -17,4 +20,6 @@ async def on_member_join(user: Member):
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")
await user.add_roles(
role, reason="User just joined and is unverified"
)

View file

@ -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,15 +14,19 @@ invites = re.compile(
)
async def autopurge(message):
class MessageEventHandler(object):
def __init__(self, bot):
self.bot = bot
self.bot.add_listener(self.on_message)
async def autopurge(self, message: Message):
autopurge = Autopurge.get(
guild=message.guild.id, channel=message.channel.id
)
if autopurge:
await message.delete(delay=autopurge.delay)
async def autoreact(message):
async def autoreact(self, message: Message):
autoreact = Autoreact.get(
guild=message.guild.id,
channel=message.channel.id,
@ -32,8 +35,7 @@ async def autoreact(message):
for reaction in autoreact.reactions:
await message.add_reaction(reaction)
async def checks(message):
async def checks(self, message: Message):
# #tech
channel = find(
lambda x: x.id == 599068193339736096, message.channel_mentions
@ -87,8 +89,7 @@ async def checks(message):
)
await message.channel.send(embed=embed)
async def massmention(message):
async def massmention(self, message: Message):
massmention = Setting.get(
guild=message.guild.id,
setting="massmention",
@ -126,19 +127,55 @@ async def massmention(message):
)
await message.channel.send(embed=embed)
async def roleping(self, message: Message):
rolepings = Roleping.get_active(guild=message.guild.id)
async def roleping(message):
roleping = Setting.get(guild=message.guild.id, setting="roleping")
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 (
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)
role_in_rolepings
and user_missing_role
and not user_is_admin
and not user_has_bypass
):
warning = Warning(
active=True,
@ -173,16 +210,23 @@ async def roleping(message):
)
await message.channel.send(embed=embed)
@jarvis.event
async def on_message(message: Message):
async def on_message(self, message: Message):
if (
not isinstance(message.channel, DMChannel)
and message.author.id != jarvis.user.id
and not message.author.bot
):
await autoreact(message)
await massmention(message)
await roleping(message)
await autopurge(message)
await checks(message)
await jarvis.process_commands(message)
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 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)