made bot leave all servers it shouldn't be in

This commit is contained in:
Rene Kievits
2024-12-20 01:03:35 +01:00
parent 0333a2501f
commit ef3399c7ef
2 changed files with 32 additions and 8 deletions

View File

@@ -236,12 +236,34 @@ client.on('messageReactionRemove', async (reaction, user) => {
client.once('ready', async () => { client.once('ready', async () => {
console.log(`Logged in as ${client.user.tag} `) console.log(`Logged in as ${client.user.tag} `)
const unauthorizedGuilds = client.guilds.cache.filter(guild => guild.id !== process.env.SERVER_ID)
const leavePromises = unauthorizedGuilds.map(async guild => {
console.log(`Leaving unauthorized server: ${guild.id}`);
await guild.leave();
})
await Promise.all(leavePromises)
startBirthdayCheckCron(client) startBirthdayCheckCron(client)
startEventCheckCron(client) startEventCheckCron(client)
updateGlobalMessage(client) updateGlobalMessage(client)
//initReactionPerRole(client) //initReactionPerRole(client)
}) })
client.on('guildCreate', guild => {
if (guild.id !== process.env.SERVER_ID) {
console.log(`Unauthorized server joined: ${guild.name} (${guild.id})`)
guild.leave()
.then(() => console.log(`Left unauthorized server: ${guild.name}`))
.catch(console.error)
}
})
client.on('guildDelete', guild => {
console.log(`Bot removed from server: ${guild.name} (${guild.id})`)
})
const connectDiscord = async () => { const connectDiscord = async () => {
try { try {
console.log('Started refreshing application (/) commands.') console.log('Started refreshing application (/) commands.')

View File

@@ -69,20 +69,22 @@ const startEventCheckCron = async (client) => {
}) })
} }
}) })
cron.schedule('0 6 * * *', async () => { cron.schedule('0 7 * * *', async () => {
for (const [_, oauthGuild] of await client.guilds.fetch()) { for (const [_, oauthGuild] of await client.guilds.fetch()) {
const guild = await oauthGuild.fetch() const guild = await oauthGuild.fetch()
const channel = (await guild.channels.fetch()).find(ch => ch.name === 'reminder') const channel = (await guild.channels.fetch()).find(ch => ch.name === 'reminder')
if (!channel) continue if (!channel) continue
try { for (let index = 0; index < 10; index++) {
const messages = await channel.messages.fetch({ limit: 1000 }) try {
messages.forEach(async (message) => { const messages = await channel.messages.fetch({ limit: 100 })
await message.delete().catch(console.error) messages.forEach(async (message) => {
}) await message.delete().catch(console.error)
} catch (error) { })
console.error('Error clearing messages:', error) } catch (error) {
console.error('Error clearing messages:', error)
}
} }
} }
}) })