Not Kuma anymore
Not Kuma anymore
DIAdiscord.js - Imagine an app
Created by Not Kuma anymore on 1/28/2024 in #djs-questions
ButtonBuilder > TypeError: Cannot read properties of undefined (reading 'customId')
Part of Index.js
const { componentTogglerIds } = require('./commands/main/start-contract');
const buttonToggleCounters = {};
...
if (interaction.isButton()) {
try {
const buttonIndex = componentTogglerIds.indexOf(interaction.customId);

if (buttonIndex !== -1) {
const styles = [ButtonStyle.Success, ButtonStyle.Danger, ButtonStyle.Primary];

// Get the toggle counter for the specific button or initialize it to 0
let toggleCounter = buttonToggleCounters[interaction.customId] || 0;

// Toggle the button style
const newStyle = styles[toggleCounter % styles.length];

// Get the current button details

const currentButton = interaction.message.components[0].components[buttonIndex];
console.log(currentButton)
// HERE IT CRASHES AND CONSOLE LOG RETURNS UNDEFINED. EVERYWHERE BEFORE IT RETURNS PROPPER ID
const currentId = currentButton.customId;
const currentLabel = currentButton.label;

// Increment the toggle counter for the specific button
buttonToggleCounters[interaction.customId] = (toggleCounter + 1) % styles.length;

// Create a new button with the updated style
const newButton = new ButtonBuilder()
.setCustomId(currentId)
.setLabel(currentLabel)
.setStyle(newStyle);

// Replace the existing button in the MessageActionRow
interaction.message.components[0].components[buttonIndex] = newButton;

// Edit the message with the updated button style
await interaction.update({
components: [interaction.message.components[0]],
});
}
} catch (error) {
console.error(error);
}}
const { componentTogglerIds } = require('./commands/main/start-contract');
const buttonToggleCounters = {};
...
if (interaction.isButton()) {
try {
const buttonIndex = componentTogglerIds.indexOf(interaction.customId);

if (buttonIndex !== -1) {
const styles = [ButtonStyle.Success, ButtonStyle.Danger, ButtonStyle.Primary];

// Get the toggle counter for the specific button or initialize it to 0
let toggleCounter = buttonToggleCounters[interaction.customId] || 0;

// Toggle the button style
const newStyle = styles[toggleCounter % styles.length];

// Get the current button details

const currentButton = interaction.message.components[0].components[buttonIndex];
console.log(currentButton)
// HERE IT CRASHES AND CONSOLE LOG RETURNS UNDEFINED. EVERYWHERE BEFORE IT RETURNS PROPPER ID
const currentId = currentButton.customId;
const currentLabel = currentButton.label;

// Increment the toggle counter for the specific button
buttonToggleCounters[interaction.customId] = (toggleCounter + 1) % styles.length;

// Create a new button with the updated style
const newButton = new ButtonBuilder()
.setCustomId(currentId)
.setLabel(currentLabel)
.setStyle(newStyle);

// Replace the existing button in the MessageActionRow
interaction.message.components[0].components[buttonIndex] = newButton;

// Edit the message with the updated button style
await interaction.update({
components: [interaction.message.components[0]],
});
}
} catch (error) {
console.error(error);
}}
29 replies
DIAdiscord.js - Imagine an app
Created by Not Kuma anymore on 1/7/2024 in #djs-questions
command deploys faster than i can fetch info from db
My goofy ahh code:
const { SlashCommandBuilder, EmbedBuilder, AttachmentBuilder } = require('discord.js');
const { MongoClient } = require("mongodb");

const CLIENT = new MongoClient(process.env.MONGO_URI);
const DB = CLIENT.db("resources");
const COLLECTION = DB.collection("item_resources");

let choicesArray = [];
let choicesArray1 = [];

async function fetchChoices() {
try {
await CLIENT.connect();

// Find documents with 'id' field equal to 'emfReader' and project only the 'id' field
const id = COLLECTION.find({}, { projection: { id: 1, name: 1, _id: 0 } });

// Convert the cursor to an array and extract 'id' values
const documentsArray = await id.toArray();
choicesArray = documentsArray.map(document => ({ name: document.name, value: document.id }));

} catch (err) {
console.error(err);
}
}

fetchChoices().catch(console.dir);

fetchChoices().then(() => {
choicesArray1 = choicesArray;

}).catch(console.dir);

