mirror of
https://github.com/AsgardEternal/DiscordWhitelist.git
synced 2024-12-28 23:39:13 -06:00
use /link command and hook into mysql database
This commit is contained in:
parent
7ca5e699e5
commit
66e5f0b0ca
3
.gitignore
vendored
3
.gitignore
vendored
@ -134,4 +134,5 @@ dmypy.json
|
|||||||
.envrc
|
.envrc
|
||||||
/test.json
|
/test.json
|
||||||
/testwl
|
/testwl
|
||||||
wlgrps/
|
wlgrps/
|
||||||
|
/upload.sh
|
||||||
|
@ -14,34 +14,8 @@ class Group(commands.Cog, name="group"):
|
|||||||
def __init__(self, client: Bot):
|
def __init__(self, client: Bot):
|
||||||
self.client = client
|
self.client = client
|
||||||
|
|
||||||
@app_commands.command()
|
async def baseperm(self, interaction: discord.Interaction, role: discord.Role, perms: str):
|
||||||
async def add(
|
if role.id in self.client.whitelistGrps.keys():
|
||||||
self,
|
|
||||||
interaction: discord.Interaction,
|
|
||||||
role: discord.Role,
|
|
||||||
):
|
|
||||||
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='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(
|
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!"
|
||||||
)
|
)
|
||||||
@ -54,6 +28,35 @@ class Group(commands.Cog, name="group"):
|
|||||||
self.client.whitelistGrps[role.id] = config.WhitelistGroup(
|
self.client.whitelistGrps[role.id] = config.WhitelistGroup(
|
||||||
name=role.name, roleID=role.id, permissions=perms
|
name=role.name, roleID=role.id, permissions=perms
|
||||||
)
|
)
|
||||||
|
membsup = []
|
||||||
|
for memb in role.members:
|
||||||
|
membsup.append(memb.id)
|
||||||
|
memupcur = self.client.squadjs.cursor(buffered=True)
|
||||||
|
in_params = ','.join(['%s'] * len(membsup))
|
||||||
|
sqlstate = "SELECT * FROM DBLog_SteamUsers WHERE discordID IN (%s)" % in_params
|
||||||
|
memupcur.execute(sqlstate, membsup)
|
||||||
|
|
||||||
|
udata = memupcur.fetchall()
|
||||||
|
for data in udata:
|
||||||
|
self.client.whitelistGrps[role.id].addMember(config.WhitelistMember(data[2], "unknown", data[0]))
|
||||||
|
self.client.squadjs.commit()
|
||||||
|
|
||||||
|
@app_commands.command()
|
||||||
|
async def add(
|
||||||
|
self,
|
||||||
|
interaction: discord.Interaction,
|
||||||
|
role: discord.Role,
|
||||||
|
):
|
||||||
|
await self.baseperm(interaction, role, "reserve")
|
||||||
|
|
||||||
|
@app_commands.command()
|
||||||
|
async def addperm(
|
||||||
|
self,
|
||||||
|
interaction: discord.Interaction,
|
||||||
|
role: discord.Role,
|
||||||
|
perms: str
|
||||||
|
):
|
||||||
|
await self.baseperm(interaction, role, perms)
|
||||||
|
|
||||||
@app_commands.command()
|
@app_commands.command()
|
||||||
async def remove(
|
async def remove(
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
import discord
|
import discord
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
import mysql.connector
|
||||||
|
|
||||||
from autowl import config
|
from autowl import config
|
||||||
from autowl.bot import Bot
|
from autowl.bot import Bot
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
@ -14,33 +17,53 @@ class Whitelist(commands.Cog):
|
|||||||
self.client = client
|
self.client = client
|
||||||
|
|
||||||
@app_commands.command()
|
@app_commands.command()
|
||||||
async def register(self, interaction: discord.Interaction, steam64: int):
|
async def link(self, interaction: discord.Interaction, steam64: str):
|
||||||
if not interaction.guild:
|
if not interaction.guild:
|
||||||
await interaction.response.send_message(
|
await interaction.response.send_message(
|
||||||
"This command must be ran within a discord server!"
|
"This command must be ran within a discord server!"
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
updatecur = self.client.squadjs.cursor(buffered=True)
|
||||||
|
try:
|
||||||
|
updatecur.execute(self.client.squadjs_updateDiscordID, (interaction.user.id, steam64))
|
||||||
|
except mysql.connector.Error as err:
|
||||||
|
log.error("MYSQL error!")
|
||||||
|
await interaction.response.send_message("Could not find steamID!")
|
||||||
|
for urole in interaction.user.roles:
|
||||||
|
if urole.id in self.client.whitelistGrps.keys():
|
||||||
|
self.client.whitelistGrps[urole.id].addMember(config.WhitelistMember(interaction.user.id, interaction.user.nick, steam64))
|
||||||
|
self.client.squadjs.commit()
|
||||||
|
interaction.response.send_message("SteamID is linked, roles updated.")
|
||||||
|
|
||||||
if not len(self.client.whitelistGrps.keys()):
|
#
|
||||||
await interaction.response.send_message(
|
# @app_commands.command()
|
||||||
"There are no Whitelist roles defined, unable to continue!"
|
# async def register(self, interaction: discord.Interaction, steam64: str):
|
||||||
)
|
# if not interaction.guild:
|
||||||
return
|
# await interaction.response.send_message(
|
||||||
|
# "This command must be ran within a discord server!"
|
||||||
steam64_updated = False
|
# )
|
||||||
for role in interaction.user.roles:
|
# return
|
||||||
for group in self.client.whitelistGrps:
|
#
|
||||||
if role.id == self.client.whitelistGrps[group].discord_role_id:
|
# if not len(self.client.whitelistGrps.keys()):
|
||||||
steam64_updated = True
|
# await interaction.response.send_message(
|
||||||
memb = config.WhitelistMember(interaction.user.id, interaction.user.name, steam64)
|
# "There are no Whitelist roles defined, unable to continue!"
|
||||||
self.client.whitelistGrps[group].addMember(memb)
|
# )
|
||||||
|
# return
|
||||||
if steam64_updated:
|
#
|
||||||
log.info(
|
# steam64_updated = False
|
||||||
f"Updated {interaction.user.name}'s ({interaction.user.id}) whitelist steam64 to {steam64}"
|
# for role in interaction.user.roles:
|
||||||
)
|
# for group in self.client.whitelistGrps:
|
||||||
await interaction.response.send_message(
|
# if role.id == self.client.whitelistGrps[group].discord_role_id:
|
||||||
f"Updated `{interaction.user.name}`'s whitelist steam64 to `{steam64}`!"
|
# steam64_updated = True
|
||||||
)
|
# memb = config.WhitelistMember(interaction.user.id, interaction.user.name, steam64)
|
||||||
else:
|
# self.client.whitelistGrps[group].addMember(memb)
|
||||||
await interaction.response.send_message(f"Unable to update `{interaction.user.name}`'s whitelist steam64 as they are not in a valid Whitelisted group")
|
#
|
||||||
|
# if steam64_updated:
|
||||||
|
# log.info(
|
||||||
|
# f"Updated {interaction.user.name}'s ({interaction.user.id}) whitelist steam64 to {steam64}"
|
||||||
|
# )
|
||||||
|
# await interaction.response.send_message(
|
||||||
|
# f"Updated `{interaction.user.name}`'s whitelist steam64 to `{steam64}`!"
|
||||||
|
# )
|
||||||
|
# else:
|
||||||
|
# await interaction.response.send_message(f"Unable to update `{interaction.user.name}`'s whitelist steam64 as they are not in a valid Whitelisted group")
|
||||||
|
@ -6,7 +6,6 @@ from autowl.config import DiscordClientConfig
|
|||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class CustomFormat(logging.Formatter):
|
class CustomFormat(logging.Formatter):
|
||||||
grey = "\x1b[38;20m"
|
grey = "\x1b[38;20m"
|
||||||
yellow = "\x1b[33;20m"
|
yellow = "\x1b[33;20m"
|
||||||
@ -51,8 +50,11 @@ def main():
|
|||||||
|
|
||||||
if disToken := environ.get("DISCORD_TOKEN"):
|
if disToken := environ.get("DISCORD_TOKEN"):
|
||||||
bot_config = DiscordClientConfig(disToken)
|
bot_config = DiscordClientConfig(disToken)
|
||||||
|
if not (dbpass := environ.get("DBPASS")):
|
||||||
|
log.error("Unable to access DBPASS in environment!")
|
||||||
|
exit(1)
|
||||||
try:
|
try:
|
||||||
bot.Bot(bot_config).start_bot()
|
bot.Bot(bot_config, dbpass).start_bot()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.critical(f"Bot exited critically, error: {e}")
|
log.critical(f"Bot exited critically, error: {e}")
|
||||||
raise e
|
raise e
|
||||||
|
@ -3,6 +3,7 @@ import os.path
|
|||||||
import jsonpickle
|
import jsonpickle
|
||||||
import logging
|
import logging
|
||||||
import discord
|
import discord
|
||||||
|
import mysql.connector
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from autowl import config
|
from autowl import config
|
||||||
|
|
||||||
@ -10,6 +11,12 @@ log = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class Bot(commands.Bot):
|
class Bot(commands.Bot):
|
||||||
|
squadjs_updateDiscordID = ("UPDATE DBLog_SteamUsers SET discordID = %s "
|
||||||
|
"WHERE steamID = %s")
|
||||||
|
|
||||||
|
squadjs_findByDiscordID = ("SELECT * FROM DBLog_SteamUsers "
|
||||||
|
"WHERE discordID = %s")
|
||||||
|
|
||||||
whitelistGrps = {}
|
whitelistGrps = {}
|
||||||
|
|
||||||
if not os.path.exists('./wlgrps'):
|
if not os.path.exists('./wlgrps'):
|
||||||
@ -22,7 +29,7 @@ class Bot(commands.Bot):
|
|||||||
wlgrp: config.WhitelistGroup = jsonpickle.decode(file.read())
|
wlgrp: config.WhitelistGroup = jsonpickle.decode(file.read())
|
||||||
whitelistGrps[wlgrp.discord_role_id] = wlgrp
|
whitelistGrps[wlgrp.discord_role_id] = wlgrp
|
||||||
|
|
||||||
def __init__(self, config: config.DiscordClientConfig):
|
def __init__(self, config: config.DiscordClientConfig, mysqlpass):
|
||||||
self.config = config
|
self.config = config
|
||||||
intents = discord.Intents.default()
|
intents = discord.Intents.default()
|
||||||
intents.message_content = True
|
intents.message_content = True
|
||||||
@ -32,6 +39,7 @@ class Bot(commands.Bot):
|
|||||||
intents=intents,
|
intents=intents,
|
||||||
help_command=commands.DefaultHelpCommand(dm_help=True),
|
help_command=commands.DefaultHelpCommand(dm_help=True),
|
||||||
)
|
)
|
||||||
|
self.squadjs = mysql.connector.connect(user='squadjs', password=mysqlpass, host='asgard.orion-technologies.io', database='squadjs')
|
||||||
|
|
||||||
async def on_command(self, ctx: commands.Context):
|
async def on_command(self, ctx: commands.Context):
|
||||||
log.info(f"{ctx.author} ({ctx.author.id}) invoked command: {ctx.command.name}, {ctx.message}")
|
log.info(f"{ctx.author} ({ctx.author.id}) invoked command: {ctx.command.name}, {ctx.message}")
|
||||||
@ -51,6 +59,11 @@ class Bot(commands.Bot):
|
|||||||
await self.tree.sync()
|
await self.tree.sync()
|
||||||
|
|
||||||
async def on_member_update(self, before: discord.Member, after: discord.Member):
|
async def on_member_update(self, before: discord.Member, after: discord.Member):
|
||||||
|
findcur = self.squadjs.cursor(buffered=True)
|
||||||
|
findcur.execute(self.squadjs_findByDiscordID, [f"{after.id}"])
|
||||||
|
if findcur.arraysize <= 0:
|
||||||
|
return
|
||||||
|
userdata = findcur.fetchone()
|
||||||
log.info(f"Updating {after.name} ({after.id})")
|
log.info(f"Updating {after.name} ({after.id})")
|
||||||
rmroles = []
|
rmroles = []
|
||||||
for befrole in before.roles:
|
for befrole in before.roles:
|
||||||
@ -61,8 +74,24 @@ class Bot(commands.Bot):
|
|||||||
rmroles.remove(aftrole.id)
|
rmroles.remove(aftrole.id)
|
||||||
log.info(f"roles found to remove from {after.name}: {rmroles}")
|
log.info(f"roles found to remove from {after.name}: {rmroles}")
|
||||||
for rmroleid in rmroles:
|
for rmroleid in rmroles:
|
||||||
|
if not rmroleid in self.whitelistGrps.keys():
|
||||||
|
continue
|
||||||
self.whitelistGrps[rmroleid].delMember(before.id)
|
self.whitelistGrps[rmroleid].delMember(before.id)
|
||||||
|
|
||||||
|
addroles = []
|
||||||
|
for aftrole in after.roles:
|
||||||
|
addroles.append(aftrole.id)
|
||||||
|
for befrole in before.roles:
|
||||||
|
for aftrole in after.roles:
|
||||||
|
if aftrole.id == befrole.id:
|
||||||
|
addroles.remove(befrole.id)
|
||||||
|
log.info(f"roles found to add to {after.name}: {addroles}")
|
||||||
|
for addroleid in addroles:
|
||||||
|
if not addroleid in self.whitelistGrps.keys():
|
||||||
|
continue
|
||||||
|
self.whitelistGrps[addroleid].addMember(config.WhitelistMember(after.id, after.nick, userdata[0]))
|
||||||
|
self.squadjs.commit()
|
||||||
|
|
||||||
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
|
||||||
|
Loading…
Reference in New Issue
Block a user