MemberCount/index.js
2025-03-15 23:40:49 -06:00

142 lines
5 KiB
JavaScript

require("dotenv").config();
const Discord = require("discord.js");
const colors = require("colors");
const sqlite3 = require("sqlite3").verbose();
const db = new sqlite3.Database("./database.db", (err) => {
if (err) {
console.error(err.message);
}
console.log(`${colors.cyan("[INFO]")} Connected to the database!`);
});
db.on("open", () => {
db.run("CREATE TABLE IF NOT EXISTS guilds (guild_id TEXT PRIMARY KEY, channel_id TEXT UNIQUE);");
})
const client = new Discord.Client({
intents: [
"Guilds",
"GuildMembers"
]
});
function updateChannels() {
// Get all channels from the database, loop through them and check if each exists, ones that dont, remove them, ones that do, update their name to `Member Count: ${client.guilds.cache.get(row.guild_id).members.cache.filter(member => !member.user.bot).size}`. Do one at a time with a 1 second delay between each.
db.all("SELECT * FROM guilds;", (err, rows) => {
if (err) {
console.error(err.message);
}
var delay = 0;
rows.forEach((row) => {
setTimeout(async () => {
const channel = client.channels.cache.get(row.channel_id);
if (!channel) {
db.run("DELETE FROM guilds WHERE guild_id = ?;", [row.guild_id], (err) => {
if (err) {
console.error(err.message);
}
});
} else {
await client.guilds.cache.get(row.guild_id).members.fetch();
console.log(`${colors.cyan("[INFO]")} Updating channel ${channel.name} in guild ${client.guilds.cache.get(row.guild_id).name} (${row.guild_id})`);
channel.setName(`Member Count: ${client.guilds.cache.get(row.guild_id).members.cache.filter(member => !member.user.bot).size}`).catch(err => {
console.log(`${colors.red("[ERROR]")} Cannot update channel name. ID: ${channel.id} | Error: ${err}`);
db.run("DELETE FROM guilds WHERE guild_id = ?;", [row.guild_id], (err) => {
if (err) {
console.error(err.message);
}
});
});;
}
delay++;
}, delay * 1000)
});
});
}
function updateChannel(guild_id) {
db.get("SELECT * FROM guilds WHERE guild_id = ?;", [guild_id], (err, row) => {
if (err) {
console.error(err.message);
}
client.channels.fetch(row.channel_id).then(async channel => {
console.log(channel.name)
if (!channel) {
db.run("DELETE FROM guilds WHERE guild_id = ?;", [guild_id], (err) => {
if (err) {
console.error(err.message);
}
});
} else {
await client.guilds.cache.get(guild_id).members.fetch();
console.log(`${colors.cyan("[INFO]")} Updating channel ${channel.name} in guild ${client.guilds.cache.get(row.guild_id).name} (${row.guild_id})`);
channel.setName(`Member Count: ${client.guilds.cache.get(guild_id).members.cache.filter(member => !member.user.bot).size}`).catch(err => {
console.log(`${colors.red("[ERROR]")} Cannot update channel name. ID: ${channel.id} | Error: ${err}`);
db.run("DELETE FROM guilds WHERE guild_id = ?;", [guild_id], (err) => {
if (err) {
console.error(err.message);
}
});
});
}
})
});
};
client.on("ready", async () => {
console.log(`${colors.cyan("[INFO]")} Logged in as ${client.user.displayName}!`);
// Register slash commands
const commands = await require("./commands.json");
await (async () => {
try {
console.log(`${colors.cyan("[INFO]")} Registering Commands...`)
//Global
const rest = await new Discord.REST({ version: "10" }).setToken(client.token);
await rest.put(Discord.Routes.applicationCommands(client.user.id), { body: commands })
console.log(`${colors.cyan("[INFO]")} Successfully registered commands.`);
} catch (error) {
console.error(error);
}
})();
updateChannels();
setInterval(updateChannels, 60000);
});
client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;
switch (interaction.commandName) {
case "setchannel":
// Check if the user has manage channels
if (!interaction.member.permissions.has("MANAGE_CHANNELS")) {
return interaction.reply({ content: "You do not have permission to use this command!", ephemeral: true });
}
const channel = interaction.options.getChannel("channel");
// Check that the channel is in the same guild as the command interaction
if (channel.guild.id !== interaction.guild.id) {
return interaction.reply({ content: "The channel must be in the same guild as the command interaction!", ephemeral: true });
}
console.log(channel.id)
// All is well, set the channel in the database
db.run("INSERT OR REPLACE INTO guilds (guild_id, channel_id) VALUES (?, ?);", [interaction.guild.id, channel.id], (err) => {
if (err) {
console.error(err.message);
return interaction.reply({ content: "An error occurred while setting the channel!", ephemeral: true });
}
interaction.reply({ content: `Set the channel to <#${channel.id}>!`, ephemeral: true });
updateChannel(interaction.guild.id);
});
break;
}
});
client.on("guildMemberAdd", (member) => {
updateChannel(member.guild.id);
});
client.on("guildMemberRemove", (member) => {
updateChannel(member.guild.id);
});
client.login(process.env.TOKEN);