const data = new SlashCommandBuilder()
.setName('item1')
.setNameLocalizations({
"en-US": "item1",
"pl": "przedmiot1"
})
.setDescription('Get information about any item')
.setDescriptionLocalizations({
"en-US": "Get information about any item",
"pl": "Wyświetl informacje o wybranym przedmiocie"
})
.addStringOption(option =>
option.setName('item_name')
.setDescription('Name of the item you want to check info about')
.setDescriptionLocalizations({
"en-US": "Name of the item you want to check info about",
"pl": "Podaj nazwę przedmiotu"
})
.addChoices(
...choicesArray1
)
.setRequired(true));

const execute = async (interaction) => {

}

module.exports = {
data,
execute
};
const { SlashCommandBuilder, EmbedBuilder, AttachmentBuilder } = require('discord.js');
const { MongoClient } = require("mongodb");

const CLIENT = new MongoClient(process.env.MONGO_URI);
const DB = CLIENT.db("resources");
const COLLECTION = DB.collection("item_resources");

let choicesArray = [];
let choicesArray1 = [];

async function fetchChoices() {
try {
await CLIENT.connect();

// Find documents with 'id' field equal to 'emfReader' and project only the 'id' field
const id = COLLECTION.find({}, { projection: { id: 1, name: 1, _id: 0 } });

// Convert the cursor to an array and extract 'id' values
const documentsArray = await id.toArray();
choicesArray = documentsArray.map(document => ({ name: document.name, value: document.id }));

} catch (err) {
console.error(err);
}
}

fetchChoices().catch(console.dir);

fetchChoices().then(() => {
choicesArray1 = choicesArray;

}).catch(console.dir);

const data = new SlashCommandBuilder()
.setName('item1')
.setNameLocalizations({
"en-US": "item1",
"pl": "przedmiot1"
})
.setDescription('Get information about any item')
.setDescriptionLocalizations({
"en-US": "Get information about any item",
"pl": "Wyświetl informacje o wybranym przedmiocie"
})
.addStringOption(option =>
option.setName('item_name')
.setDescription('Name of the item you want to check info about')
.setDescriptionLocalizations({
"en-US": "Name of the item you want to check info about",
"pl": "Podaj nazwę przedmiotu"
})
.addChoices(
...choicesArray1
)
.setRequired(true));

const execute = async (interaction) => {

}

module.exports = {
data,
execute
};
8 replies
DIAdiscord.js - Imagine an app
Created by Not Kuma anymore on 12/17/2023 in #djs-questions
.addChoices() as locale
Is there an option to set choices based on user language like .setNameLocalizations & .setDescriptionLocalizations?
6 replies
DIAdiscord.js - Imagine an app
Created by Not Kuma anymore on 12/14/2023 in #djs-questions
Trying to set `/language` command that completely changes language of a bot but app is not respondin
const { SlashCommandBuilder, PermissionFlagsBits } = require('discord.js');
const fs = require('fs');
const restartBot = require('../../utils/restart-bot');
const { language } = require('../../config.json'); // Fetch language key from config.json
const languageSetting = require(`../../resources/languages/${language}.json`) ; // Fetch language based text responses

module.exports = {
data: new SlashCommandBuilder()
.setName(`${languageSetting.language_name}`)
.setDescription(`${languageSetting.language_desc}`)
.addStringOption(option =>
option.setName('choose_language')
.setDescription(`${languageSetting.language_optdesc}`)
.setRequired(true)
.addChoices(
{ name: 'English', value: 'eng' },
{ name: 'Polski', value: 'pl' }
)
),
category: 'admin',
async execute(interaction) {

const interactionUserPermissions = interaction.member.permissions;

if(interactionUserPermissions.has(PermissionFlagsBits.Administrator) || interactionUserPermissions.has(PermissionFlagsBits.ManageGuild) || interaction.member.id == process.env.BOT_DEVELOPER){
// Get the chosen language from the interaction
const chosenLanguage = interaction.options.getString('choose_language');

// Read the current configuration from the file
const configPath = './config.json';
const rawConfig = fs.readFileSync(configPath);
const config = JSON.parse(rawConfig);

// Update the language key with the chosen language
config.language = chosenLanguage;

// Write the updated configuration back to the file
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));

if(chosenLanguage == "pl"){}
// Respond to the user indicating that the language has been changed
await interaction.reply(`${languageSetting.language_success} ${chosenLanguage}`);
restartBot.restartBot();
} else{
interaction.reply({content: `${languageSetting.error_permissionsNeeded}`, ephemeral: true})
}
},
};
const { SlashCommandBuilder, PermissionFlagsBits } = require('discord.js');
const fs = require('fs');
const restartBot = require('../../utils/restart-bot');
const { language } = require('../../config.json'); // Fetch language key from config.json
const languageSetting = require(`../../resources/languages/${language}.json`) ; // Fetch language based text responses

