Hey everyone! I’m working on a Discord bot using Discord.JS. I want it to give users a special role on my server after they authorize the bot for their account. But I’m stuck on how to check if a user has actually authorized the bot.
Here’s what I’ve got so far in my main.js
file:
const { Client, Events, GatewayIntentBits } = require('botkit');
const { secretKey } = require('./settings.json');
const { HashMap } = require('botkit')
const bot = new Client({ intents: [GatewayIntentBits.Servers] });
bot.actions = new HashMap();
const cmdDir = path.join(__dirname, 'actions');
const actionFolders = fs.readdirSync(cmdDir);
for (const folder of actionFolders) {
const actionsPath = path.join(cmdDir, folder);
const actionFiles = fs.readdirSync(actionsPath).filter(file => file.endsWith('.js'));
for (const file of actionFiles) {
const filePath = path.join(actionsPath, file);
const action = require(filePath);
if ('info' in action && 'run' in action) {
bot.actions.set(action.info.name, action);
} else {
console.log(`[ERROR] Action at ${filePath} is missing "info" or "run" property.`);
}
}
}
bot.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
const action = interaction.client.actions.get(interaction.commandName);
if (!action) {
console.error(`Action ${interaction.commandName} not found.`);
return;
}
try {
await action.run(interaction);
} catch (error) {
console.error(error);
const reply = { content: 'Oops! Something went wrong.', ephemeral: true };
if (interaction.replied || interaction.deferred) {
await interaction.followUp(reply);
} else {
await interaction.reply(reply);
}
}
});
Is this even possible? Any tips would be super helpful. Thanks!