How can I resolve these errors in my code? - node.js

bot.on('guildMemberAdd', (guildMember, channel, message) => {
guildMember.addRole(guildMember.guild.roles.find(role => role.name === "ew"));
embed = new discord.RichEmbed()
.setTitle("User Join Notification")
.setDescription("**guildMember.username** has joined this server.")
.setColor("#21a1e1")
message.channel.id('430681100956991511').sendEmbed(embed);
});
When I execute this code, when a user joins, it reports in the console TypeError: Cannot read property 'channel' of undefined. How could I resolve this?
Thanks.

The guildMemberAdd event only provides you with the GuildMember object so your channel and message variables are undefined.
Instead you want to get the channel to send the embed to through the GuildMember object:
guildMember.guild.channels.get("430681100956991511")
I believe

Related

Discord.js V13 sending a message to a specific channel of a specific guild

I'm failing to achieve something very simple. I can't send a message to a specific channel of a specific guild. I've browsed through the documentation and similar threads on stack overflow.
const guild = client.guilds.cache.find(g => g.id === process.env.guildId)
const channel = guild.channels.cache.find(ch => ch.name === "log-channel")
channel.send('👍')
Does not work. Instead, I'm getting this error:
const channel = guild.channels.cache.find(ch => ch.name === "log-channel")
^
TypeError: Cannot read properties of undefined (reading 'channels')
What am I missing?
I actually figured out what the problem was. I didn't set the GUILDS intent (thanks to Jakye in the comments).

How edit messages in Discord.js V12 using message ID

There is the way to edit a message on DiscordJS
To do this, we just need to pass it to the function, the client variable.
exports.myNewFunction = async function myNewTestFunction(client)
First, in order to edit a message using the Discord.JS API, we will need to find the server's GuildID.
const guild = client.guilds.cache.get(`Your Guild ID`);
Now, we're going to need to find the channel the message is on. For this, we will use the channel ID
const channel = guild.channels.cache.find(c => c.id === `Your message Channel` && c.type === 'text');
Finally, let's go to the editing part of the message. For this, we will only need to provide the ID of the message that you want to be edited.
channel.messages.fetch(`Your Message ID`).then(message => {
message.edit("New message Text");
}).catch(err => {
console.error(err);
});

How would one create a guild and then give myself an invite link

so I am currently in the process of trying to have a bot create a new guild/discord server and then console.log() an invite link for that so that I can join the guild and set it up to then invite friends. However, I am running into some slight problems as I am unsure as to how to go about this. My thought process is the following:
run : function(msg, client, cmds, disc, args){
const guild = client.guild.create("New Guild");
let invite //(and here get a new guild invite code )
console.log(invite);
}
My first problem is that I am not even sure if the guild creation is valid and how to get a guild invite from a guild object.
I have semi-completed what I wished to do. However, this is not the final thing because there is an error messaged:
DiscordAPIError: Unknown Channel
at RequestHandler.execute (C:\Users\jonas\node_modules\discord.js\src\rest\RequestHandler.js:154:13)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async RequestHandler.push (C:\Users\jonas\node_modules\discord.js\src\rest\RequestHandler.js:39:14) {
method: 'post',
path: '/channels/801898492082782310/invites',
code: 10003,
httpStatus: 404
I am unsure what the reason for the error in getting the correct channel but I'll reply to this with the full code when I have solved the problem.
run : function(msg, client, cmds, disc, args){
//console.log("1");
client.guilds.create("New Guild")
.then(value => {
let channel = value.channels.cache.first();
let invite = channel.createInvite({
maxAge: 0, // 0 = infinite expiration
maxUses: 0 // 0 = infinite uses
})
.then(result => {
console.log(result);
})
.catch(console.error);
console.log(invite.url);
})
.catch(console.error);
}
The way I fixed it was that previously the object that it fetched was a category channel object meanwhile I needed a text channel/voice channel to create an invite. The solution I used was the following:
run : function(msg, client, cmds, disc, args){
client.guilds.create("New Guild")
.then(value => {
value.channels.create('new-general', { reason: 'Needed a cool new channel' })
.then(channel => {
let invite = channel.createInvite()
.then(invite => console.log(invite))
.catch(console.error);
console.log(invite.url);
})
.catch(console.error);
})
.catch(console.error);
}
To explain the fix, I had to create a channel in the server to invite someone so before I created an invite I created a new channel and used the newly created channel as the channel for the invite.

Cannot add a role, Discord.js

so i'm coding a discord bot, and there's a command, this command know when the user changes is custom status, and if the special status is "hello" the bot will send a message in a specific channel, and add a role to the user. But the code gives my an error
Here's the code :
bot.on('presenceUpdate', (oldMember, newMember) => {
console.log(newMember.activities[0].state)
let guildChannels = newMember.guild.channels;
var Cha = guildChannels.cache.find(channel => channel.name === "general")
if(newMember.activities[0].state === "hello") {
Cha.send('hello !')
.then(msg => {
let GradeUser = bot.users.cache.find(user => user.id == newMember.userID);
let role = msg.guild.roles.cache.find(r => r.id == "742423876004216883");
GradeUser.roles.add('742423876004216883')
})
.catch(console.error)
}
});
And here's the error :
TypeError: Cannot read property 'add' of undefined
at C:\Users\Nathan\Desktop\Dev\Effectivement Bot\index.js:320:29
at processTicksAndRejections (internal/process/task_queues.js:93:5)
I just want to solve the error with the role ( add the role )
You need to use a GuildMember object, not a user object.
From discord.js guide
What is the difference between a User and a GuildMember?
A lot of users get confused as to what the difference between Users and GuildMembers is. The simple answer is that a User represents a global Discord user and a GuildMember represents a Discord user on a specific server. That means only GuildMembers can have permissions, roles, and nicknames, for example, because all of these things are server-bound information that could be different on each server that user is in.
Replace bot.users.cache.find() with msg.guild.members.cache.find()

Sending message to specific channel, not working

Having trouble sending a bot message on a specific channel. I very simply want to have the bot send a message to #general when it activates, to let everyone know it's working again.
In the bot.on function, I've tried the classic client.channels.get().send(), but the error messages I'm getting show that it thinks client is undefined. (It says "cannot read property 'channels' of undefined")
bot.on('ready', client => {
console.log("MACsim online")
client.channels.get('#general').send("#here");
})
The bot crashes immediately, saying: Cannot read property 'channels' of undefined
The ready event doesn't pass any client parameter.
To get a channel by name use collection.find():
client.channels.find(channel => channel.name == "channel name here");
To get a channel by ID you can simply do collection.get("ID") because the channels are mapped by their IDs.
client.channels.get("channel_id");
Here is an example I made for you:
const Discord = require("discord.js");
const Client = new Discord.Client();
Client.on("ready", () => {
let Channel = Client.channels.find(channel => channel.name == "my_channel");
Channel.send("The bot is ready!");
});
Client.login("TOKEN");
You need to get the guild / server by ID, and then the channel by ID, so your code will be something like :
const discordjs = require("discord.js");
const client = new discordjs.Client();
bot.on('ready', client => {
console.log("MACsim online")
client.guilds.get("1836402401348").channels.get('199993777289').send("#here");
})
Edit : added client call
The ready event does not pass a parameter. To gain the bot's client, simply use the variable that you assigned when you created the client.
From your code, it looks like the bot variable, where you did const bot = new Discord.Client();.
From there, using the bot (which is the bot's client), you can access the channels property!

Resources