bansync/sync.js
2026-01-17 16:05:33 -07:00

91 lines
4.5 KiB
JavaScript

const colors = require('colors');
const { PermissionFlagsBits } = require('discord.js');
module.exports = (client, db) => {
// Handle auto_add guilds
db.all("SELECT guild_id, list_id, auto_add FROM guilds WHERE auto_add = 1", async (err, rows) => {
if (err) {
console.log(`${colors.red('[ERROR]')} Could not query guilds for auto_add:`, err);
return;
}
if (rows) {
for (const row of rows) {
// Check we can access guild and have ban perms
const guild = await client.guilds.fetch(row.guild_id);
if (!guild) {
console.log(`[ERROR] Could not find guild: ${row.guild_id}`); // We don't remove if they re-add the bot
continue;
}
const member = await guild.members.fetch(client.user.id);
if (!member || !member.permissions.has(PermissionFlagsBits.BanMembers)) {
console.log(`${colors.red('[ERROR]')} Bot does not have ban permissions in guild: ${row.guild_id}`);
continue;
}
try {
const listId = row.list_id;
const guildId = row.guild_id;
const guild = await client.guilds.fetch(guildId);
const member = await guild.members.fetch(client.user.id);
const guildBans = await guild.bans.fetch();
db.all("SELECT user_id, reason FROM bans WHERE list_id = ?", [listId], async (err, bans) => {
for (const ban of bans) {
if (!guildBans.has(ban.user_id)) {
await guild.members.ban(ban.user_id, { reason: `Auto-synced ban from list ${listId}: ${ban.reason}` });
if (process.env.ENVIRONMENT !== 'production') {
console.log(`[AUTO-ADD] Banned user ${ban.user_id} in guild ${guildId} from list ${listId}`);
};
}
}
});
} catch (e) {
console.log(`[ERROR] Could not sync bans for guild ${row.guild_id}:`, e);
}
}
}
});
// Handle auto_remove guilds
db.all("SELECT guild_id, list_id, auto_remove FROM guilds WHERE auto_remove = 1", async (err, rows) => {
if (err) {
console.log(`${colors.red('[ERROR]')} Could not query guilds for auto_remove:`, err);
return;
}
if (rows) {
for (const row of rows) {
// Check we can access guild and have ban perms
const guild = await client.guilds.fetch(row.guild_id);
if (!guild) {
console.log(`[ERROR] Could not find guild: ${row.guild_id}`); // We don't remove if they re-add the bot
continue;
}
const member = await guild.members.fetch(client.user.id);
if (!member || !member.permissions.has(PermissionFlagsBits.BanMembers)) {
console.log(`${colors.red('[ERROR]')} Bot does not have ban permissions in guild: ${row.guild_id}`);
continue;
}
try {
const listId = row.list_id;
const guildId = row.guild_id;
db.all("SELECT user_id FROM bans WHERE list_id = ?", [listId], async (err, bans) => {
if (err) {
console.error(err);
return;
}
const guildBans = await guild.bans.fetch();
// Anybody not in the bans list should be unbanned
for (const [bannedUserId, banInfo] of guildBans) {
if (!bans.find(ban => ban.user_id === bannedUserId)) {
await guild.members.unban(bannedUserId, `Auto-removed ban from list ${listId}`);
if (process.env.ENVIRONMENT !== 'production') {
console.log(`[AUTO-REMOVE] Unbanned user ${bannedUserId} in guild ${guildId} from list ${listId}`);
};
}
}
});
} catch (e) {
console.log(`[ERROR] Could not sync unbans for guild ${row.guild_id}:`, e);
}
}
}
});
}