Error - const roleId = interaction.options.getString('role_id');

Morning all, I've been having this error since last night and I've seen a few people with the same error on Stackoverflow but didn't find any working solutions... This is for my Role React Command and I've getting the error seen in the title. Any help would be great 🙂 addreactrole.js - Broken Area
module.exports = {
data: new SlashCommandBuilder()
.setName('addrolereact')
.setDescription('Reaction Role Addition')
.addStringOption(option =>
option
.setName('role_id')
.setDescription('The ID of the role to assign when reacted to')
.setRequired(true)
)
.addStringOption(option =>
option
.setName('message_text')
.setDescription('Custom text for the reaction message')
),

async execute(interaction, client) {
const roleOption = interaction.options.getString('role_id'); // <-- This is the broken line
if (!roleOptionId) {
return interaction.reply({ content: "Role ID option is missing or not a valid string.", ephemeral: true });
}

const roleId = roleOption;
const customText = interaction.options.getString('message_text'); // 'message_text' is optional

// Ensure roleId is a valid role ID
const role = interaction.guild.roles.cache.get(roleId);
if (!role) {
return interaction.reply({ content: "Invalid role ID!", ephemeral: true });
}
module.exports = {
data: new SlashCommandBuilder()
.setName('addrolereact')
.setDescription('Reaction Role Addition')
.addStringOption(option =>
option
.setName('role_id')
.setDescription('The ID of the role to assign when reacted to')
.setRequired(true)
)
.addStringOption(option =>
option
.setName('message_text')
.setDescription('Custom text for the reaction message')
),

async execute(interaction, client) {
const roleOption = interaction.options.getString('role_id'); // <-- This is the broken line
if (!roleOptionId) {
return interaction.reply({ content: "Role ID option is missing or not a valid string.", ephemeral: true });
}

const roleId = roleOption;
const customText = interaction.options.getString('message_text'); // 'message_text' is optional

// Ensure roleId is a valid role ID
const role = interaction.guild.roles.cache.get(roleId);
if (!role) {
return interaction.reply({ content: "Invalid role ID!", ephemeral: true });
}
ready.js (Nothings wrong here but might be needed)
// Import the interaction listener function and execute it
const addReactRoleListener = require('../commands/utility/addreactrole.js');

// Pass the 'client' object to the function when executing it
addReactRoleListener.execute(client);
// Import the interaction listener function and execute it
const addReactRoleListener = require('../commands/utility/addreactrole.js');

// Pass the 'client' object to the function when executing it
addReactRoleListener.execute(client);
No description
10 Replies
d.js toolkit
d.js toolkit14mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
d.js docs
d.js docs14mo ago
The order of function parameters must match between definition and function call.
function execute(client, interaction) { ... };
execute(interaction, client);
function execute(client, interaction) { ... };
execute(interaction, client);
- mismatch! you pass an interaction where the client is expected - mismatch! you pass the client where an interaction is expected
treble/luna
treble/luna14mo ago
also You never even pass in the interaction You just pass in your client
Slothy
SlothyOP14mo ago
You're talking about the ready.js? If so I've tried making it (client, interaction) Vice Versa. Regardless thanks for the info, I'll go ahead and try to fix it 🙂 It's right above the top part
function addReactRoleListener(client) {
client.on('interactionCreate', async (interaction) => {
if (!interaction.isButton()) return;

const roleId = interaction.customId === 'role_reaction';
if (!roleId) return;

const member = interaction.guild.members.cache.get(interaction.user.id);

if (member) {
if (member.roles.cache.has(roleId)) {
await member.roles.remove(roleId);
await interaction.reply({ content: 'Role removed!', ephemeral: true });
} else {
await member.roles.add(roleId);
await interaction.reply({ content: 'Role added!', ephemeral: true });
}
}
});
}
function addReactRoleListener(client) {
client.on('interactionCreate', async (interaction) => {
if (!interaction.isButton()) return;

const roleId = interaction.customId === 'role_reaction';
if (!roleId) return;

const member = interaction.guild.members.cache.get(interaction.user.id);

if (member) {
if (member.roles.cache.has(roleId)) {
await member.roles.remove(roleId);
await interaction.reply({ content: 'Role removed!', ephemeral: true });
} else {
await member.roles.add(roleId);
await interaction.reply({ content: 'Role added!', ephemeral: true });
}
}
});
}
treble/luna
treble/luna14mo ago
I dont see you handling that command interaction anywhere
Slothy
SlothyOP14mo ago
It's at the bottom here
// Insert the reaction role info into the database
db.run(
'INSERT INTO role_reactions (messageId, channelId, roleId) VALUES (?, ?, ?)',
[sentMessage.id, interaction.channelId, roleId],
(err) => {
if (err) {
console.error('Error inserting data into the database:', err);
return;
}
console.log(`Role reaction button added for the ${role.name} role!`);
}
);

// listener for button interactions
addReactRoleListener(client);
}
// Insert the reaction role info into the database
db.run(
'INSERT INTO role_reactions (messageId, channelId, roleId) VALUES (?, ?, ?)',
[sentMessage.id, interaction.channelId, roleId],
(err) => {
if (err) {
console.error('Error inserting data into the database:', err);
return;
}
console.log(`Role reaction button added for the ${role.name} role!`);
}
);

// listener for button interactions
addReactRoleListener(client);
}
treble/luna
treble/luna14mo ago
no The only thing i see you handling are buttons Not ChatInputCommands
Slothy
SlothyOP14mo ago
Do you have a /tag for that lol
d.js docs
d.js docs14mo ago
method BaseInteraction#isChatInputCommand() Indicates whether this interaction is a ChatInputCommandInteraction.
Slothy
SlothyOP14mo ago
❤️
Want results from more Discord servers?
Add your server