This commit is contained in:
Skillet 2023-03-04 19:32:59 -05:00
parent ef315a70e1
commit adee5095c6
5 changed files with 101 additions and 41 deletions

1
.gitignore vendored
View File

@ -134,3 +134,4 @@ dmypy.json
.envrc .envrc
/test.json /test.json
/testwl /testwl
wlgrps/

View File

@ -20,7 +20,7 @@ class Group(commands.Cog, name="group"):
interaction: discord.Interaction, interaction: discord.Interaction,
role: discord.Role, role: discord.Role,
): ):
if self.client.whitelist.get(role.name): if self.client.whitelistGrps.get(role.name):
await interaction.response.send_message( await interaction.response.send_message(
f"**{role.name}** is already added, cannot add it again!" 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( await interaction.response.send_message(
f"Adding **{role.name}** as a Whitelist role" f"Adding **{role.name}** as a Whitelist role"
) )
self.client.whitelist[f"{role.name}"] = config.WhitelistGroup( self.client.whitelistGrps[role.id] = config.WhitelistGroup(
discord_role_id=role.id, members={} 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() @app_commands.command()
@ -40,7 +61,7 @@ class Group(commands.Cog, name="group"):
interaction: discord.Interaction, interaction: discord.Interaction,
role: discord.Role, role: discord.Role,
): ):
if not self.client.whitelist.get(role.name): if not self.client.whitelistGrps.get(role.id):
await interaction.response.send_message( await interaction.response.send_message(
f"**{role.name}** has not been added as a whitelisted group, unable to remove!" 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( await interaction.response.send_message(
f"Removed **{role.name}** from Whitelisted roles" 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() @app_commands.command()
async def list_whitelisted_roles(self, interaction: discord.Interaction): async def list_whitelisted_roles(self, interaction: discord.Interaction):
@ -61,8 +83,8 @@ class Group(commands.Cog, name="group"):
) )
return return
for group in self.client.whitelist: for group in self.client.whitelistGrps:
role_id = self.client.whitelist[group].discord_role_id role_id = self.client.whitelistGrps[group].discord_role_id
if not interaction.guild.get_role(role_id): if not interaction.guild.get_role(role_id):
continue continue
whitelisted_roles.append(f"<@&{role_id}>") whitelisted_roles.append(f"<@&{role_id}>")

View File

@ -1,4 +1,3 @@
import jsonpickle
import discord import discord
import logging import logging
from autowl import config from autowl import config
@ -22,7 +21,7 @@ class Whitelist(commands.Cog):
) )
return return
if not len(self.client.whitelist.keys()): if not len(self.client.whitelistGrps.keys()):
await interaction.response.send_message( await interaction.response.send_message(
"There are no Whitelist roles defined, unable to continue!" "There are no Whitelist roles defined, unable to continue!"
) )
@ -30,26 +29,13 @@ class Whitelist(commands.Cog):
steam64_updated = False steam64_updated = False
for role in interaction.user.roles: for role in interaction.user.roles:
for group in self.client.whitelist: for group in self.client.whitelistGrps:
if role.id == self.client.whitelist[group].discord_role_id: if role.id == self.client.whitelistGrps[group].discord_role_id:
steam64_updated = True steam64_updated = True
self.client.whitelist[group].members[ memb = config.WhitelistMember(interaction.user.id, interaction.user.name, steam64)
str(interaction.user.id) self.client.whitelistGrps[group].addMember(memb)
] = config.WhitelistMember(interaction.user.name, steam64)
if steam64_updated: 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( log.info(
f"Updated {interaction.user.name}'s ({interaction.user.id}) whitelist steam64 to {steam64}" f"Updated {interaction.user.name}'s ({interaction.user.id}) whitelist steam64 to {steam64}"
) )

View File

@ -1,3 +1,5 @@
import os.path
import jsonpickle import jsonpickle
import logging import logging
import discord import discord
@ -8,15 +10,17 @@ log = logging.getLogger(__name__)
class Bot(commands.Bot): class Bot(commands.Bot):
whitelist = config.Whitelist({}).whitelist whitelistGrps = {}
try: if not os.path.exists('./wlgrps'):
infile = open("test.json", "r") os.makedirs('./wlgrps')
if infile: else:
whitelist = jsonpickle.decode(infile.read()) for wlfile in os.listdir('./wlgrps'):
infile.close() filename = os.fsdecode(wlfile)
except: if filename.endswith('.json'):
pass 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): def __init__(self, config: config.DiscordClientConfig):
self.config = config self.config = config
@ -46,6 +50,19 @@ class Bot(commands.Bot):
await self.tree.sync() 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): async def setup_hook(self):
log.info("Setting up bot") log.info("Setting up bot")
from autowl import Cogs from autowl import Cogs

View File

@ -1,5 +1,9 @@
import os
import jsonpickle
from dataclasses import dataclass from dataclasses import dataclass
@dataclass @dataclass
class DiscordClientConfig: class DiscordClientConfig:
login_token: str login_token: str
@ -7,20 +11,50 @@ class DiscordClientConfig:
@dataclass @dataclass
class WhitelistMember: class WhitelistMember:
discord_id: int
discord_username: str discord_username: str
steam64: int steam64: int
@dataclass @dataclass
class WhitelistGroup: class WhitelistGroup:
name: str
discord_role_id: int 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 def delGroup(self):
class Whitelist: os.remove(f"./wlgrps/{self.name}")
whitelist: dict[str, WhitelistGroup] os.remove(f"./wlgrps/{self.name}.json")
def __iter__(self): def updateFile(self):
for key in self.whitelist: outFile = open(f"./wlgrps/{self.name}.json", "w")
yield self.whitelist[key] 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()