2023-08-01 20:46:41 -05:00
|
|
|
import os.path
|
|
|
|
|
2023-02-05 04:32:26 -06:00
|
|
|
import discord
|
2023-02-22 22:34:03 -06:00
|
|
|
import random
|
2023-02-22 22:12:41 -06:00
|
|
|
import logging
|
2023-04-26 14:11:54 -05:00
|
|
|
|
|
|
|
import mysql.connector
|
|
|
|
|
2023-02-05 04:32:26 -06:00
|
|
|
from autowl import config
|
|
|
|
from autowl.bot import Bot
|
|
|
|
from discord.ext import commands
|
|
|
|
from discord import app_commands
|
|
|
|
|
2023-02-22 22:12:41 -06:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2023-02-05 04:32:26 -06:00
|
|
|
class Group(commands.Cog, name="group"):
|
|
|
|
def __init__(self, client: Bot):
|
|
|
|
self.client = client
|
|
|
|
|
2024-10-01 18:27:04 -05:00
|
|
|
async def updateRole(self, role: discord.Role):
|
2023-08-01 21:17:23 -05:00
|
|
|
self.client.squadjs.connect()
|
2023-04-17 20:46:53 -05:00
|
|
|
membsup = []
|
|
|
|
for memb in role.members:
|
|
|
|
membsup.append(memb.id)
|
2023-04-17 23:07:20 -05:00
|
|
|
if len(membsup) > 0:
|
2023-08-01 21:17:23 -05:00
|
|
|
memupcur = self.client.squadjs.cursor(buffered=True)
|
2024-10-01 18:27:04 -05:00
|
|
|
in_params = ",".join(["%s"] * len(membsup))
|
|
|
|
sqlstate = (
|
|
|
|
"SELECT * FROM DBLog_SteamUsers WHERE discordID IN (%s)" % in_params
|
|
|
|
)
|
2023-04-17 23:07:20 -05:00
|
|
|
log.info(sqlstate)
|
|
|
|
memupcur.execute(sqlstate, membsup)
|
|
|
|
|
|
|
|
udata = memupcur.fetchall()
|
|
|
|
for data in udata:
|
2024-10-01 18:27:04 -05:00
|
|
|
self.client.whitelistGrps[role.id].addMember(
|
|
|
|
config.WhitelistMember(data[2], data[1], data[0])
|
|
|
|
)
|
2023-04-17 23:07:20 -05:00
|
|
|
|
2023-08-01 21:17:23 -05:00
|
|
|
self.client.squadjs.commit()
|
|
|
|
self.client.squadjs.close()
|
2023-04-17 20:46:53 -05:00
|
|
|
|
2024-10-01 18:27:04 -05:00
|
|
|
async def baseperm(
|
|
|
|
self, interaction: discord.Interaction, role: discord.Role, perms: str
|
|
|
|
):
|
2023-08-02 22:07:44 -05:00
|
|
|
|
2024-10-01 18:27:04 -05:00
|
|
|
await interaction.response.send_message(
|
|
|
|
"Whitelist group successfully added/updated"
|
|
|
|
)
|
2023-08-02 22:07:44 -05:00
|
|
|
if role.id in self.client.whitelistGrps.keys():
|
|
|
|
if perms is not None:
|
|
|
|
self.client.whitelistGrps[role.id].squadPerms = perms
|
|
|
|
self.client.whitelistGrps[role.id].updateGroup()
|
|
|
|
else:
|
|
|
|
log.info(f"Adding {role.name} ({role.id}) as a Whitelist role")
|
|
|
|
self.client.whitelistGrps[role.id] = config.WhitelistGroup(
|
|
|
|
name=role.name, roleID=role.id, permissions=perms
|
|
|
|
)
|
|
|
|
|
|
|
|
await self.updateRole(role)
|
|
|
|
|
|
|
|
@app_commands.command()
|
2024-10-01 18:27:04 -05:00
|
|
|
async def update(self, interaction: discord.Interaction, role: discord.Role):
|
2023-08-02 22:07:44 -05:00
|
|
|
if role.id in self.client.whitelistGrps.keys():
|
|
|
|
await interaction.response.send_message("updating role!")
|
|
|
|
await self.updateRole(role)
|
|
|
|
else:
|
|
|
|
await interaction.response.send_message("role not found!")
|
|
|
|
|
2023-04-17 20:46:53 -05:00
|
|
|
@app_commands.command()
|
|
|
|
async def add(
|
2024-10-01 18:27:04 -05:00
|
|
|
self,
|
|
|
|
interaction: discord.Interaction,
|
|
|
|
role: discord.Role,
|
2023-04-17 20:46:53 -05:00
|
|
|
):
|
|
|
|
await self.baseperm(interaction, role, "reserve")
|
2023-03-04 18:32:59 -06:00
|
|
|
|
|
|
|
@app_commands.command()
|
|
|
|
async def addperm(
|
2024-10-01 18:27:04 -05:00
|
|
|
self, interaction: discord.Interaction, role: discord.Role, perms: str
|
2023-03-04 18:32:59 -06:00
|
|
|
):
|
2023-04-17 20:46:53 -05:00
|
|
|
await self.baseperm(interaction, role, perms)
|
2023-02-05 04:32:26 -06:00
|
|
|
|
2023-08-01 20:46:41 -05:00
|
|
|
@app_commands.command()
|
|
|
|
async def addremote(
|
2024-10-01 18:27:04 -05:00
|
|
|
self,
|
|
|
|
interaction: discord.Interaction,
|
|
|
|
shortname: str,
|
|
|
|
remoteurl: str,
|
|
|
|
perms: str = "whitelist",
|
2023-08-01 20:46:41 -05:00
|
|
|
):
|
|
|
|
if os.path.exists(f"wlgrps/{shortname}.cfg"):
|
|
|
|
await interaction.response.send_message("Already exists!")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
outfile = open(f"./wlgrps/{shortname}.cfg", "w")
|
2023-08-01 21:17:23 -05:00
|
|
|
outfile.write(f"remotelist={remoteurl}\n")
|
2023-08-01 20:46:41 -05:00
|
|
|
outfile.write(f"permissions={perms}\n")
|
|
|
|
outfile.close()
|
|
|
|
await interaction.response.send_message("created!")
|
|
|
|
|
2023-02-22 22:12:41 -06:00
|
|
|
@app_commands.command()
|
|
|
|
async def remove(
|
2024-10-01 18:27:04 -05:00
|
|
|
self,
|
|
|
|
interaction: discord.Interaction,
|
|
|
|
role: discord.Role,
|
2023-02-22 22:12:41 -06:00
|
|
|
):
|
2023-03-04 18:32:59 -06:00
|
|
|
if not self.client.whitelistGrps.get(role.id):
|
2023-02-22 22:12:41 -06:00
|
|
|
await interaction.response.send_message(
|
|
|
|
f"**{role.name}** has not been added as a whitelisted group, unable to remove!"
|
2023-02-05 04:32:26 -06:00
|
|
|
)
|
2023-02-22 22:12:41 -06:00
|
|
|
return
|
2023-02-05 04:32:26 -06:00
|
|
|
|
2023-02-22 22:12:41 -06:00
|
|
|
log.info(f"Removing {role.name} ({role.id}) from Whitelisted role")
|
|
|
|
await interaction.response.send_message(
|
|
|
|
f"Removed **{role.name}** from Whitelisted roles"
|
|
|
|
)
|
2023-03-04 18:32:59 -06:00
|
|
|
self.client.whitelistGrps[role.id].delGroup()
|
|
|
|
self.client.whitelistGrps.pop(role.id)
|
2023-02-22 22:34:03 -06:00
|
|
|
|
|
|
|
@app_commands.command()
|
|
|
|
async def list_whitelisted_roles(self, interaction: discord.Interaction):
|
|
|
|
whitelisted_roles = []
|
|
|
|
if not interaction.guild:
|
|
|
|
await interaction.response.send_message(
|
|
|
|
"This command must be ran within a discord server"
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
2023-03-04 18:32:59 -06:00
|
|
|
for group in self.client.whitelistGrps:
|
|
|
|
role_id = self.client.whitelistGrps[group].discord_role_id
|
2023-02-22 22:34:03 -06:00
|
|
|
if not interaction.guild.get_role(role_id):
|
|
|
|
continue
|
|
|
|
whitelisted_roles.append(f"<@&{role_id}>")
|
|
|
|
|
|
|
|
embed_description = (
|
|
|
|
"\n".join(whitelisted_roles)
|
|
|
|
if whitelisted_roles
|
|
|
|
else "No whitelisted roles found, you can some using `/add`"
|
|
|
|
)
|
|
|
|
|
|
|
|
embed = discord.Embed(
|
|
|
|
title="Whitelisted Roles",
|
|
|
|
description=embed_description,
|
|
|
|
color=random.randint(0, 0xFFFFFF),
|
|
|
|
).set_footer(text="Users with these roles will be whitelisted on Squad")
|
|
|
|
|
|
|
|
await interaction.response.send_message(embed=embed)
|