geas-bot/app/cogs/MembershipRestriction.py

112 lines
4.5 KiB
Python

import discord
from discord.ext import commands, tasks
from datetime import datetime
from bot import dbClient, p, state, gameTimes, gameTime, timeSlotList, dbFindTimeslot, dbLookupRole
def gameCategories(client):
l = []
try:
for guild in client.guilds:
dbName = str(guild.id)
db = dbClient[dbName]
for colName in db.list_collection_names():
if colName != 'settings':
ret = db[colName].find()
for e in ret:
l.append(guild.get_channel(e['category']))
except:
for guild in client.guilds:
for cat in guild.categories:
if cat.name.split(': ',maxsplit=1)[0] in timeSlotList():
l.append(cat)
return l
def gameRoles(client):
l = []
try:
for guild in client.guilds:
dbName = str(guild.id)
db = dbClient[dbName]
for colName in db.list_collection_names():
if colName != 'settings':
ret = db[colName].find()
for e in ret:
l.append(guild.get_role(e['role']))
except:
for guild in client.guilds:
for role in guild.roles:
if role.name.split(': ',maxsplit=1)[0] in timeSlotList():
l.append(role)
return l
def committeeRoles(client):
l = []
for guild in client.guilds:
role = discord.utils.find(lambda r: r.name == 'Committee', guild.roles)
l.append(role)
return l
def memberRoles(client):
l = []
for guild in client.guilds:
role = discord.utils.find(lambda r: r.name == 'Life Members', guild.roles)
l.append(role)
role = discord.utils.find(lambda r: r.name == 'Members: Full Year', guild.roles)
l.append(role)
role = discord.utils.find(lambda r: r.name == 'Members: Semester 2', guild.roles)
l.append(role)
role = discord.utils.find(lambda r: r.name == 'Members: Semester 1', guild.roles)
l.append(role)
role = discord.utils.find(lambda r: r.name == 'New Member', guild.roles)
l.append(role)
role = discord.utils.find(lambda r: r.name == 'Temporary Access', guild.roles)
l.append(role)
return l
def checkCommitteeRoles(author,committee):
if set(author.roles) & set(committee):
return True
def checkMemberRoles(author,memberRoles):
if set(author.roles) & set(memberRoles):
return True
class MembershipRestriction(commands.Cog, name='Membership Restriction Protocol'):
def __init__(self,client):
self.client = client
# Event Listener for Message from Non-Member in Game Channels
@commands.Cog.listener()
async def on_message(self,message):
if message.author.bot:
return
guestRole = discord.utils.find(lambda g: g.name == 'Guest', message.guild.roles)
if guestRole.permissions.read_messages:
return
if message.channel.category not in gameCategories(self.client):
return
if checkCommitteeRoles(message.author, committeeRoles(self.client)) or message.guild.owner == message.author:
return
if checkMemberRoles(message.author, memberRoles(self.client)):
return
signupChannel = discord.utils.find(lambda c: c.name == '📋-membership-signups', message.guild.channels)
if message.channel.overwrites_for(message.author).manage_channels:
return
await message.channel.send(f'{message.author.mention} does not have a verified membership of Geas. Please submit your membership confirmation for verification in the {signupChannel.mention} to ensure you have access to your game.')
await message.channel.category.set_permissions(message.author, send_messages = False, connect = False)
await message.delete()
# Event Listener for Reinstating Permissions when Membership is Assigned
@commands.Cog.listener()
async def on_member_update(self,before,after):
if before.roles == after.roles:
return
if checkMemberRoles(after, memberRoles(self.client)):
for g in after.roles:
if g in gameRoles(self.client):
cat = dbLookupRole(after.guild, g)
if not cat.overwrites_for(after).send_messages and not cat.overwrites_for(after).manage_channels:
await cat.set_permissions(after, overwrite = None)
def setup(client):
client.add_cog(MembershipRestriction(client))