bvs-nitro-did/index.js
2026-06-12 10:30:21 -06:00

108 lines
4.4 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 { REST, Routes } = require("discord.js");
const rest = new REST({ version: "10" }).setToken(process.env.DISCORD_TOKEN);
async function getAllDIDs() {
return new Promise((resolve, reject) => {
fetch(`https://portal.bulkvs.com/api/v1.0/tnRecord?Status=Active&Trunk%20Group=${process.env.TRUNK_GROUP}`, {
headers: {
"Authorization": `Bearer ${process.env.BVS_TOKEN}`,
}
})
.then(res => {
if (!res.ok) {
throw new Error(`Error fetching DIDs: ${res.status} ${res.statusText}`);
}
return res.json();
})
.then(data => {
data.forEach(record => {
console.log(record.ReferenceID);
});
// data is array of objects. If object.ReferenceID.split(";;")[0] is premDID, then [1] is the user id, and anything after is a note
const dids = data.filter(record => record.ReferenceID && record.ReferenceID.startsWith(";;")).map(record => {
// Get all flags (just keep looking at the next split item until its not a valid flag, after that, it's all a custom note, most likely to be used fo)
return {
did: record.TN,
userId: record.ReferenceID.split(";;")[1],
}
});
resolve(dids);
})
.catch(error => {
console.error("Error fetching DIDs:", error);
reject(error);
});
});
}
getAllDIDs().then(dids => {
console.log(`Fetched ${dids.length} DIDs from BulkVS.`);
console.log(dids)
}).catch(error => {
console.error("Error fetching 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);
// Check that we have a valid BVS_TOKEN with https://portal.bulkvs.com/api/v1.0/accountInfo (Bearer token in Authorization header)
if (!process.env.BVS_TOKEN) {
console.error("BVS_TOKEN environment variable is not set. Please set it to a valid token from https://portal.bulkvs.com/api/v1.0/accountInfo (Bearer token in Authorization header).");
process.exit(1);
}
fetch("https://portal.bulkvs.com/api/v1.0/accountDetail", {
headers: {
"Authorization": `Bearer ${process.env.BVS_TOKEN}`,
}
}).then(res => {
if (!res.ok) {
throw new Error(`BVS_TOKEN is invalid: ${res.status} ${res.statusText}`);
}
// Get raw text
return res.json();
}).then(data => {
console.log(`Validated BulkVS Token. Account Contact is ${data["Main Contact"].Name}`)
}).catch(error => {
console.error("Error validating BVS_TOKEN:", error);
process.exit(1);
});
});
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);
} catch (error) {
console.error(`Error handling interaction ${interaction.id}:`, error);
}
}
});
client.login(process.env.DISCORD_TOKEN);