I’m building a Discord bot using discord.js that should automatically assign roles to members. The bot reads its prefix from a configuration file but the commands aren’t responding when I test them in my server.
main.js
const { Client, Intents, EmbedBuilder } = require("discord.js");
const database = require("quick.db");
const settings = require("./settings.json");
const bot = new Client({ intents: 32767 });
bot.once("ready", () => {
console.log("Bot is running!");
});
bot.runCommand = async function (bot, msg, parameters, cmdPrefix) {
const commandFile = require(`./commands/rolemanager`);
commandFile.run(bot, msg, parameters, cmdPrefix);
};
bot.login(settings.botToken);
rolemanager.js
const { EmbedBuilder } = require("discord.js");
const database = require("quick.db");
module.exports = {
commandName: "setrole",
shortcuts: ["sr"],
info: "Configure automatic role assignment.",
run(bot, msg, parameters, cmdPrefix) {
if (!msg.member.permissions.has("Administrator")) {
const errorEmbed = new EmbedBuilder()
.setColor("#Red")
.setTitle("🚫 Access Denied 🚫")
.setDescription("You need administrator permissions for this command.")
.setTimestamp();
return msg.channel.send({ embeds: [errorEmbed] });
}
const operation = parameters[0];
const targetRole = msg.mentions.roles.first() ||
msg.guild.roles.cache.find(r => r.name === parameters[1]);
if (!operation || !targetRole) {
const usageEmbed = new EmbedBuilder()
.setColor("#Red")
.setTitle("🚫 Wrong Format 🚫")
.setDescription(`Use: \`${cmdPrefix}setrole create/delete @role\``);
return msg.channel.send({ embeds: [usageEmbed] });
}
if (operation === "create") {
database.set(`role_${msg.guildId}`, targetRole.id);
const successEmbed = new EmbedBuilder()
.setColor("#Green")
.setTitle("✅ Role Configuration Updated ✅")
.setDescription(`Auto role \`${targetRole.name}\` has been configured.`);
return msg.channel.send({ embeds: [successEmbed] });
}
}
};
settings.json
{
"botToken": "your-token-here",
"cmdPrefix": "!"
}
The bot shows as online but when I type commands in the server nothing happens. No error messages appear in the console either. What could be causing this issue?