Send is undefined - node.js

client.on('guildMemberAdd', member => {
member.roles.add(member.guild.roles.cache.find(i => i.name === 'User'))
const welcomeEmbed = new Discord.MessageEmbed()
welcomeEmbed.setColor('#5cf000')
welcomeEmbed.setTitle('**' + member.user.username + '** Has joined! **' )
member.guild.channels.cache.find(i => i.name === 'welcome').send(welcomeEmbed)
})
Send is undefined im not to sure what to do.

Your bug is in member.guild.channels.cache.find(i => i.name === 'welcome'), There are no channels named 'welcome'.
You may want to add a check:
const channel = member.guild.channels.cache.find(i => i.name === 'welcome');
if (channel) {
channel.send(welcomeEmbed);
}

Related

tag a user using discord node js

I want to make a command that tags a user when there name got mentoined. But now the bot only tags the username and not a nickname the person has in the server
client.on("message", message => {
if(message.content === 'test'){
message.channel.send("works")
console.log("logged the message")
}
})
client.on("message", message => {
const list = message.guild;
list.members.cache.each(member => {
pinging = member.user.id
console.log(member.user.username)
if(message.content.includes(member.user.username)){
message.channel.send(`<#${pinging}>`)
}
})
})
can anyone help?
First thing, you only want to have one listener for the message event, so try this code:
client.on("message", message => {
if (message.content === 'test'){
message.channel.send("works")
console.log("logged the message")
}
// insert choices from below here
})
If you want it to listen for a member being tagged
const mentionedMember = message.mentions.members.first()
if (mentionedMember){
message.channel.send(`${mentionedMember}`)
}
Now if you want it to listen for username instead of #username
const args = message.content.slice().trim().split(' ')
const guild = message.guild
args.forEach(arg => {
const member = guild.members.cache.find(member => member.user.username.toLowerCase() === arg.toLowerCase())
if (member) {
message.channel.send(`${member}`)
} else {
return
}
})
To listen for displayName
const args = message.content.slice().trim().split(' ')
const guild = message.guild
args.forEach(arg => {
const member = guild.members.cache.find(member => member.displayName.toLowerCase() === arg.toLowerCase())
if (member) {
message.channel.send(`${member}`)
} else {
return
}
})

Parsing error: Unexpected token Client Discord.js

