guildMemberAdd event not working even with intents enabled. (discord.js) - discord.js

I'm trying to set up a welcome message function on my discord bot, but no matter what I do, the bot doesn't seem to be able to use guildMemberAdd. I'm aware of the new update, and as suggested I've turned on both options under Private Giveaway Intents. But it still does not work. Here is my code:
client.on('guildMemberAdd', member => {
const emb = new MessageEmbed()
.setColor('#FFBCC9')
.setTitle("new member")
.setDescription("welcome to the server!")
member.guild.channels.get('780902470657376298').send(emb);
});
I have also found an answer online suggesting to use this:
const { Client, Intents } = require("discord.js");
const client = new Discord.Client({ ws: { intents: new Discord.Intents(Discord.Intents.ALL) }});
But no matter how I write that, my bot just won't even come online unless I use const client = new Discord.Client(); with nothing in the parentheses.

With v12 coming along, in order to get your full channels list you're only able to get the cached ones in your server. Hence why you should change this line:
member.guild.channels.get('780902470657376298').send(emb);
to:
member.guild.channels.cache.get('780902470657376298').send(emb);

You passed in the intents params wrong. Here is the correct way to do it.
const { Client, Intents } = require("discord.js");
const client = new Discord.Client({ ws: { intents: ['GUILDS', 'GUILD_MESSAGES', 'GUILD_MEMBERS', 'GUILD_PRESENCES'] } });
If you use this the guildMemberAdd event will emit.
Make sure you have intents turned on in the developer portal. As Shown in this image https://i.imgur.com/WfBLtXY.png.

Related

How can i fix this Discord.Js Client Intents initialization?

I want to send a private message to every Servermember.
If i use this intents its not working.
Also not working with Intents.ALL, Intents.Guild.ALL, ...
`const client = new Client({intents: [
Intents.all()
]});
client.guilds.cache.forEach(guild => {
guild.members.cache.forEach(member => {
member.send("hi").catch(console.error);
});
});`
Tried many different intents, yet not working.
Never use all intents, it significally slows down your bot and is a bad practise. You can read the guide on intents here and a list of intents here.
Here's an example from the guide:
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
});
In Discord.JS, all intents are PascalCase. Keep that in mind.

How to send an embed message to a different channel in interactions?

How to send a separate embed message to a channel different from where the slash command was executed? My current code is the following block.
const { SlashCommandBuilder, PermissionFlagsBits, EmbedBuilder, Guild } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('arrest')
.setDescription('puts the member to jail')
.addUserOption(option =>
option
.setName('target')
.setDescription('The member to arrest')
.setRequired(true))
.addStringOption(option =>
option
.setName('reason')
.setDescription('The reason for arrest'))
.setDefaultMemberPermissions(PermissionFlagsBits.ModerateMembers),
async execute(interaction) {
const messageEmbed = new EmbedBuilder()
.setTitle('ARRESTED!')
.setDescription(`***${interaction.options.getUser('target').tag}*** *broke a leg. Oops.*`)
.setColor(0xBD7A21)
.setImage('https://eca.astrookai.repl.co/media/arrest.gif')
await interaction.reply({embeds: [ messageEmbed ]});
},
};
Sending a separate embed object to a different channel is intended for channel logging for moderation actions in the server and I would like to know how to achieve this, thank you.
You can use <channel>.send() for this. In which <channel> is your channel object. You can get the channel in a multiple of different ways but this is probably the easiest:
logChannel = <interaction>.guild.channels.fetch(SNOWFLAKE_ID)
Then, you can send your embed to the logchannel like so:
logChannel.send({ embeds: [YOUR_EMBED] });
To account for every command, the easiest way is to do this in your interactionCreate event
If you need more context, you can check out this example which "logs" an embed to the logChannel.

How can i setup my discord bot automatically?

I have a discord bot but I want when someone add my bot to their server they dont need to write !setup. How I can do it automatically ?
client.on('messageCreate', async message => {
if (message.content === '!setup') {
await message.guild.commands
.set(client.commands)
}
});
Use the Client#guildCreate event
For instance:
client.on("guildCreate", guild => {
// What to do when the bot is invited
}
Under client in the discord.js docs there is an event called guildCreate which is emitted when the client joins a guild. If you listen for this event and run your setup code when it is emitted this might be what your after.
const { Client, Intents} = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
client.on('guildCreate', guild => {
//Your setup code
});
If it's just one server that you wan to add the bot to, you can do it manually. Just follow these steps:
Go into discord developer portal > click on your bot > Oauth2 > URl Generator > click bot and any other scope you might need > choose your perms > copy and past the link into your browser and you should be done!
To do this however, you must have manage server perms in that server

Trying to debug my bot but nothing shows up

Hi this is my code for my discord bot, I try to run it and it comes up without an error, even the "im alive" log comes up in console, but nothing else, thought it was my token but the bot logs in,
the only thing I can think of is the client.on but even in doc's I cant see the diffrence.
Im new to js so anybody's help is appreciated
const Discord = require("discord.js");
const client = new Discord.Client({intents: [Discord.Intents.FLAGS.GUILDS]});
const prefix = '!';
client.once('ready', () => {
console.log("I'm alive!");
});
client.on('message', message => {
console.log('asd');
if(message.content = '${prefix}hi'){
console.log('command detected');
message.channel.send('Hi ${message.author}');
}
});
client.login('my-token');
For getting the message data, you need to enable the GUILD_MESSAGES intent in your client as well as you need to go to your Discord Developer page and then in your application, go to the Bot option and the check the Message Content Intent

Why does my discord bot not add roles properly?

I am trying to make a bot that would add a role that is in the server when a person types !join choiceOutOfVariousRoles. I am currently using discord version 12. My error message is:
fn = fn.bind(thisArg);
Although trying various techniques I could not get the code to work.
const Discord = require('discord.js');
const client= new Discord.Client();
const token = process.env.DISCORD_BOT_SECRET
client.on('ready', () => {
console.log("I'm in");
console.log(client.user.username);
});
client.on('message', msg => {
if (msg.content.toLowerCase().startsWith("!join"))
{
var args = msg.content.toLowerCase().split(" ")
console.log(args)
if (args[1] === 'sullen')
{
msg.channel.send('You have successfully joined Sullen!')
const sullenRole = msg.guild.roles.cache.find('name','Sullen')
msg.member.addRole(role.id)
}
}
});
client.login(token)
**EDIT: Fixed what everyone was saying and all I need to do now Is update the permissions, (my friend has to do that because its not my bot) and I should be all good. Thanks everyone! :D
discord.js introduces breaking changes very frequently, and v12 is no exception. You need to make sure you find up-to-date code otherwise it won't work. GuildMember#addRole was moved to GuildMemberRoleManager#add, which means you must use msg.member.roles.add(sullenRole).

Resources