diff --git a/.gitignore b/.gitignore index 5e8208f..0900b52 100644 --- a/.gitignore +++ b/.gitignore @@ -134,3 +134,4 @@ dmypy.json .envrc /test.json /testwl +wlgrps/ \ No newline at end of file diff --git a/autowl/Cogs/Group.py b/autowl/Cogs/Group.py index 560e92a..09420b9 100644 --- a/autowl/Cogs/Group.py +++ b/autowl/Cogs/Group.py @@ -20,7 +20,7 @@ class Group(commands.Cog, name="group"): interaction: discord.Interaction, role: discord.Role, ): - if self.client.whitelist.get(role.name): + if self.client.whitelistGrps.get(role.name): await interaction.response.send_message( f"**{role.name}** is already added, cannot add it again!" ) @@ -30,8 +30,29 @@ class Group(commands.Cog, name="group"): await interaction.response.send_message( f"Adding **{role.name}** as a Whitelist role" ) - self.client.whitelist[f"{role.name}"] = config.WhitelistGroup( - discord_role_id=role.id, members={} + self.client.whitelistGrps[role.id] = config.WhitelistGroup( + name=role.name, roleID=role.id, permissions='reserve' + ) + + @app_commands.command() + async def addperm( + self, + interaction: discord.Interaction, + role: discord.Role, + perms: str + ): + if self.client.whitelistGrps.get(role.name): + await interaction.response.send_message( + f"**{role.name}** is already added, cannot add it again!" + ) + return + + log.info(f"Adding {role.name} ({role.id}) as a Whitelist role") + await interaction.response.send_message( + f"Adding **{role.name}** as a Whitelist role" + ) + self.client.whitelistGrps[role.id] = config.WhitelistGroup( + name=role.name, roleID=role.id, permissions=perms ) @app_commands.command() @@ -40,7 +61,7 @@ class Group(commands.Cog, name="group"): interaction: discord.Interaction, role: discord.Role, ): - if not self.client.whitelist.get(role.name): + if not self.client.whitelistGrps.get(role.id): await interaction.response.send_message( f"**{role.name}** has not been added as a whitelisted group, unable to remove!" ) @@ -50,7 +71,8 @@ class Group(commands.Cog, name="group"): await interaction.response.send_message( f"Removed **{role.name}** from Whitelisted roles" ) - self.client.whitelist.pop(role.name) + self.client.whitelistGrps[role.id].delGroup() + self.client.whitelistGrps.pop(role.id) @app_commands.command() async def list_whitelisted_roles(self, interaction: discord.Interaction): @@ -61,8 +83,8 @@ class Group(commands.Cog, name="group"): ) return - for group in self.client.whitelist: - role_id = self.client.whitelist[group].discord_role_id + for group in self.client.whitelistGrps: + role_id = self.client.whitelistGrps[group].discord_role_id if not interaction.guild.get_role(role_id): continue whitelisted_roles.append(f"<@&{role_id}>") diff --git a/autowl/Cogs/Whitelist.py b/autowl/Cogs/Whitelist.py index 4a54f8d..cf39014 100644 --- a/autowl/Cogs/Whitelist.py +++ b/autowl/Cogs/Whitelist.py @@ -1,4 +1,3 @@ -import jsonpickle import discord import logging from autowl import config @@ -22,7 +21,7 @@ class Whitelist(commands.Cog): ) return - if not len(self.client.whitelist.keys()): + if not len(self.client.whitelistGrps.keys()): await interaction.response.send_message( "There are no Whitelist roles defined, unable to continue!" ) @@ -30,26 +29,13 @@ class Whitelist(commands.Cog): steam64_updated = False for role in interaction.user.roles: - for group in self.client.whitelist: - if role.id == self.client.whitelist[group].discord_role_id: + for group in self.client.whitelistGrps: + if role.id == self.client.whitelistGrps[group].discord_role_id: steam64_updated = True - self.client.whitelist[group].members[ - str(interaction.user.id) - ] = config.WhitelistMember(interaction.user.name, steam64) + memb = config.WhitelistMember(interaction.user.id, interaction.user.name, steam64) + self.client.whitelistGrps[group].addMember(memb) if steam64_updated: - outFile = open("test.json", "w") - outFile.write(jsonpickle.encode(self.client.whitelist)) - outFile.close() - wlFile = open("testwl", "w") - for wlgrp in self.client.whitelist: - wlFile.write(f"Group={wlgrp}:reserve\n") - for wlgrp in self.client.whitelist: - wlFile.write("\n") - wlFile.write(f"//{wlgrp}\n") - for wlmem in self.client.whitelist[wlgrp].members: - wlFile.write(f"Admin={self.client.whitelist[wlgrp].members[wlmem].steam64}:{wlgrp} //{self.client.whitelist[wlgrp].members[wlmem].discord_username} ({wlmem})\n") - wlFile.close() log.info( f"Updated {interaction.user.name}'s ({interaction.user.id}) whitelist steam64 to {steam64}" ) diff --git a/autowl/bot.py b/autowl/bot.py index 8034174..7088602 100644 --- a/autowl/bot.py +++ b/autowl/bot.py @@ -1,3 +1,5 @@ +import os.path + import jsonpickle import logging import discord @@ -8,15 +10,17 @@ log = logging.getLogger(__name__) class Bot(commands.Bot): - whitelist = config.Whitelist({}).whitelist + whitelistGrps = {} - try: - infile = open("test.json", "r") - if infile: - whitelist = jsonpickle.decode(infile.read()) - infile.close() - except: - pass + if not os.path.exists('./wlgrps'): + os.makedirs('./wlgrps') + else: + for wlfile in os.listdir('./wlgrps'): + filename = os.fsdecode(wlfile) + if filename.endswith('.json'): + file = open(f'./wlgrps/{filename}', 'r') + wlgrp: config.WhitelistGroup = jsonpickle.decode(file.read()) + whitelistGrps[wlgrp.discord_role_id] = wlgrp def __init__(self, config: config.DiscordClientConfig): self.config = config @@ -46,6 +50,19 @@ class Bot(commands.Bot): await self.tree.sync() + async def on_member_update(self, before: discord.Member, after: discord.Member): + log.info(f"Updating {after.name} ({after.id})") + rmroles = [] + for befrole in before.roles: + rmroles.append(befrole.id) + for aftrole in after.roles: + for befrole in before.roles: + if befrole.id == aftrole.id: + rmroles.remove(aftrole.id) + log.info(f"roles found to remove from {after.name}: {rmroles}") + for rmroleid in rmroles: + self.whitelistGrps[rmroleid].delMember(before.id) + async def setup_hook(self): log.info("Setting up bot") from autowl import Cogs diff --git a/autowl/config.py b/autowl/config.py index 552d55d..09169cb 100644 --- a/autowl/config.py +++ b/autowl/config.py @@ -1,5 +1,9 @@ +import os + +import jsonpickle from dataclasses import dataclass + @dataclass class DiscordClientConfig: login_token: str @@ -7,20 +11,50 @@ class DiscordClientConfig: @dataclass class WhitelistMember: + discord_id: int discord_username: str steam64: int @dataclass class WhitelistGroup: + name: str discord_role_id: int - members: dict[int, WhitelistMember] + squadPerms: str + members: dict[str, WhitelistMember] + def __init__(self, name, roleID, permissions='reserve'): + self.name = name + self.discord_role_id = roleID + self.squadPerms = permissions + self.members = {} -@dataclass -class Whitelist: - whitelist: dict[str, WhitelistGroup] + def delGroup(self): + os.remove(f"./wlgrps/{self.name}") + os.remove(f"./wlgrps/{self.name}.json") - def __iter__(self): - for key in self.whitelist: - yield self.whitelist[key] + def updateFile(self): + outFile = open(f"./wlgrps/{self.name}.json", "w") + outFile.write(jsonpickle.encode(self)) + outFile.close() + + def updateWL(self): + wlFile = open(f"./wlgrps/{self.name}", "w") + wlFile.write(f"Group={self.name}:{self.squadPerms}\n") + wlFile.write("\n") + wlFile.write(f"//{self.name}\n") + for wlmem in self.members: + memb = self.members[wlmem] + wlFile.write(f"Admin={memb.steam64}:{self.name} //{memb.discord_username} ({memb.discord_id})\n") + wlFile.close() + + def addMember(self, member): + self.members[f"{member.discord_id}"] = member + self.updateFile() + self.updateWL() + pass + + def delMember(self, discordID): + self.members.pop(f'{discordID}') + self.updateFile() + self.updateWL()