bvs-nitro-did/index.js

99 lines
3.2 KiB
JavaScript

require("dotenv").config({ quiet: true });
const Discord = require("discord.js");
const client = new Discord.Client({
intents: [
Discord.IntentsBitField.Flags.Guilds,
Discord.IntentsBitField.Flags.GuildMembers
]
})
const bvs = require("./bvs");
const mariadb = require("mariadb");
const pool = mariadb.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
connectionLimit: 5
});
global.db = {
pool,
query: async (query, params) => {
// Use a one time connection (or no connection at all)
let conn;
try {
conn = await pool.getConnection();
const res = await conn.query(query, params);
return res;
} catch (error) {
console.error("Database query error:", error);
throw error;
} finally {
if (conn) conn.release();
}
}
}
const { REST, Routes } = require("discord.js");
const rest = new REST({ version: "10" }).setToken(process.env.DISCORD_TOKEN);
function checkNitroBoosts() {
bvs.getPremiumDIDs().then(dids => {
// map to object userid: did
const userDIDs = {};
dids.forEach(did => {
userDIDs[did.userId] = did;
});
client.guilds.cache.get(process.env.HOME_GUILD).members.fetch().then(members => {
members.forEach(member => {
if (userDIDs[member.id] && !member.premiumSince) {
// I dont feel comfortable handling deletions automatically just yet, log to console
console.log(`User ${member.user.tag} (${member.id}) is no longer boosting but still has a DID ${bvs.formatPhoneNumber(userDIDs[member.id].did)}. Please investigate and delete the DID if necessary.`);
}
});
}).catch(error => {
console.error("Error fetching guild members:", error);
});
}).catch(error => {
console.error("Error fetching premium DIDs:", error);
});
}
client.on("clientReady", () => {
console.log(`Logged in as ${client.user.tag}!`);
// Check that HOME_GUILD is set, and actually exists (as far as the client can see)
if (!process.env.HOME_GUILD) {
console.error("HOME_GUILD environment variable is not set. Please set it to the ID of the guild you want to use for testing.");
process.exit(1);
}
const homeGuild = client.guilds.cache.get(process.env.HOME_GUILD);
if (!homeGuild) {
console.error(`Could not find guild with ID ${process.env.HOME_GUILD}. Please make sure the bot is in that guild and that the ID is correct.`);
process.exit(1);
}
const commands = require("./commands");
rest.put(Routes.applicationGuildCommands(client.user.id, process.env.HOME_GUILD), { body: commands })
.then(() => console.log("Successfully registered application commands."))
.catch(console.error);
checkNitroBoosts();
setInterval(checkNitroBoosts, 60 * 60 * 1000)
});
client.on('interactionCreate', async interaction => {
// Check if there is a handler for the interaction in interactions/{interaction.type}/{interaction.commandName}.js, and if so, run it.
if (interaction.isChatInputCommand()) {
try {
const handler = require(`./interactions/chatCommand/${interaction.commandName}.js`);
await handler(interaction, client, bvs);
} catch (error) {
console.error(`Error handling interaction ${interaction.id}:`, error);
}
}
});
client.login(process.env.DISCORD_TOKEN);