addition and subtraction

part of index.js
client.on('interactionCreate', (interaction) => {

if (interaction.commandName === 'add') {
const num1 = interaction.options.get('first-number');
const num2 = interaction.options.get('second-number');

interaction.reply(`The sum is ${num1 + num2}`);
}

if (interaction.commandName === 'subtract') {
const num1 = interaction.options.get('first-number');
const num2 = interaction.options.get('second-number');

interaction.reply(`The difference is ${num1 - num2}`);
}
})
client.on('interactionCreate', (interaction) => {

if (interaction.commandName === 'add') {
const num1 = interaction.options.get('first-number');
const num2 = interaction.options.get('second-number');

interaction.reply(`The sum is ${num1 + num2}`);
}

if (interaction.commandName === 'subtract') {
const num1 = interaction.options.get('first-number');
const num2 = interaction.options.get('second-number');

interaction.reply(`The difference is ${num1 - num2}`);
}
})
part of register-commands.js
const commands = [
{
name: 'add',
description: 'adds two numbers',
options: [
{
name: 'first-number',
description: 'the first number to be added',
type: ApplicationCommandOptionType.Number,
required: true,
},
{
name: 'second-number',
description: 'the second number to be added',
type: ApplicationCommandOptionType.Number,
required: true,
}
]
},
{
name: 'subtract',
description: 'subtracts two numbers',
options: [
{
name: 'first-number',
description: 'the first number to be subtracted',
type: ApplicationCommandOptionType.Number,
required: true,
},
{
name: 'second-number',
description: 'the second number to be subtracted',
type: ApplicationCommandOptionType.Number,
required: true,
}
]
},
];
const commands = [
{
name: 'add',
description: 'adds two numbers',
options: [
{
name: 'first-number',
description: 'the first number to be added',
type: ApplicationCommandOptionType.Number,
required: true,
},
{
name: 'second-number',
description: 'the second number to be added',
type: ApplicationCommandOptionType.Number,
required: true,
}
]
},
{
name: 'subtract',
description: 'subtracts two numbers',
options: [
{
name: 'first-number',
description: 'the first number to be subtracted',
type: ApplicationCommandOptionType.Number,
required: true,
},
{
name: 'second-number',
description: 'the second number to be subtracted',
type: ApplicationCommandOptionType.Number,
required: true,
}
]
},
];
results for addition: The sum is [object Object][object Object] results for subtraction: The difference is NaN
3 Replies
d.js toolkit
d.js toolkit9mo 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
Spaxter
Spaxter9mo ago
It should be interaction.options.getNumber .get returns a CommandInteractionOption object
f(x) = 1/x
f(x) = 1/xOP9mo ago
oh i got it thanks
Want results from more Discord servers?
Add your server