2023-03-04 18:32:59 -06:00
|
|
|
import os.path
|
|
|
|
|
2023-03-04 12:12:17 -06:00
|
|
|
import jsonpickle
|
2023-02-05 04:32:26 -06:00
|
|
|
import logging
|
|
|
|
import discord
|
2023-04-17 20:46:53 -05:00
|
|
|
import mysql.connector
|
2023-02-05 04:32:26 -06:00
|
|
|
from discord.ext import commands
|
|
|
|
from autowl import config
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Bot(commands.Bot):
|
2023-04-17 20:46:53 -05:00
|
|
|
squadjs_updateDiscordID = ("UPDATE DBLog_SteamUsers SET discordID = %s "
|
|
|
|
"WHERE steamID = %s")
|
|
|
|
|
|
|
|
squadjs_findByDiscordID = ("SELECT * FROM DBLog_SteamUsers "
|
|
|
|
"WHERE discordID = %s")
|
|
|
|
|
2023-03-04 18:32:59 -06:00
|
|
|
whitelistGrps = {}
|
2023-02-05 04:32:26 -06:00
|
|
|
|
2023-03-04 18:32:59 -06:00
|
|
|
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
|
2023-03-04 12:12:17 -06:00
|
|
|
|
2023-04-17 20:46:53 -05:00
|
|
|
def __init__(self, config: config.DiscordClientConfig, mysqlpass):
|
2023-02-05 04:32:26 -06:00
|
|
|
self.config = config
|
|
|
|
intents = discord.Intents.default()
|
|
|
|
intents.message_content = True
|
|
|
|
intents.members = True
|
|
|
|
super().__init__(
|
|
|
|
command_prefix=commands.when_mentioned_or("&"),
|
|
|
|
intents=intents,
|
|
|
|
help_command=commands.DefaultHelpCommand(dm_help=True),
|
|
|
|
)
|
2023-04-17 22:11:05 -05:00
|
|
|
self.squadjs = mysql.connector.connect(user='squadjs', password=mysqlpass, host='asgard.orion-technologies.io', database='squadjs', use_pure=False)
|
2023-02-05 04:32:26 -06:00
|
|
|
|
2023-02-22 22:13:39 -06:00
|
|
|
async def on_command(self, ctx: commands.Context):
|
|
|
|
log.info(f"{ctx.author} ({ctx.author.id}) invoked command: {ctx.command.name}, {ctx.message}")
|
|
|
|
|
2023-02-05 04:32:26 -06:00
|
|
|
async def on_ready(self):
|
|
|
|
log.info(f"Logged in as '{self.user}' ({self.user.id})")
|
|
|
|
log.info(
|
|
|
|
f"Have access to the following guilds: "
|
|
|
|
f"{', '.join([str(guild.name) + ' (' + str(guild.id) + ')' for guild in self.guilds])}"
|
|
|
|
)
|
|
|
|
|
|
|
|
for guild in self.guilds:
|
|
|
|
self.tree.copy_global_to(guild=guild)
|
|
|
|
await self.tree.sync(guild=guild)
|
|
|
|
log.info(f"Synced guild: {guild.name}")
|
|
|
|
|
|
|
|
await self.tree.sync()
|
|
|
|
|
2023-03-04 18:32:59 -06:00
|
|
|
async def on_member_update(self, before: discord.Member, after: discord.Member):
|
2023-04-17 21:20:39 -05:00
|
|
|
disusername = after.user.nick if after.user.nick is not None else after.user.name
|
2023-04-17 20:46:53 -05:00
|
|
|
findcur = self.squadjs.cursor(buffered=True)
|
|
|
|
findcur.execute(self.squadjs_findByDiscordID, [f"{after.id}"])
|
|
|
|
if findcur.arraysize <= 0:
|
|
|
|
return
|
|
|
|
userdata = findcur.fetchone()
|
2023-04-17 21:20:39 -05:00
|
|
|
log.info(f"Updating {disusername} ({after.id})")
|
2023-03-04 18:32:59 -06:00
|
|
|
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)
|
2023-04-17 21:20:39 -05:00
|
|
|
log.info(f"roles found to remove from {disusername}: {rmroles}")
|
2023-03-04 18:32:59 -06:00
|
|
|
for rmroleid in rmroles:
|
2023-04-17 20:46:53 -05:00
|
|
|
if not rmroleid in self.whitelistGrps.keys():
|
|
|
|
continue
|
2023-03-04 18:32:59 -06:00
|
|
|
self.whitelistGrps[rmroleid].delMember(before.id)
|
|
|
|
|
2023-04-17 20:46:53 -05:00
|
|
|
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)
|
2023-04-17 21:20:39 -05:00
|
|
|
log.info(f"roles found to add to {disusername}: {addroles}")
|
2023-04-17 20:46:53 -05:00
|
|
|
for addroleid in addroles:
|
|
|
|
if not addroleid in self.whitelistGrps.keys():
|
|
|
|
continue
|
2023-04-17 21:20:39 -05:00
|
|
|
self.whitelistGrps[addroleid].addMember(config.WhitelistMember(after.id, disusername, userdata[0]))
|
2023-04-17 20:46:53 -05:00
|
|
|
self.squadjs.commit()
|
|
|
|
|
2023-02-05 04:32:26 -06:00
|
|
|
async def setup_hook(self):
|
|
|
|
log.info("Setting up bot")
|
|
|
|
from autowl import Cogs
|
|
|
|
|
|
|
|
await self.add_cog(Cogs.Whitelist(self))
|
|
|
|
await self.add_cog(Cogs.Group(self))
|
|
|
|
|
|
|
|
def start_bot(self):
|
|
|
|
log.info("Starting discord bot")
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.run(self.config.login_token, log_handler=None)
|
|
|
|
except discord.errors.LoginFailure as e:
|
|
|
|
log.debug(f"Received login failure: {e}")
|
|
|
|
log.error("Failed to login to discord, check your discord token!")
|
|
|
|
raise e
|