Discord js Cannot read properties of undefined (reading 'add') - discord

Hey I have a little problem.
I wan't to assign a discord role when the bot starts up
I wan't it to work with id's like user id and role id but it is saying
Cannot read properties of undefined (reading 'add')
Thanks in advance Neo
My intents are enabled on discord developer portal
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS],
}); // intents stuff
enter image description here

Guild should not be a variable as it is going to change. Roles need to be fetched from the cache. And then once you have defined guild you can use it without defining it again. Try the code below and you should be good.
client.on('ready' async () => {
const guild = client.guilds.cache.get('976862525801000960')
const role = guild.roles.cache.get('978375133649592361')
const member = await guild.members.cache.get('404645579705024523')
member.roles.add(role).then(
console.log(`TOKEN: `)
)

You don't need to use async/await for getting user from guild. Use get() instead of fetch() that's probably gonna solve your problem.

Thanks Gh0st Fixed it by doing this.
const guild = client.guilds.cache.get('976862525801000960')
const role = guild.roles.cache.get('978375133649592361')
const member = await guild.members.fetch('404645579705024523')
member.roles.add(role).then(
console.log(`TOKEN: `)
)

Related

How can a bot read embed messages sent by another bot and post a message if it contains a specific content?

I'm trying to code a Discord bot that would read embed messages sent by another bot and ping a specific role if the embed contains '(FREE)' in the title, as displayed here:
https://i.stack.imgur.com/kPsR1.png
Unfortunately, my code produces nothing. I don't run into any error, the bot simply doesn't post a message when conditions are matched to do so — yet it is online and has the permissions to post messages in the channel.
Any help would be really appreciated. I went through all the questions related to this topic on SO, but I couldn't find a working solution to my issue. I'd like to mention that I'm a beginner in JS.
Thank you so much.
const Discord = require('discord.js');
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_WEBHOOKS"] })
client.on('ready', () => {
console.log(`Logged in...`);
});
client.on('messageCreate', (message) => {
if (message.embeds) {
const embed = message.embeds[0]
if (embed.title === '(FREE)') {
return message.channel.send('#Free mint')
}
}
})
Edit: I believe my question is different from the one suggested in the comment as I'm looking for specific content in the embed.
I managed to make the bot work with the following code, thanks #Gavin for suggesting it:
const Discord = require('discord.js');
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_WEBHOOKS"] })
client.on('ready', () => {
console.log(`Logged in...`);
});
client.on('messageCreate', message => {
for (let embed of message.embeds) {
if (embed.title.includes('FREE')) {
return message.channel.send("<#&" + roleId + ">")
}
}
});

Discord Auto role when joining a server

I am using the v12 and this is what I have tried so far:
client.on('guildMemberAdd', (guildMember) => {
guildMember.roles.add(
guildMember.guild.roles.find((r) => r.name === 'Newbie')
);
});
Discord JS v12 introduces the concept of managers, you will no longer be able to directly use collection methods such as GuildMember.roles.find. You will now have to directly ask for cache on a manager before trying to use collection methods. (GuildMember.roles.cache.find);
client.on("guildMemberAdd", guildMember => { // Detecting when a GuildMember joins the guild.
const Role = guildMember.guild.roles.cache.find(role => role.name == "Newbie"); // Getting the Role by name.
if (!Role) return console.log("Invalid Role"); // Checking if the Role exists.
guildMember.roles.add(Role.id).catch(error => console.log(error)); // Adding the Role to the GuildMember.
});

Ways to login to multiple bot accounts with one script?

The ways I've tried.
Loop through tokens, this is fine but not much room to customise what bot does what. Example having each bot type in a specific channel, with this I've realised the bots type in the same location right after each other.
const auth = require('./tokens.json')
const Discord = require('discord.js')
for (const token of auth.Tokens) {
const client = new Discord.Client()
client.on('ready', () => {
console.log('I am ready !')
console.log(client.user.id)
})
client.login(token)
}
Also by creating multiple instances of a discord.client
const Discord = require('discord.js');
const client1 = new Discord.Client();
const client2 = new Discord.Client();
ect...
client1.once('ready',() => {
})
client1.on('message', async(message) => {
})
client2.once('ready',() => {
})
client2.on('message', async(message) => {
})
client1.login(CONFIG.Token1);
client2.login(CONFIG.Token2);
I am just wondering if there is other ways of doing this, lets say I have 5-6 bots and I do the 2nd method the code will get quite long depending on what i want to add into it.
I did think about adding a loop something like this.
for(var i = 0; i < token.length; i++)
And having a channel id linked to a specific number as the i++ is increasing it. So each bot would get its own number and channel id, but I'm not sure if that's even a thing that would work or if it would be good enough to use.
Any suggestions would be greatly appreciated and thank you for reading.
I think your second method:
creating multiple instances of a discord.client
Would be the best way to do it.
The easiest way to give the bots specific channels is to change their discord permissions and physically assigning them the channels.
Another way could be something like:
client.channelID = <ID of channel you want the bot in>
client2.channelID = <ID of channel you want the bot in>
// ... code
client.on('message', message => {
if (message channel.id !== client.channelID) return;
// ...
I was able to run multiple bots from the same script using this code:
const auth = ['TOKEN1', 'TOKEN2']
const Discord = require('discord.js')
for (const token of auth) {
const client = new Discord.Client()
client.on('ready', () => {
console.log('I am ready !')
console.log(client.user.id)
});
client.on('message', (msg) => {
if (msg.content === '!ping') {msg.reply('pong!')}
});
client.login(token);
}

Discord.js How can I make the bot send a message whenever it gets invited to a server?

I just want it to say some simple instructions or information in the chat whenever the bot joins a server.
Anyone know how I could do that?
client.on("guildCreate", guild => {
const channels = guild.channels.cache.filter(channel => channel.type == "text");
channels.first().send("Thank you for inviting me!").catch(e => console.log(e));
});

Discord.js send message if have a role

I want to send dm to anyone who has the role of "xxx" when the bot starts. Can you help me?
You can do it with this code:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
const list = client.guilds.get("guild ID");
list.members.forEach(member => {
if (member.roles.some(role => role.name === 'Your role')) {
member.send('Your message')
}
});
});
The way it works is when the bot is ready it will check each member if they have the specified role and if they do then the bot will send it a DM.
A tip, when asking questions like this provide more information on what the problem is, what you've tried and you're current code.

Resources