I'm trying to create a discord bot with a slash command. I already verified that the command does exist, by running console.log(client.stores.get('commands'));
. However, when I tried converting the data to JSON:
try {
console.log(client.stores.get('commands'));
const commands = client.stores.get('commands').map(command => command.data.toJSON());
console.log(`Registering ${commands.length} commands...`);
await rest.put(Routes.applicationCommands(client.user.id), {
body: commands,
});
console.log('Commands registered successfully.');
} catch (error) {
console.error('Error during command registration:', error);
}
The terminal logged this message:
Error during command registration: TypeError: Cannot read properties of undefined (reading 'toJSON')
Does anyone know where I went wrong? ChatGPT gave me the same line of code every time I asked it how to fix the issue.
For reference, this is what was outputted when I tried running console.log(client.stores.get('commands'));
CommandStore(1) [Map] {
'biodata' => BiodataCommand {
store: [Circular *1],
location: PieceLocation {
full: 'src/commands/biodata.js',
root: 'src/commands'
},
name: 'biodata',
enabled: true,
options: {
name: 'biodata',
enabled: true,
description: 'Send biodata form message'
},
aliases: [],
applicationCommandRegistry: ApplicationCommandRegistry {
chatInputCommands: Set(0) {},
contextMenuCommands: Set(0) {},
guildIdsToFetch: Set(0) {},
globalCommandId: null,
globalChatInputCommandIds: Set(0) {},
globalContextMenuCommandIds: Set(0) {},
guildCommandIds: Collection(0) [Map] {},
guildIdToChatInputCommandIds: Collection(0) [Map] {},
guildIdToContextMenuCommandIds: Collection(0) [Map] {},
apiCalls: [],
commandName: 'biodata'
},
rawName: 'biodata',
description: 'Send biodata form message',
detailedDescription: '',
strategy: FlagUnorderedStrategy {
prefixes: [Array],
separators: [Array],
flags: [],
options: [],
matchFlag: [Function: never],
matchOption: [Function: never]
},
fullCategory: [],
typing: true,
lexer: Lexer { quotes: [Array], separator: ' ' },
preconditions: PreconditionContainerArray {
entries: [],
runCondition: 0,
mode: 0
}
},
Constructor: [class Command extends AliasPiece],
name: 'commands',
paths: Set(1) { 'src/commands' },
strategy: LoaderStrategy {
clientUsesESModules: true,
supportedExtensions: [ '.js', '.cjs', '.mjs' ],
filterDtsFiles: false
},
aliases: Collection(0) [Map] {},
Symbol(@sapphire/pieces:ManuallyRegisteredPieces): Map(0) {}
}
The error tells you that command.data
at least for some of the commands. Looking at the object that you have shown we see that commands are a Map
and a Map
has keys and values. None of its items have a data
.
Here's how you can build an array of map values:
let map = new Map();
map.set("biodata", [1, 2, 3]);
map.set("somethingelse", {a: 1, b: 2});
let result = [];
for (let item of map) {
result.push(item[1]);
};
console.log(result);
I get the element with the index of 1 for each item
to get the value, the 0th element is the key.
You can of course do whatever you wish in this loop, you can convert it to JSON if it was a String, but you will not need to if it's already an object or array.