Ok so i have this problem, i am new at node.js, please help, the error says: "Parsing error: Unexpected token Client" i've tried to do something for a while and i don't know what to do.
const {Client, Intents} = require('discord.js')
const client = new Client({intents: ["GUILD_MESSAGES"]})
client.login("TOKEN")
client.on('ready', function () {
}
Client.on("message", message => { try{
let server1 = "861068256844316683";
let server2 = "936852675352481842";
let channel1 = "861068256844316686";
let channel2 = "936852675352481845";
let emojis = [":joy:",":rofl:",":heart_eyes:",":smiling_face_with_3_hearts:",":sunglasses:",":nerd:",":face_with_monocle:",":kissing_heart:",":pensive:",":rage:",":face_with_symbols_over_mouth:",":hot_face:",":cold_face:",":thinking:",":flushed:",":scream:",":yawning_face:",":lying_face:",":heart_eyes_cat:",":joy_cat:",":scream_cat:"];
let msj = message.content;
if (msj.includes("#everyone")) return;
if (msj.includes("#here")) return;
if (msj.includes("<#&")) return;
if (msj.includes("http")) return;
if(message.channel.id === channel1){
let emoji = emojis[Math.floor(Math.random() * 21)];
if(message.member.id === "947818875137953863") return;
let chatGlobal = Client.guilds.cache.find(g => g.id === server2).channels.cache.find(c => c.id === channel2);
chatGlobal.send(`**${emoji} ${message.member.user.username}:**\n> ${msj.replace("\n","\n> ")}`);
}
if(message.channel.id === channel2){
let emoji = emojis[Math.floor(Math.random() * 21)];
if(message.member.id === "947818875137953863") return;
let chatGlobal = Client.guilds.cache.find(g => g.id === server1).channels.cache.find(c => c.id === channel1);
chatGlobal.send(`**${emoji} ${message.member.user.username}:**\n> ${msj.replace("\n","\n> ")}`);
}
} catch(error){
console.log(error)
}})
The error is coming because you missed a parentheses when you are calling client.on('ready'). Also, as #omar al-hamad told, you are confusing the client (with a small c) with Client which is completely different. Your fixed code might look something like this:
const { Client } = require("discord.js");
const client = new Client({ intents: ["GUILD_MESSAGES"] });
let server1 = "861068256844316683";
let server2 = "936852675352481842";
let channel1 = "861068256844316686";
let channel2 = "936852675352481845";
let emojis = [
":joy:",
":rofl:",
":heart_eyes:",
":smiling_face_with_3_hearts:",
":sunglasses:",
":nerd:",
":face_with_monocle:",
":kissing_heart:",
":pensive:",
":rage:",
":face_with_symbols_over_mouth:",
":hot_face:",
":cold_face:",
":thinking:",
":flushed:",
":scream:",
":yawning_face:",
":lying_face:",
":heart_eyes_cat:",
":joy_cat:",
":scream_cat:",
];
client.on("ready", function () {
console.log(`Logged in as ${client.user.tag}!`)
});
client.on("message", (message) => {
try {
if (message.author.bot) return
let msj = message.content;
if (msj.includes("#everyone")) return;
if (msj.includes("#here")) return;
if (msj.includes("<#&")) return;
if (msj.includes("http")) return;
if (message.channel.id === channel1) {
let emoji = emojis[Math.floor(Math.random() * 21)];
if (message.member.id === "947818875137953863") return;
let chatGlobal = Client.guilds.cache
.find((g) => g.id === server2)
.channels.cache.find((c) => c.id === channel2);
chatGlobal.send(
`**${emoji} ${message.member.user.username}:**\n> ${msj.replace(
"\n",
"\n> "
)}`
);
}
if (message.channel.id === channel2) {
let emoji = emojis[Math.floor(Math.random() * 21)];
if (message.member.id === "947818875137953863") return;
let chatGlobal = Client.guilds.cache
.find((g) => g.id === server1)
.channels.cache.find((c) => c.id === channel1);
chatGlobal.send(
`**${emoji} ${message.member.user.username}:**\n> ${msj.replace(
"\n",
"\n> "
)}`
);
}
} catch (error) {
console.log(error);
}
});
client.login("TOKEN");
(Note: I took the liberty to format the code and put some variables at the top so it can be accessed everywhere.)
you declared client with small letters like this:
const client = new Client({intents: ["GUILD_MESSAGES"]})
---------^
and then you are trying to call with with a capital C like this:
Client.on
instead do:
client.on

Cannot read the property 'inviter' of undefined

I am getting a Cannot read the property 'inviter' of undefined error, and I'm not quite sure why. Here is my code:
Client.on('guildMemberAdd', async (member) => {
const cachedInvites = GuildInvites.get(member.guild.id);
const nweInvites = await member.guild.fetchInvites();
GuildInvites.set(member.guild.id, nweInvites);
try {
const usedInvites = nweInvites.find(
(inv) => cachedInvites.get(inv.code) < inv.uses
);
const embed = new Discord.MessageEmbed()
.setDescription(
`${member.user.tag} is the ${member.guild.memberCount}th member. \nInvited by ${usedInvites.inviter.username} \nNumber of uses: ${usedInvites.uses}`
)
.setTimestamp()
.setColor('YELLOW');
const WelcomeChannel = member.guild.channels.cache.find(
(channel) => channel.id === '704908658068422698'
);
WelcomeChannel.send(embed).catch((err) => {
console.log(err);
});
} catch (err) {
console.log(err);
}
});
Client.login();
I'm trying to create an invite manager bot. Any help would be appreciated, thanks!
What you can do is manage that situation by protecting your code.
try {
const usedInvites = nweInvites.find(
(inv) => cachedInvites.get(inv.code) < inv.uses
);
if (!usedInvites) {
return;
}
const embed = new Discord.MessageEmbed()
.setDescription(
`${member.user.tag} is the ${member.guild.memberCount}th member. \nInvited by ${usedInvites.inviter.username} \nNumber of uses: ${usedInvites.uses}`
)
.setTimestamp()
.setColor('YELLOW');
const WelcomeChannel = member.guild.channels.cache.find(
(channel) => channel.id === '704908658068422698'
);
WelcomeChannel.send(embed).catch((err) => {
console.log(err);
});
}
I added: if (!usedInvites) return; but you can do whatever action you need.

Discord.j - Function strangely empty

this question is linked to another one. If you need to know why I'm asking this, check this question.
I'm developing a bot who should get a collection filled with members who have a specific role. But after testing and double checking role name and be sure I'm testing the bot in a server and not in DM the collection is always empty and unusable (the program couldn't run without it).
const eventMembers = message.guild.members.cache.filter(m =>
m.roles.cache.some(r => r.name === "event")
);
const connectedMembers = eventMembers.members.filter(m => {
return voiceChannel.members.has(m.id)
});
console.log(connectedMembers);
If anybody has a hint or a solution I take it
You forgot return :)
const eventMembers = message.guild.members.cache.filter(m => {
return m.roles.cache.some(r => r.name === "event")
});
or
const eventMembers = message.guild.members.cache.filter(m => m.roles.cache.some(r => r.name === "event"));
You can check for the role with this name exist:
let role = message.guild.roles.cache.find(role => role.name === 'event')
if (role) {
console.log('ok')
} else {
console.log('No role found with this nickname')
}
V2
const eventMembers = message.guild.members.cache.filter(m => {
return m.roles.cache.some(r => r.name === "event") && m.voice && m.voice.channelID === message.member.voice.channelID
});
or
const eventMembers = message.guild.members.cache.filter(m => m.roles.cache.some(r => r.name === "event") && m.voice && m.voice.channelID === message.member.voice.channelID)
V3 :D
bot.on('message', message => {
if(message.content === '!test') {
if(!message.member.voice.channel) return message.reply('You need joinVoiceChannel for use this command');
let targetRole = message.guild.roles.cache.find(role => role.name === 'event')
if (!targetRole) return message.reply('Can`t find a role');
let eventMembersNotInVoice = targetRole.members.filter(member => member.voice.channelID !== message.member.voice.channelID)
console.log(eventMembersNotInVoice.size)
}
});

How can I promise so that way I can use sentMessage in this area. DISCORD.JS

How can I replace [the message the bot sent] with sentMessage?
client.guilds.get("588125693225992195")
.channels
.find(ch => ch.name === 'order-requests')
.send(richemb)
.then(sentMessage => sentMessage.react('🗑'))
.catch(() => console.error('Failed to react.'))
const filter = (reaction) => reaction.emoji.name === '🗑'
message.awaitReactions(filter)
.then([themessagethebotsent].delete(0500))
.catch(console.error);```
The scope of sentMessage is within the then() callback. This means that it can't be accessed outside of that callback.
You have two main solutions. Keeping your current setup, you could place the code that needs sentMessage inside the callback. Or, you could use the keyword await for a better flow. Note that it can only be used in async functions.
Example 1:
const guild = client.guilds.get('588125693225992195');
const channel = guild.channels.find(ch => ch.name == 'order-requests');
channel.send(richemb)
.then(sentMessage => {
sentMessage.react('🗑');
message.awaitReactions(reaction => reaction.emoji.name === '🗑')
.then(() => sentMessage.delete(0500));
})
.catch(console.error);
Example 2:
const guild = client.guilds.get('588125693225992195');
const channel = guild.channels.find(ch => ch.name === 'order-requests');
try {
const sentMessage = await channel.send(richemb);
await sentMessage.react('🗑');
await sentMessage.awaitReactions(reaction => reaction.emoji.name === '🗑');
await sentMessage.delete(0500);
} catch(err) {
console.error(err);
}

Resources