How to resolve client.guilds.cache.get().commands cannot read properties of undefined? - node.js

Before discordjs update my code works well but now with the update this part doesn't work.
module.exports = {
name : 'ready',
once : true,
async execute(client){
const devGuild = await client.guilds.cache.get('243406719638568960')
devGuild.commands.set(client.commands.map(cmd => cmd))
}
}
devGuild is undefined. How can I resolve the error to set my commands:
UNCAUGHT_EXCEPTION : TypeError: Cannot read properties of undefined (reading 'commands'), Origine : uncaughtException

Related

Why is Fetch Undefined?

I am copying a code DIRECTLY from discord.js guide to send a direct message to myself (and only myself) through my bot.
module.exports = {
callback: async(client) => {
const user = await client.users.fetch('503965897203908609');
user.send('content');
}
}
here's the relevant code. and when I run the command, I get
TypeError: Cannot read properties of undefined (reading 'fetch')
WHY
Because your client.users is undefined, if we try to access a property on undefined it will throw an error.
as to why client.users is undefined, that would be something which requires more code to look at

TypeError: Cannot read property 'facts' of undefined

I am trying to make a word randomizer, and it gives me this error:
message.channel.send(item.facts);
^
TypeError: Cannot read property 'facts' of undefined
and I don't know how to fix it.
Here is my code:
const Discord = require('discord.js');
module.exports = {
name: 'game',
execute(message, args) {
const gameChanger = require('../text.txt')
const item = gameChanger[Math.floor(Math.random() * gameChanger.length)];
setInterval(() => {
const item = gameChanger[Math.floor(Math.random() * gameChanger.length)];
message.channel.send(item.facts);
}, 5 * 1000);
}
}
Someone please help me
The error indicates that the variable item is undefined. This is probably because you are going out of bounds of the gameChanger array, which is causing undefined to be assigned to item.

TypeError: Cannot read property 'then' of null | discord.js

I am trying to make a message tracker and this error shows and I don't know why
Code: messagecounter.js
const db = require('quick.db');
module.exports = {
name: "msgc",
description: "Message Counter",
async run(client, message, args) {
// checking who wants to fetch it
let member = message.mentions.members.first() || message.member; // this checks if they mentioned a members
db.fetch(`messageSent_${member.id}`).then(obj => {
message.channel.send(`**Messages Sent:** \`${obj.value}\``);
});
}
}
Code: bot.js:70:42
client.commands.get(command).run(client, message, args);
Error:
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'then' of null
at Object.run (C:\Users\Familia\OneDrive\Documents\Other Stuff\Visual Studio code\blade\commands\messagecounter.js:13:45)
at Client.<anonymous> (C:\Users\Familia\OneDrive\Documents\Other Stuff\Visual Studio code\blade\bot.js:70:42)
Any help would be appreciated
Using discord.js v12
After quickly glancing over "quick.db" I couldn't find a method called fetch defined on the db object. "get", however is defined and is perhaps what you meant to use.

TypeError: Cannot read property 'EventEmitter' of undefined typescript nodejs

I have a typescript application running on node.
I am using 'EventEmitter' class to emit a change in variable value.
This is my piece of code,
import events from 'events';
public async updateStream(streamContext: string, state: boolean): Promise<string> {
const eventEmitter = new events.EventEmitter();
if (state === true) {
return StreamManagement.instance.activeStreams.get(streamContext).streamState = 'Paused';
} else {
const streamState = StreamManagement.instance.activeStreams.get(streamContext).streamState = 'Active';
eventEmitter.emit('resume');
return streamState;
}
}
public async waitForStreamActive(stream: Stream) {
const eventEmitter = new events.EventEmitter();
// tslint:disable-next-line:no-unused-expression
return new Promise(( resolve ) => {
eventEmitter.on('resume', resolve );
});
}
This piece of code builds fine. But when i run the code, as in execute the operation, I am getting the following error,
error: errorHandler - Apply - Hit Unhandled exception {"timestamp":"2019-04-29T12:33:49.209Z"}
error: errorHandler - Apply - Cannot read property 'EventEmitter' of undefined - TypeError: Cannot read property 'EventEmitter' of undefined
at StreamResource.updateStream (C:\Vertigo\core\reference_platform\dist\index.js:10695:51)
at StreamService.patchStream (C:\Vertigo\core\reference_platform\dist\index.js:22524:40)
at process._tickCallback (internal/process/next_tick.js:68:7) {"timestamp":"2019-04-29T12:33:49.215Z"}
What am I doing wrong?
I've set up minimal project to reproduce it and immediately ts compiler warns me about:
TS1192: Module '"events"' has no default export.
But this seems to work:
import * as EventEmitter from 'events'
new EventEmitter();

NodeJS - TypeError: Cannot read property 'name' of undefined

I am getting the following error from my code: If you could help me that would be amazing! I am using discord.js!
TypeError: Cannot read property 'name' of undefined at
files.forEach.file (/root/eternity-bot/eternity-bot/index.js:21:33) at
Array.forEach () at fs.readdir
(/root/eternity-bot/eternity-bot/index.js:18:9) at
FSReqWrap.oncomplete (fs.js:135:15)
fs.readdir("./commands/", (err, files) => {
if (err) return console.error(err);
files.forEach(file => {
if (!file.endsWith(".js")) return;
let props = require(`./commands/${file}`);
console.log(`Loading Command: ${props.help.name}.`);
bot.commands.set(props.help.name, props);
props.conf.aliases.forEach(alias => {
bot.aliases.set(alias, props.help.name);
})
});
});
TypeError: A TypeError is thrown when an operand or argument passed to a function is incompatible with the type expected by that operator or function.
The possible cause is your props is not loaded correctly and doesn't include any property help, thus accessing property name of unknown property help throws TypeError. Similar to following:
let obj = {
o1: {
a: 'abc'
}
};
obj.o1 // gives {a: 'abc'}, as o1 is property obj which is an object.
obj.o1.a // gives 'abc', as a is property of o1, which is property of obj.
obj.o2 // undefined, as there's no o2 property in obj.
obj.o2.a // TypeError as there's no o2 property of obj and thus accessing property a of undefined gives error.
What is happening is that the code is working perfectly fine, but there seems to be some problem with the exports of your javascript files in the commands folder. Most probably, the help property is not defined in your files.

Resources