DiscordWhitelist/autowl/bot.py

145 lines
5.3 KiB
Python
Raw Permalink Normal View History

2023-03-04 18:32:59 -06:00
import os.path
import jsonpickle
import logging
import discord
import mysql.connector
from discord.ext import commands
from autowl import config
log = logging.getLogger(__name__)
class Bot(commands.Bot):
2024-10-01 18:27:04 -05:00
squadjs_updateDiscordID = (
"UPDATE DBLog_SteamUsers SET discordID = %s " "WHERE steamID = %s"
)
2024-10-01 18:27:04 -05:00
squadjs_findByDiscordID = "SELECT * FROM DBLog_SteamUsers " "WHERE discordID = %s"
2023-03-04 18:32:59 -06:00
whitelistGrps = {}
2024-10-01 18:27:04 -05:00
if not os.path.exists("./wlgrps"):
os.makedirs("./wlgrps")
2023-03-04 18:32:59 -06:00
else:
2024-10-01 18:27:04 -05:00
for wlfile in os.listdir("./wlgrps"):
2023-03-04 18:32:59 -06:00
filename = os.fsdecode(wlfile)
2024-10-01 18:27:04 -05:00
if filename.endswith(".json"):
file = open(f"./wlgrps/{filename}", "r")
2023-03-04 18:32:59 -06:00
wlgrp: config.WhitelistGroup = jsonpickle.decode(file.read())
whitelistGrps[wlgrp.discord_role_id] = wlgrp
def __init__(self, config: config.DiscordClientConfig, mysqlpass):
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),
)
2024-10-01 18:27:04 -05:00
self.squadjs = mysql.connector.connect(
user="squadjs",
password=mysqlpass,
host="asgard.orion-technologies.io",
database="squadjs",
use_pure=False,
)
2023-02-22 22:13:39 -06:00
async def on_command(self, ctx: commands.Context):
2024-10-01 18:27:04 -05:00
log.info(
f"{ctx.author} ({ctx.author.id}) invoked command: {ctx.command.name}, {ctx.message}"
)
2023-02-22 22:13:39 -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])}"
)
2023-04-17 23:35:39 -05:00
#
# 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()
# HACK: Ensure correct members are synced to their groups when bot is started
for guild in self.guilds:
for wlgrp in self.whitelistGrps.values():
if role := guild.get_role(wlgrp.discord_role_id):
role_member_ids = set([member.id for member in role.members])
wlgrp_member_ids = set(
[
wlgrp_member.discord_id
for wlgrp_member in wlgrp.members.values()
]
)
log.info(
f"Checking group membership for {role.name} ({role.id}) on startup"
)
members_to_remove = wlgrp_member_ids - role_member_ids
for member_id in members_to_remove:
log.info(
f"Removing {member_id} from {role.id} role group"
)
wlgrp.delMember(member_id)
2023-03-04 18:32:59 -06:00
async def on_member_update(self, before: discord.Member, after: discord.Member):
2023-04-17 22:39:34 -05:00
disusername = after.nick if after.nick is not None else after.name
2023-08-01 21:17:23 -05:00
self.squadjs.connect()
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:
2024-10-01 18:27:04 -05:00
if not (rmroleid in self.whitelistGrps.keys()):
continue
2023-03-04 18:32:59 -06:00
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)
2023-04-17 21:20:39 -05:00
log.info(f"roles found to add to {disusername}: {addroles}")
for addroleid in addroles:
2024-10-01 18:27:04 -05:00
if not (addroleid in self.whitelistGrps.keys()):
continue
2024-10-01 18:27:04 -05:00
self.whitelistGrps[addroleid].addMember(
config.WhitelistMember(after.id, disusername, userdata[0])
)
self.squadjs.commit()
2023-08-01 21:17:23 -05:00
self.squadjs.close()
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