module.exports = {
data: new SlashCommandBuilder()
.setName(`${languageSetting.language_name}`)
.setDescription(`${languageSetting.language_desc}`)
.addStringOption(option =>
option.setName('choose_language')
.setDescription(`${languageSetting.language_optdesc}`)
.setRequired(true)
.addChoices(
{ name: 'English', value: 'eng' },
{ name: 'Polski', value: 'pl' }
)
),
category: 'admin',
async execute(interaction) {

const interactionUserPermissions = interaction.member.permissions;

if(interactionUserPermissions.has(PermissionFlagsBits.Administrator) || interactionUserPermissions.has(PermissionFlagsBits.ManageGuild) || interaction.member.id == process.env.BOT_DEVELOPER){
// Get the chosen language from the interaction
const chosenLanguage = interaction.options.getString('choose_language');

// Read the current configuration from the file
const configPath = './config.json';
const rawConfig = fs.readFileSync(configPath);
const config = JSON.parse(rawConfig);

// Update the language key with the chosen language
config.language = chosenLanguage;

// Write the updated configuration back to the file
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));

if(chosenLanguage == "pl"){}
// Respond to the user indicating that the language has been changed
await interaction.reply(`${languageSetting.language_success} ${chosenLanguage}`);
restartBot.restartBot();
} else{
interaction.reply({content: `${languageSetting.error_permissionsNeeded}`, ephemeral: true})
}
},
};
5 replies
DIAdiscord.js - Imagine an app
Created by Not Kuma anymore on 8/10/2023 in #djs-questions
Missing permissions while trying to ban someone
throw new DiscordAPIError(data, "code" in data ? data.code : data.error, status, method, url, requestData);
^

DiscordAPIError[50013]: Missing Permissions
at handleErrors (C:\Users\lukas\Desktop\bot\node_modules\@discordjs\rest\dist\index.js:640:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async SequentialHandler.runRequest (C:\Users\lukas\Desktop\bot\node_modules\@discordjs\rest\dist\index.js:1021:23)
at async SequentialHandler.queueRequest (C:\Users\lukas\Desktop\bot\node_modules\@discordjs\rest\dist\index.js:862:14)
at async REST.request (C:\Users\lukas\Desktop\bot\node_modules\@discordjs\rest\dist\index.js:1387:22)
at async GuildBanManager.create (C:\Users\lukas\Desktop\bot\node_modules\discord.js\src\managers\GuildBanManager.js:169:5) {
requestBody: { files: undefined, json: { delete_message_seconds: undefined } },
rawError: { message: 'Missing Permissions', code: 50013 },
code: 50013,
status: 403,
method: 'PUT',
url: 'https://discord.com/api/v10/guilds/1138944612321480834/bans/454341168344596483'
}
throw new DiscordAPIError(data, "code" in data ? data.code : data.error, status, method, url, requestData);
^

DiscordAPIError[50013]: Missing Permissions
at handleErrors (C:\Users\lukas\Desktop\bot\node_modules\@discordjs\rest\dist\index.js:640:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async SequentialHandler.runRequest (C:\Users\lukas\Desktop\bot\node_modules\@discordjs\rest\dist\index.js:1021:23)
at async SequentialHandler.queueRequest (C:\Users\lukas\Desktop\bot\node_modules\@discordjs\rest\dist\index.js:862:14)
at async REST.request (C:\Users\lukas\Desktop\bot\node_modules\@discordjs\rest\dist\index.js:1387:22)
at async GuildBanManager.create (C:\Users\lukas\Desktop\bot\node_modules\discord.js\src\managers\GuildBanManager.js:169:5) {
requestBody: { files: undefined, json: { delete_message_seconds: undefined } },
rawError: { message: 'Missing Permissions', code: 50013 },
code: 50013,
status: 403,
method: 'PUT',
url: 'https://discord.com/api/v10/guilds/1138944612321480834/bans/454341168344596483'
}
when executing
member.ban({ reason: 'Sorry, there is no place for mean people.' }).then(banned => console.log(`Użytkownik o nicku ${banned.name} został/a zbanowany/a`));
member.ban({ reason: 'Sorry, there is no place for mean people.' }).then(banned => console.log(`Użytkownik o nicku ${banned.name} został/a zbanowany/a`));
33 replies
DIAdiscord.js - Imagine an app
Created by Not Kuma anymore on 1/15/2023 in #djs-questions
TypeError: Cannot read properties of undefined (reading 'user')
22 replies