Commit e3a6dd48 authored by IS1_4M's avatar IS1_4M
Browse files

finished abstracting restricted and pub chans

parent 0533bb7c
......@@ -121,6 +121,7 @@ class AbstractChanSuite:
Returns club category that matches proposed cli if there is one.
'''
def is_cli(chans, check_channel, CLIUserRoleName):
for chan in chans:
if chan.name.lower() + '-' + CLIUserRoleName.lower() + '-command-line' == check_channel.name.lower():
return chan
......@@ -163,6 +164,7 @@ class AbstractChanSuite:
async def remove_text_channel(chan, channel_name):
#remove text channel from category
print('removing text channel ' + channel_name + ' from ' + chan.name)
channel_name = AbstractChanSuite.format_uinp(channel_name)
for channel in chan.text_channels:
if channel.name.lower() == channel_name:
......
......@@ -40,6 +40,7 @@ with open('../../../ECHackersBotCreds/credentials.json') as file:
student_role_name = 'ECStudent'
faculty_role_name = 'ECFaculty'
general_user_role_names = [student_role_name, faculty_role_name]
account_password = credentials['email_password']
clubs = []
users = []
......@@ -129,7 +130,7 @@ async def on_message(message):
return
# and make the club if not a duplicate
newClub = PublicChan(message.guild, message.author, clubName)
await newClub._init()
await newClub._init(general_user_role_names)
return
'''
Restricted Channel commands go here
......
......@@ -26,7 +26,6 @@ class Club(AbstractChannel): # Creates a club category channel with CLUB MEMBER
'speak': False}
convenor_color = discord.Colour.blue()
member_color = discord.Color.greyple()
Member_abstract_permissions = None
channel_type = 'c'
convener_role_name = 'Convener'
member_role_name = 'Member'
......@@ -50,8 +49,8 @@ class Club(AbstractChannel): # Creates a club category channel with CLUB MEMBER
Club.Convenor_cli_permissions)
async def _init(self):
await self._guild.create_role(name=self._channelName + " Member")
await AbstractChannel._init(self, [self._channelName + " Member"],
await self._guild.create_role(name=self._channelName + " " + Club.member_role_name)
await AbstractChannel._init(self, [self._channelName + " " + Club.member_role_name],
suite=ClubSuite)
class ClubSuite(AbstractChanSuite):
def get_all_clubs(guild): # returns list of club categories
......@@ -78,12 +77,6 @@ class ClubSuite(AbstractChanSuite):
def get_cli(category):
return AbstractChanSuite.get_cli(category, Club.convener_role_name)
'''
Create a new role for the club.
Club - a guild category
role_name: a string of the new role name. Ex: Organizer
permissions: Permissions item to give the role
'''
#await club.guild.create_role(club.name + ' ' + role_name, permissions) # Unstable.
......
......@@ -7,14 +7,28 @@ Created on Sat Sep 5 14:06:37 2020
import discord
from AbstractChannel import AbstractChanSuite, AbstractChannel
# Public Channels are areas where people can make text and voice channels
# Whenever they want to
class PublicChan: # Creates a club category channel with CLUB MEMBER role read and write permissions for club members and CLI access for club convenors
Convenor_abstract_permissions = None
Convenor_cli_permissions = {'read_messages':True,
'send_message':True}
Member_permissions = {'read_messages':True,
'send_messages':True,
'connect':True,
'speak':True}
default_guild_role_perms = {'read_messages':True,
'send_messages':True,
'connect':True,
'speak': True}
convenor_color = discord.Colour.red()
member_color = discord.Color.greyple()
Member_abstract_permissions = None
channel_type = 'o'
convener_role_name = 'Moderator'
member_role_name = 'none'
convenor_color = discord.Colour.red()
def __init__(self, guild, moderator, channelName):
# Make a cli where commands from club conveners here can shape club below.
# Have list of all voice and text channels owned by the club.
......@@ -24,116 +38,51 @@ class PublicChan: # Creates a club category channel with CLUB MEMBER role read a
#Auto give at least one person convener access to the club.
# Needs functionality to give other ECStudents convener roles.
self._guild = guild
self._moderator = moderator
self._channelName = 'o-' + channelName
AbstractChannel.__init__(self, guild, moderator, channelName, PublicChan.channel_type,
PublicChan.convener_role_name,
PublicChan.member_role_name,
PublicChan.convenor_color,
PublicChan.member_color,
PublicChan.default_guild_role_perms,
PublicChan.Member_permissions,
PublicChan.Convenor_cli_permissions)
async def _init(self):
async def _init(self, general_user_roles):
'''
CREATE & ASSIGN ROLES. (Expandable to include mods, operators, etc.)
'''
# Create convenor role
self._convener_role = await self._guild.create_role(name=self._channelName + " Moderator", colour=PublicChan.convenor_color)
# assign convener role
await self._moderator.add_roles(self._convener_role)
'''
CREATE HOLDER FOR CLUB ASSOCIATED STUDENT ROLES
'''
self._club_roles = [self._convener_role] + [role for role in self._guild.roles if role.name in ['ECStudent', 'ECFaculty']]
'''
CREATE CATEGORY CHANNEL W/ CLUB ASSOCIATED STUDENT ROLES PARTICIPATION PRIVILEDGES
'''
# Create channel
self._category = await self._guild.create_category(name=self._channelName)
for role in self._guild.roles:
await self._category.set_permissions(role, read_messages=False, send_messages=False)
for role in self._club_roles:
await self._category.set_permissions(role, read_messages=True, send_messages=True, connect=True, speak=True)
'''
CREATE CATEGORY DEFAULT TEXT CHANNELS
'''
# Create CLI text channel
self._cli = await self._category.create_text_channel(self._channelName + "-moderator-command-line")
# And make it private to all but conveners
for role in self._guild.roles:
await self._cli.set_permissions(role, read_messages=False, send_messages=False)
await self._cli.edit(topic = 'Only Moderators can use this channel')
await self._cli.set_permissions(self._convener_role, read_messages=True, send_messages=True)
await self._cli.send(PubChanSuite.get_help_message(self._cli))
await AbstractChannel._init(self, general_user_roles,
suite=PubChanSuite)
# Only Conveners of this club can post in this channel
# textchannel.set_permissions(rolename, read_messages=True, write_messages=True)
# self._cli_channel_id = None # Cli channel for the club
# Conveners and members of this club can post in this channel. (Use textchannel.set_permissions)
def __repr__(self):
return self._cli_channel_id
class PubChanSuite:
class PubChanSuite(AbstractChanSuite):
def get_all_pubchans(guild): # returns list of club categories
roleNames= []
for role in guild.roles:
roleNames.append(role.name)
return filter(lambda x: x.name + ' Moderator' in roleNames, guild.channels)
return AbstractChanSuite.get_all_chans(guild, PublicChan.channel_type)
'''
Resolve club category from club category name
'''
def get_pubchan(clubs, name):
for club in clubs:
if club.name.lower() == name.lower():
return club
return None
def get_pubchan(chans, name):
return AbstractChanSuite.get_chan(chans, name)
def get_pubchan_roles(club):
roles = []
for role in club.guild.roles:
if role.name.startswith(club.name):
roles.append(role)
return roles
'''
Resolve club role from club and full role name
'''
def get_role(club, roleName):
for role in club.guild.roles:
print(role.name, roleName)
if role.name == roleName:
return role
return None
return AbstractChanSuite.get_chan_roles(club)
'''
Returns club category that matches proposed cli if there is one.
'''
def is_cli(clubs, channel):
for club in clubs:
if club.name.lower() + "-moderator-command-line" == channel.name.lower():
return club
return False
def is_cli(categories, channel):
return AbstractChanSuite.is_cli(categories, channel, PublicChan.convener_role_name)
'''
Create a new role for the club.
Club - a guild category
role_name: a string of the new role name. Ex: Organizer
permissions: Permissions item to give the role
'''
async def add_role(club, role_name, permissions):
raise NotImplementedError('Not Applicable Here')
#await club.guild.create_role(club.name + ' ' + role_name, permissions) # Unstable.
async def add_role_to_user(club, member, role):
await member.add_roles(PubChanSuite.get_role(club, role))
def get_cli(category):
return AbstractChanSuite.get_cli(category, PublicChan.convener_role_name)
# Add new convener to the club
async def add_moderator(club, member, moderator_role_name=None):
if moderator_role_name == None:
moderator_role_name = club.name + ' Moderator'
await member.add_roles(PubChanSuite.get_role(club, moderator_role_name))
async def add_moderator(club, member):
await AbstractChanSuite.add_CLIUser(club, member, PublicChan.convener_role_name)
async def remove_as_moderator(chan, member):
await member.remove_roles(PubChanSuite.get_role(chan, chan.name + ' ' + PublicChan.convener_role_name))
async def ban_member(self, member):
# permaban member from the pubchannel
# do not allow join if on a list, i guess
......@@ -143,63 +92,7 @@ class PubChanSuite:
# remove from the list, i guess
pass
'''
add text channel to resolved club category channel
'''
async def add_text_channel(club, channelName):
# add a text channel to this category.
await club.create_text_channel(channelName)
async def remove_text_channel(club, channel_name):
channel_name = channel_name.lower()
#remove text channel from category
for channel in club.text_channels:
if channel.name.lower() == channel_name:
await channel.delete()
return True
return False
'''
add text channel to resolved club category channel
'''
async def add_voice_channel(club, channelName):
#add voice channel to category
await club.create_voice_channel(channelName)
async def remove_voice_channel(club, channel_name):
#remove voice channel from this category
channel_name = channel_name.lower()
for channel in club.voice_channels:
if channel.name == channel_name:
await channel.delete()
return True
return False
async def delete_category(club): # Provide your category
# Ask "are you sure" in the discord cli. if yes, delete the category.
# if no, don't. Return true or false if category was successfully/unsuccessfully deleted, respectively.
# Remove the clubnameX roles from the discord.
'''
TAKE LOG OF ALL PRIOR ACTIVITY FROM ALL EXISTING TEXT CHANNELS
'''
#TODO
'''
DELETE ASSOCIATED CHANNELS & CATEGORIES
'''
for channel in club.text_channels + club.voice_channels:
await channel.delete()
'''
DELETE ASSOCIATED CLUB ROLES
'''
for role in PubChanSuite.get_pubchan_roles(club):
await role.delete()
await club.delete()
def get_pubchan_commands():
return ['!addrole - notimplemented','!addmember [member]- adds a member','!kick [member] - removes a member','!addvoicechannel [name] - adds a voice channel','!addtextchannel [name] - adds a text channel','!addconvener [member] - makes [member] a convener', '!ban [member] - unimplemented', '!unban [member] - unimplemented', '!closeclub - closes your club',
'!removevoicechannel [name] - remove voice channel', '!removetextchannel [name] - remove text channel']
......@@ -212,6 +105,7 @@ class PubChanSuite:
for command in commands:
send_text += command + '\n\t'
return send_text
async def process_commands(message):
content = message.content.lower()
if len(content.split(' ')) < 3:
......
......@@ -10,15 +10,29 @@ Created on Sat Sep 5 14:34:41 2020
# and voice channels
import discord
from AbstractChannel import AbstractChanSuite, AbstractChannel
# Public Channels are areas where people can make text and voice channels
# Whenever they want to
class RestrictChan: # Creates a restricted category channel with CLUB MEMBER role read and write permissions for club members and CLI access for club convenors
# Is the same as a public channel, except that
Convenor_abstract_permissions = None
Convenor_cli_permissions = {'read_messages':True,
'send_message':True}
Member_permissions = {'read_messages':True,
'send_messages':True,
'connect':True,
'speak':True}
default_guild_role_perms = {'read_messages':True,
'send_messages':True,
'connect':True,
'speak': True}
convenor_color = discord.Colour.red()
member_color = discord.Color.greyple()
Member_abstract_permissions = None
channel_type = 'r'
convener_role_name = 'Moderator_'
member_role_name = 'none'
convenor_color = discord.Colour.red()
def __init__(self, guild, moderator, channelName):
# Make a cli where commands from club conveners here can shape club below.
# Have list of all voice and text channels owned by the club.
......@@ -28,123 +42,47 @@ class RestrictChan: # Creates a restricted category channel with CLUB MEMBER rol
#Auto give at least one person convener access to the club.
# Needs functionality to give other ECStudents convener roles.
self._guild = guild
self._moderator = moderator
self._channelName = 'r-' + channelName
AbstractChannel.__init__(self, guild, moderator, channelName,
RestrictChan.channel_type,
RestrictChan.convener_role_name,
RestrictChan.member_role_name,
RestrictChan.convenor_color,
RestrictChan.member_color,
RestrictChan.default_guild_role_perms,
RestrictChan.Member_permissions,
RestrictChan.Convenor_cli_permissions)
async def _init(self):
'''
CREATE & ASSIGN ROLES. (Expandable to include mods, operators, etc.)
'''
# Create convenor role
self._convener_role = await self._guild.create_role(name=self._channelName + " Moderator_", colour=RestrictChan.convenor_color)
# assign convener role
await self._moderator.add_roles(self._convener_role)
'''
CREATE HOLDER FOR CLUB ASSOCIATED STUDENT ROLES
'''
self._club_roles = [self._convener_role] + [role for role in self._guild.roles if role.name in ['ECStudent', 'ECFaculty']]
'''
CREATE CATEGORY CHANNEL W/ CLUB ASSOCIATED STUDENT ROLES PARTICIPATION PRIVILEDGES
'''
# Create channel
self._category = await self._guild.create_category(name=self._channelName)
for role in self._guild.roles:
await self._category.set_permissions(role, read_messages=False, send_messages=False)
for role in self._club_roles:
await self._category.set_permissions(role, read_messages=True, send_messages=True, connect=True, speak=True)
async def _init(self, general_user_roles):
await AbstractChannel._init(self, general_user_roles,
suite=RestrictChanSuite)
'''
CREATE CATEGORY DEFAULT TEXT CHANNELS
'''
# Create CLI text channel
self._cli = await self._category.create_text_channel(self._channelName + "-moderator_-command-line")
# And make it private to all but conveners
for role in self._guild.roles:
await self._cli.set_permissions(role, read_messages=False, send_messages=False)
await self._cli.edit(topic = 'Only Moderators can use this channel')
await self._cli.set_permissions(self._convener_role, read_messages=True, send_messages=True)
await self._cli.send(RestrictChanSuite.get_help_message(self._cli))
# Only Conveners of this club can post in this channel
# textchannel.set_permissions(rolename, read_messages=True, write_messages=True)
# self._cli_channel_id = None # Cli channel for the club
# Conveners and members of this club can post in this channel. (Use textchannel.set_permissions)
def __repr__(self):
return self._cli_channel_id
class RestrictChanSuite:
class RestrictChanSuite(AbstractChanSuite):
def get_all_restrictchans(guild): # returns list of club categories
roleNames= []
for role in guild.roles:
roleNames.append(role.name)
return filter(lambda x: x.name + ' Moderator_' in roleNames, guild.channels)
return AbstractChanSuite.get_all_chans(guild, RestrictChan.channel_type)
'''
Resolve club category from club category name
'''
def get_restrictchan(clubs, name):
for club in clubs:
print(club.name.lower(), name.lower())
if club.name.lower() == name.lower():
return club
return None
def get_restrictchan_roles(club):
roles = []
for role in club.guild.roles:
if role.name.startswith(club.name):
roles.append(role)
return roles
def get_restrictchan(chans, name):
return AbstractChanSuite.get_chan(chans, name)
'''
Resolve club role from club and full role name
'''
def get_role(club, roleName):
for role in club.guild.roles:
print(role.name, roleName)
if role.name == roleName:
return role
return None
def get_restrictchan_roles(chan):
return AbstractChanSuite.get_chan_roles(chan)
def get_cli(category):
for channel in category.channels:
if category.name.lower() + '-moderator_-command-line' == channel.name.lower():
return channel
return False
return AbstractChanSuite.get_cli(category, RestrictChan.convener_role_name)
'''
Returns club category that matches proposed cli if there is one.
'''
def is_cli(clubs, channel):
for club in clubs:
if club.name.lower() + "-moderator_-command-line" == channel.name.lower():
return club
return False
'''
Create a new role for the club.
Club - a guild category
role_name: a string of the new role name. Ex: Organizer
permissions: Permissions item to give the role
'''
async def add_role(club, role_name, permissions):
raise NotImplementedError('Not Applicable Here')
#await club.guild.create_role(club.name + ' ' + role_name, permissions) # Unstable.
async def add_role_to_user(club, member, role):
await member.add_roles(RestrictChanSuite.get_role(club, role))
def is_cli(categories, channel):
return AbstractChanSuite.is_cli(categories, channel, RestrictChan.convener_role_name)
# Add new convener to the club
async def add_moderator(club, member, moderator_role_name=None):
if moderator_role_name == None:
moderator_role_name = club.name + ' Moderator_'
await member.add_roles(RestrictChanSuite.get_role(club, moderator_role_name))
async def add_moderator(club, member):
await AbstractChanSuite.add_CLIUser(club, member, RestrictChan.convener_role_name)
async def remove_as_moderator(chan, member):
await member.remove_roles(RestrictChanSuite.get_role(chan, chan.name + ' ' + RestrictChan.convener_role_name))
async def ban_member(self, member):
# permaban member from the pubchannel
# do not allow join if on a list, i guess
......@@ -157,60 +95,7 @@ class RestrictChanSuite:
'''
add text channel to resolved club category channel
'''
async def add_text_channel(club, channelName):
# add a text channel to this category.
await club.create_text_channel(channelName)
async def remove_text_channel(club, channel_name):
#remove text channel from category
channel_name = channel_name.lower()
for channel in club.text_channels:
if channel.name.lower() == channel_name:
await channel.delete()
return True
return False
'''
add text channel to resolved club category channel
'''
async def add_voice_channel(club, channelName):
#add voice channel to category
await club.create_voice_channel(channelName)
async def remove_voice_channel(club, channel_name):
#remove voice channel from this category
channel_name = channel_name.lower()
for channel in club.voice_channels:
if channel.name == channel_name:
await channel.delete()
return True
return False
async def delete_category(club): # Provide your category
# Ask "are you sure" in the discord cli. if yes, delete the category.
# if no, don't. Return true or false if category was successfully/unsuccessfully deleted, respectively.
# Remove the clubnameX roles from the discord.
'''
TAKE LOG OF ALL PRIOR ACTIVITY FROM ALL EXISTING TEXT CHANNELS
'''
#TODO
'''
DELETE ASSOCIATED CHANNELS & CATEGORIES
'''
for channel in club.text_channels + club.voice_channels:
await channel.delete()
'''
DELETE ASSOCIATED CLUB ROLES
'''
for role in RestrictChanSuite.get_pubchan_roles(club):
await role.delete()
await club.delete()
def get_restrictchan_commands():
return ['!addrole - notimplemented','!addmember [member]- adds a member','!kick [member] - removes a member','!addvoicechannel [name] - adds a voice channel','!addtextchannel [name] - adds a text channel','!addconvener [member] - makes [member] a convener', '!ban [member] - unimplemented', '!unban [member] - unimplemented', '!closeclub - closes your club',
'!removevoicechannel [name] - remove voice channel', '!removetextchannel [name] - remove text channel']
......@@ -234,7 +119,7 @@ class RestrictChanSuite:
elif content.startswith('!addvoicechannel'):
await RestrictChanSuite.add_voice_channel(maybe_restrict_chan, content[len('!addvoicechannel '):])
elif content.startswith('!removevoicechannel'):
await RestrictChanSuite.add_voice_channel(maybe_restrict_chan, content[len('!removevoicechannel '):])
await RestrictChanSuite.remove_voice_channel(maybe_restrict_chan, content[len('!removevoicechannel '):])
return True
except:
print('could not find restrictchannelcommandline');
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment