This question already has an answer here:
Bot won't messsage on join (djs)
(1 answer)
Closed 2 years ago.
I'm very confused why this isn't working. There are no errors in the console.
Its not sending messages when a user joins or leaves
member.roles.add(member.guild.roles.cache.find(i => i.name === 'Member'))
const welcomeEmbed = new Discord.MessageEmbed()
welcomeEmbed.setColor('#5cf000')
welcomeEmbed.setTitle('**' + member.user.username + '** is now Among Us other **' + member.guild.memberCount + '** people')
welcomeEmbed.setImage('https://cdn.mos.cms.futurecdn.net/93GAa4wm3z4HbenzLbxWeQ-650-80.jpg.webp')
member.guild.channels.cache.find(i => i.name === '〔🛬〕arrivals').send(welcomeEmbed)
})
client.on('guildMemberRemove', member => {
const goodbyeEmbed = new Discord.MessageEmbed()
goodbyeEmbed.setColor('#f00000')
goodbyeEmbed.setTitle('**' + member.user.username + '** was not the impostor there are **' + member.guild.memberCount + '** left Among Us')
goodbyeEmbed.setImage('https://gamewith-en.akamaized.net/article/thumbnail/rectangle/22183.png')
member.guild.channels.cache.find(i => i.name === '〔🛫〕departures').send(goodbyeEmbed)
})
//Welcome & goodbye messages end\\
});
client.login(config.token);```
The problem is the Discord API's relatively new intents feature. You need to subscribe to specific intents in order to reliably receive the affiliated events. guildMemberAdd and guildMemberRemove are on the list of events that may require subscription to an intent.
Here's one possible fix you'll need to implement wherever you are defining client:
const intents = ["GUILDS", "GUILD_MEMBERS"];
const client = new Discord.Client({intents: intents, ws:{intents: intents}});
You may also need to enable the below setting for your bot on its discord developers page, as I think guildMemberAdd and guildMemberRemove are parts of a privileged intent:
Relevant resources:
General info about intents
List of events requiring intents
A similar question that I previously answered
Related
I keep getting this error
C:\WINDOWS\Temp\OneDrive\Documents\microsoft\node_modules\discord.js\src\client\Client.js:544
throw new TypeError('CLIENT_MISSING_INTENTS');
^
TypeError [CLIENT_MISSING_INTENTS]: Valid intents must be provided for the Client
but when I do add it it says client is declared
const Discord = require('discord.js')
const client = new Discord.Client();
client.on('ready', () => {
console.log("Bot is online!")
});
const allIntents = new Intents(32767);
how do I fix this?
Since discord.js v13, you're required to explicitly pass intents when instantiating a new Client.
Intents are what tells Discord about which events your bot wants to receive, so it doesn't send too much unnecessary stuff that you're not going to use in your code.
To do this, you don't even have to do the numbers thing. The library supports passing intents as an array of strings (and then it does the math behind the scenes).
Here's an example if you wanted to receive some information about the guilds your bot is in:
const client = new Discord.Client({
intents: [
'GUILDS',
'GUILD_MEMBERS'
]
});
You can add as many intents as you want, and a complete list of the currently supported ones can be found in this discord.js documentation page. There's also this official documentation page by Discord that tells you which intent controls which events.
if(message.content == `${config.prefix}mods`) {
const ListEmbed = new Discord.MessageEmbed()
.setTitle('Mods:')
.setDescription(message.guild.roles.cache.get('813803673703809034').members.map(m=>m.user.tag).join('\n'));
message.channel.send(ListEmbed);
}
Hey so iam making a command which displays all the members with that role but it only seems to be sending 1 of the mods
await message.guild.roles.fetch();
let role = message.guild.roles.cache.find(role => role.id == args[0]);
if (!role) return message.channel.send('Role does not exist'); //if role cannot be found by entered ID
let roleMembers = role.members.map(m => m.user.tag).join('\n');
const ListEmbed = new Discord.MessageEmbed()
.setTitle(`Users with \`${role.name}\``)
.setDescription(roleMembers);
message.channel.send(ListEmbed);
};
Make sure your command is async, otherwise it will not run the await message.guild.roles.fetch(); - this fetches all roles in the server to make sure the command works reliably.
In Discord.jsV12 the get method was redacted and find was implemented.
Aswell as this, I would highly recommend defining variables to use in embeds and for searching since it is much more easy to error trap.
If you have many members with the role, you will encounter the maximum character limit for an embeds description. Simply split the arguments between multiple embeds.
I'm somewhat new to coding and I am making a bot in discord.js v12 and I was wondering how I could add verification for new users who join the server. Weather than be sending them a captcha they must complete or just adding a reaction role that they click and it gives them x role such as "member" or "verified." Thanks in advance :D
I have actually just recently created one of those using a very simple method of creating two questions and answers arrays and checking if the user's input matches the answer.
So, first off you'll want to create 2 arrays:
const questions = ['Do you like melon?', 'Do you want melon?']
const answers = ['Yes!', 'What do you want from me...']
Then you can get a randomized question that would match the answer:
var randomIndex = Math.floor(Math.random() * questions.length)
var randomQuestion = questions[randomIndex]
var randomAnswer = answers[randomIndex]
And finally, you can send the question and check if the answer matches:
message.channel.send(randomQuestion)
client.on('message', message => {
if (message.content === randomAnswer) {
let role = message.guild.roles.cache.find(r => r.name === "Member")
message.member.roles.add(role)
message.channel.send('Congratulations! you've completed the verification!');
} else {
message.channel.send('Verification failed. Incorrect answer.');
}
});
This question already has an answer here:
Testing if Discord invite link is invalid?
(1 answer)
Closed 2 years ago.
How do I make the bot check if the invite link posted on a channel (a lot of channels) is valid or not? I've searched around here, google, github and discord servers but I wasn't able to get an answer.. I'm coding the bot with Discord.js, and any help would be appreciated!
As suggested in the comments, you should use the invite endpoint. You can use your preferred method to create a GET request, but here's an example with node-fetch:
const fetch = require('node-fetch');
const code = 'this-is-invalid';
fetch(`https://discordapp.com/api/invite/${code}`)
.then((res) => res.json())
.then((json) => {
if (json.message === 'Unknown Invite') {
// the invite is invalid
} else {
// the invite is valid
}
});
Here's what the result object looks like
I'd like to auto set roles for new users when they join server X, problem is that this bot is on server Y as well, and server Y doesn't have the role.
client.on('guildMemberAdd', member => {
console.log('User ' + member.user.tag + ' has joined Steampunk.');
var role = member.guild.roles.find(x => x.name === "name");
member.addRole('247442955651121154');
})
I was hoping that I could do a simple check before applying the role, if user have joined server X, add the role, if else, do nothing. So far my attempts have failed.
Any input would be appreciated!
The original code is trying add a specific role you put in, and not the role you just looked up a line before that. There shouldn't be any issues with other servers the bot is in, because member.guild gives the guild of the member object, and since this member object is coming from a guildMemberAdd it means that member.guild will only return the guild that was just joined. The code in the edit has a lot of strange things going on, though.
The first issue I see is that in order to look through the channels in a guild, you have to use guild.channels.cache.find(), guild.channels.find() is old now and doesn't work in v12. Same goes for client.guild, guild.roles, etc. The part at the bottom of your edited code doesn't seem to make much sense at all. The code should get the roles of the newly joined guild (member.guild.roles.cache), look through them to find the role you want (you did this correctly with the .find()) and then apply the role to the member. Neither server nor role ever get used in your code, and guild.id is a property, not a method.
Let me know if you need help following these instructions, although it looks like you should be able to do it now. Always look at the discord.js docs first to check properties, methods, etc.
I ended up defining the guild in question by name, saved it as a const, and made a check for the specific guild.
If your bot is on several servers but you only want the greet/addRole on a specific one, all integrated into the message displayed on joining;
//Welcome message and autorole for specific server and channel.
client.on('guildMemberAdd', member => {
const = client.guilds.find(guild => guild.name === "Steampunk");
if (!guild) return;
const channel = member.guild.channels.find(ch => ch.name === "greetings");
if (!channel) return;
let botembed = new Discord.RichEmbed()
.setColor("#ff9900")
.setThumbnail(member.user.avatarURL)
.setDescription(`${member} Welcome to the Ark Steampunk Sponsored Mod Discord!\nPlease report all <#244843742568120321> and if u need any help view or ask your question in <#334492216292540417>. Have any ideas we got a section for that too <#244843792132341761>. Enjoy!:stuck_out_tongue: Appreciate all the support! <http://paypal.me/ispezz>`)
.setTimestamp()
.setFooter("Steampunk Bot", client.user.avatarURL);
channel.send(botembed);
console.log('User ' + member.user.tag + ' has joined ' + member.guild.name + '.');
var role = member.guild.roles.find(x => x.name === "name");
member.addRole('247442955651121154');
});