Fix events, also close #59
This commit is contained in:
parent
9a3c86815d
commit
feda3036ab
3 changed files with 226 additions and 172 deletions
|
@ -2,25 +2,30 @@ import asyncio
|
||||||
|
|
||||||
from discord.utils import find
|
from discord.utils import find
|
||||||
|
|
||||||
from jarvis import jarvis
|
|
||||||
from jarvis.db.types import Setting
|
from jarvis.db.types import Setting
|
||||||
|
|
||||||
|
|
||||||
@jarvis.event
|
class GuildEventHandler(object):
|
||||||
async def on_guild_join(guild):
|
def __init__(self, bot):
|
||||||
general = find(lambda x: x.name == "general", guild.channels)
|
self.bot = bot
|
||||||
if general and general.permissions_for(guild.me).send_messages:
|
self.bot.add_listener(self.on_guild_join)
|
||||||
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...")
|
|
||||||
|
|
||||||
# Set some default settings
|
async def on_guild_join(self, guild):
|
||||||
setting = Setting(guild=guild.id, setting="massmention", value=5)
|
general = find(lambda x: x.name == "general", guild.channels)
|
||||||
setting.insert()
|
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")
|
||||||
|
|
|
@ -1,20 +1,25 @@
|
||||||
from discord import Member
|
from discord import Member
|
||||||
|
|
||||||
from jarvis import jarvis
|
|
||||||
from jarvis.db.types import Mute, Setting
|
from jarvis.db.types import Mute, Setting
|
||||||
|
|
||||||
|
|
||||||
@jarvis.event
|
class MemberEventHandler(object):
|
||||||
async def on_member_join(user: Member):
|
def __init__(self, bot):
|
||||||
guild = user.guild
|
self.bot = bot
|
||||||
mutes = Mute.get_active(guild=guild.id)
|
self.bot.add_listener(self.on_member_join)
|
||||||
if mutes and len(mutes) >= 1:
|
|
||||||
mute_role = Setting.get(guild=guild.id, setting="mute")
|
async def on_member_join(self, user: Member):
|
||||||
role = guild.get_role(mute_role.value)
|
guild = user.guild
|
||||||
await user.add_roles(
|
mutes = Mute.get_active(guild=guild.id)
|
||||||
role, reason="User is muted still muted from prior mute"
|
if mutes and len(mutes) >= 1:
|
||||||
)
|
mute_role = Setting.get(guild=guild.id, setting="mute")
|
||||||
unverified = Setting.get(guild=guild.id, setting="unverified")
|
role = guild.get_role(mute_role.value)
|
||||||
if unverified:
|
await user.add_roles(
|
||||||
role = guild.get_role(unverified.value)
|
role, reason="User is muted still muted from prior mute"
|
||||||
await user.add_roles(role, reason="User just joined and is unverified")
|
)
|
||||||
|
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"
|
||||||
|
)
|
||||||
|
|
|
@ -3,9 +3,8 @@ import re
|
||||||
from discord import DMChannel, Message
|
from discord import DMChannel, Message
|
||||||
from discord.utils import find
|
from discord.utils import find
|
||||||
|
|
||||||
from jarvis import jarvis
|
|
||||||
from jarvis.config import get_config
|
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 import build_embed
|
||||||
from jarvis.utils.field import Field
|
from jarvis.utils.field import Field
|
||||||
|
|
||||||
|
@ -15,57 +14,182 @@ invites = re.compile(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def autopurge(message):
|
class MessageEventHandler(object):
|
||||||
autopurge = Autopurge.get(
|
def __init__(self, bot):
|
||||||
guild=message.guild.id, channel=message.channel.id
|
self.bot = bot
|
||||||
)
|
self.bot.add_listener(self.on_message)
|
||||||
if autopurge:
|
|
||||||
await message.delete(delay=autopurge.delay)
|
|
||||||
|
|
||||||
|
async def autopurge(self, message: Message):
|
||||||
async def autoreact(message):
|
autopurge = Autopurge.get(
|
||||||
autoreact = Autoreact.get(
|
guild=message.guild.id, channel=message.channel.id
|
||||||
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"
|
|
||||||
)
|
)
|
||||||
content = re.sub(r"\s+", "", message.content)
|
if autopurge:
|
||||||
match = invites.search(content)
|
await message.delete(delay=autopurge.delay)
|
||||||
if match:
|
|
||||||
guild_invites = await message.guild.invites()
|
async def autoreact(self, message: Message):
|
||||||
allowed = [x.code for x in guild_invites] + [
|
autoreact = Autoreact.get(
|
||||||
"dbrand",
|
guild=message.guild.id,
|
||||||
"VtgZntXcnZ",
|
channel=message.channel.id,
|
||||||
]
|
)
|
||||||
if match.group(1) not in allowed:
|
if autoreact:
|
||||||
await message.delete()
|
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(
|
warning = Warning(
|
||||||
active=True,
|
active=True,
|
||||||
admin=get_config().client_id,
|
admin=get_config().client_id,
|
||||||
duration=24,
|
duration=24,
|
||||||
guild=message.guild.id,
|
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,
|
user=message.author.id,
|
||||||
)
|
)
|
||||||
warning.insert()
|
warning.insert()
|
||||||
fields = [
|
fields = [
|
||||||
Field(
|
Field(
|
||||||
"Reason",
|
"Reason",
|
||||||
"Sent an invite link",
|
"Pinged a blocked role/user with a blocked role",
|
||||||
False,
|
False,
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
@ -81,108 +205,28 @@ async def checks(message):
|
||||||
icon_url=message.author.avatar_url,
|
icon_url=message.author.avatar_url,
|
||||||
)
|
)
|
||||||
embed.set_footer(
|
embed.set_footer(
|
||||||
text=f"{message.author.name}#"
|
text=f"{message.author.name}#{message.author.discriminator} "
|
||||||
+ f"{message.author.discriminator} "
|
|
||||||
+ f"| {message.author.id}"
|
+ f"| {message.author.id}"
|
||||||
)
|
)
|
||||||
await message.channel.send(embed=embed)
|
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):
|
async def on_message_edit(self, message: Message):
|
||||||
massmention = Setting.get(
|
if (
|
||||||
guild=message.guild.id,
|
not isinstance(message.channel, DMChannel)
|
||||||
setting="massmention",
|
and not message.author.bot
|
||||||
)
|
):
|
||||||
if (
|
await self.massmention(message)
|
||||||
massmention.value > 0
|
await self.roleping(message)
|
||||||
and len(message.mentions)
|
await self.checks(message)
|
||||||
- (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)
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue