getNumber doesn't exists in CommandInteractionOptionResolver discord.js - javascript

I'm trying to create a simple add slash command with discord.js, but it seems that interaction.options.getNumber just doesn't exists (look at https://github.com/discordjs/discord.js/blob/main/packages/discord.js/src/structures/CommandInteractionOptionResolver.js#L181)
This is a piece of my code:
...
commands?.create({
name: 'add',
description: 'add two numbers',
options: [
{
name: 'num1',
description: 'The first num',
required: true,
type: DiscordJS.ApplicationCommandOptionType.Number
},
{
name: 'num2',
description: 'The second num',
required: true,
type: DiscordJS.ApplicationCommandOptionType.Number
} ]
})
})
...
client.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand()) {
return
}
const { commandName, options } = interaction
if (commandName === 'add') {
console.log(options)
const num1 = options.getNumber('num1')!
const num2 = options.get('num2')!
// idk why but "get" method exists. So I was trying to use it,
// but js says that you couldn't add a string | number | boolean value to an another one
console.log(num1.value + num2.value)
console.log(typeof(num2.value)) // this print "number" btw
interaction.reply({
content: `The sum is ${num1.value}`
})
}
}

There is a solution, but it's intricated:
...
} else if (commandName === 'add') {
console.log(options)
const raw_num1 = options.get('num1')!
const raw_num2 = options.get('num2')!
const num1 = raw_num1.value!
const num2 = raw_num2.value!
interaction.reply({
content: `The sum is ${+(num1) + +(num2)}`
})
}
})

Related

Cannot read properties of undefined (reading 'label') Discord.js button label sometimes does not exist

I made a coinflip command, and I need that the player can choose between heads or tails with 2 different buttons, and I need its label. 75% of the times the command executes correctly, but the other 25% crashes and sends this error: TypeError: Cannot read properties of undefined (reading 'label')
why is this happening?
const Coinflip: MsgCommand = {
name: "coinflip",
description: "Bet some chips and choose heads or tails!",
aliases: ["cf"],
exe: async ({ msg, args, authorID }) => {
let n = Number(args[0]);
let valid = await sqlite.bet(authorID, n, msg);
if (!valid) return true;
let choice: string;
let coin = math.flip() ? "heads" : "tails";
let hID = math.buttonID("heads");
let tID = math.buttonID("tails");
let row = new ActionRowBuilder<ButtonBuilder>().addComponents(
misc.Button({
customID: hID,
label: "Heads",
style: ButtonStyle.Secondary,
}),
misc.Button({
customID: tID,
label: "Tails",
style: ButtonStyle.Secondary,
})
);
let ebd = misc.Embed({
title: "Heads or tails?",
image: EmbedAssets.CoinflipStill,
});
let botMsg = await msg.channel.send({
embeds: [ebd],
components: [row],
});
let cl = msg.channel.createMessageComponentCollector({
filter: filters.coinflip([hID, tID], authorID),
max: 1,
idle: 15e3,
componentType: ComponentType.Button,
dispose: true,
});
cl.on("collect", async btnInt => {
await btnInt.deferUpdate();
choice = btnInt.component.label.toLowerCase();
});
cl.on("end",() => {
console.log(choice);
});
}
the error is in this line: choice = btnInt.component.label.toLowerCase();

distube filters not applying

I'm trying to make a discord slash commands filter using distube but when I use the slash command it didn't apply the filter that I picked but it sent the embed when filter is applied
Node: v17.7.2
Discord: ^13.2.0
can someone help me or tell me why the filter is not applying to the current music in vc?
thank you
const { MessageEmbed } = require('discord.js');
const ee = require('../../config.json');
module.exports = {
name: 'filter',
description: 'Add filter.',
usage: 'filter',
options: [
{
name: 'preset',
description: 'Filters to add.',
type: 'STRING',
required: true,
choices: [
{
name: 'BassBoost',
value: 'bassboost'
},
{
name: 'Nightcore',
value: 'nightcore'
},
{
name: 'Vaporwave',
value: 'vaporwave'
}
]
}
],
run: async(client, interaction, args) => {
const filterss = interaction.options.getString('preset');
const queue = client.distube.getQueue(interaction);
let player = client.distube.filters;
if(!queue) {
return interaction.followUp({embeds: [
new MessageEmbed()
.setColor(ee.color)
.setAuthor({name: 'Error', iconURL: 'https://i.imgur.com/81ig9jl.jpg'})
.setDescription('No songs are playing!')
]})
}
let thing = new MessageEmbed().setColor(ee.color);
if (filterss == "nightcore") {
thing.setDescription("✅ | Nightcore filter is now active!");
player.nightcore = true;
} else if (filterss == "bassboost") {
thing.setDescription("✅ | BassBoost filter is now on!");
player.bassboost = true;
} else if (filterss == "vaporwave") {
thing.setDescription("✅ | Vaporwave filter is now on!");
player.vaporwave = true;
}
return interaction.followUp({ embeds: [thing] });
}
}
From what I can find in the docs, it is recommended to use .setFilter() instead of calling client.distube.filters.<filter> = true.
I'd try changing your run code to use this instead and see if that works:
run: async(client, interaction, args) => {
// Your queue check code here
let thing = new MessageEmbed().setColor(ee.color);
if (filterss == 'nightcore') {
thing.setDescription('✅ | Nightcore filter is now active!');
client.distube.setFilter(interaction, filterss);
} else if (filterss == 'bassboost') {
thing.setDescription('✅ | BassBoost filter is now on!');
client.distube.setFilter(interaction, filterss);
} else if (filterss == 'vaporwave') {
thing.setDescription('✅ | Vaporwave filter is now on!');
client.distube.setFilter(interaction, filterss);
}
return interaction.followUp({
embeds: [thing],
});
}

How can I prompts loop in Node.js?

I want the main.js loop to be re-selectable when the user has selected option.
Here is the code.
main.js
const prompts = require('prompts');
var option = require('./option.js');
(async () => {
const response = await prompts({
type: 'select',
name: 'value',
message: 'choice',
choices: [
{ title: 'option1', description: 'option1 description', value: '1' },
{ title: 'exit', description: 'exit from script',value: '0' }
],
initial: 1
}
).then(response => {
if(response.value == 1){
console.info('you select option1');
option.option1()
// i want to run main.js again here
}
else {
console.info('you select exit');
// i want to exit from main.js
}
});
})();
option.js
module.exports = {
option1:function() {
console.log("You have selected option 1")
}
}
When the user has selected option1, after the option.js script's function is done, I would like it to return to the new options page.
I've tried many ways but I still can't find a way. What can I try?
Is this the solution to your problem? Also notice when using await, dont use then.
const prompts = require('prompts');
var option = require('./option.js');
async function prompt () {
const response = await prompts({
type: 'select',
name: 'value',
message: 'choice',
choices: [
{ title: 'option1', description: 'option1 description', value: '1' },
{ title: 'exit', description: 'exit from script',value: '0' }
],
initial: 1
})
return response.value === '1'?option.option1() & prompt():false
}
prompt()

How to get an object value inside another object by a property value in Javascript

I have an object like this
const test = {
'/blogs/architecture':
{
name: 'Architecture',
icon: 'archway',
iconCategory: 'fas',
slug: '/blogs/architecture'
}
}
Now I want find the slug field and I have only the name field. How can I find the slug by the name? please help
Try utilizing Object.values() with Array#find():
const test = {
'/blogs/architecture': {
name: 'Architecture',
icon: 'archway',
iconCategory: 'fas',
slug: '/blogs/architecture'
}
};
const findSlugByName = name => {
return Object.values(test).find(obj => obj.name === name)?.slug;
}
console.log(findSlugByName('Architecture'));
const test = {
'/blogs/architecture': {
name: 'Architecture',
icon: 'archway',
iconCategory: 'fas',
slug: '/blogs/architecture'
},
'/blogs/architecture2': {
name: 'Architecture2',
icon: 'archway',
iconCategory: 'fas',
slug: '/blogs/architecture'
}
}
const searchName = 'Architecture';
let searchResult;
const node = Object.values(test).forEach((node) => {
if(node.name === searchName) {
searchResult = node;
}
});
console.log(searchResult);

Check if userid matches one in the array

I'm attempting to check if a user's ID is in this array and if they are, also get the "text" from it.
Array:
const staff = [
{
user: '245569534218469376',
text: 'dev'
},
{
user: '294597887919128576',
text: 'loner'
}
];
I've tried if (staff.user.includes(msg.member.id)) (Which I didn't think was going to work, and didn't.)
const findUser = (users, id) => users.find(user => user.id === id)
const usersExample = [
{
id: '123456765',
text: 'sdfsdfsdsd'
},
{
id: '654345676',
text: 'fdgdgdg'
}
]
//////////////////
const user = findUser(usersExample, '123456765')
console.log(user && user.text)
The some method on an array is used to tell if an item meets a condition, it is similar to the find method but the find method returns the item where the some method return true or false.
const staff = [
{
user: '245569534218469376',
text: 'dev'
},
{
user: '294597887919128576',
text: 'loner'
}
];
const isStaff = (staff, id) => staff.some(s => s.user === id);
console.log(isStaff(staff, '123'));
console.log(isStaff(staff, '245569534218469376'));
You may try something like this:
const staff = [
{
user: '245569534218469376',
text: 'dev'
},
{
user: '294597887919128576',
text: 'loner'
}
];
let item = staff.find(item => item.user == '294597887919128576'); // msg.member.id
if (item) {
console.log(item.text);
}
One another way to do that is:
const inArray = (array, id) => array.filter(item => item.user === id).length >= 1;
const users = [
{
user: '245569534218469356',
text: 'foo'
}, {
user: '245564734218469376',
text: 'bar'
}, {
user: '246869534218469376',
text: 'baz'
}
];
console.log(inArray(users, '246869534218469376')); // true
console.log(inArray(users, '222479534218469376')); // false

Categories