Discord.js Bot that Automatically Gives Roles - javascript

I'm trying to find out how to give a person a role automatically once he joins a server on Discord.
The tutorial I watched didn't explain this very well.
Here's a snippet of I have so far:
const fs = require('node:fs')
const path = require('node:path')
const { Client, Collection, GatewayIntentBits } = require('discord.js')
const { token } = require('./config.json')
const client = new Client({ intents: [GatewayIntentBits.Guilds], partials: ["MESSAGE", "CHANNEL", "REACTION"] })
client.commands = new Collection()
const commandsPath = path.join(__dirname, 'commands')
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'))
client.on('guildMemberAdd', guildMember => {
guildMember.roles.add('ROLE ID')
guildMember.guild.channels.cache.get('CHANNEL ID').send(`<#${guildMember.user.id}> joined as <#ROLE ID>!`)
})
Does anyone have any idea as to what I should do?
Thanks!
I expected an automatic Discord bot that gives someone a role on joining, but I got nothing. Nothing seems to respond.

Discord has made it so that listening for member join events is now a privileged intent, so you must add it:
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers], partials: ["MESSAGE", "CHANNEL", "REACTION"] });
See this page in the guide.

Related

My discord bot doesn't join voice channels, but it doesnt give me any error messages

Im making a discord bot in javascript for a server, I need it to join a voice channel when it starts but the code just doesn't work. It has the needed permisions, but it doesnt do anything, there isnt even an error message
This is the code (In channelId and guildId I just used the actual id of the channel i needed and the guild I needed) :
const Discord = require("discord.js")
const VoiceDiscord= require("#discordjs/voice");
const Client = new Discord.Client({
intents: [
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildMessages,
Discord.GatewayIntentBits.MessageContent,
Discord.GatewayIntentBits.GuildMessageReactions,
Discord.GatewayIntentBits.GuildMembers
]
});
Client.once("ready",() => {
const connection =VoiceDiscord.joinVoiceChannel({
channelId: 980878176928550912,
guildId: 980878092849528832,
adapterCreator: Client.guilds.cache.find(guild => guild.id == 980878092849528832).voiceAdapterCreator
})
});
I tried uninstalling and reinstalling discord.js and #discordjs/voice, I tried using another bot and it also didnt work
Your event listener never fires because it should be Client.on and not Client.once. Your correct code looks like:
const Discord = require("discord.js")
const VoiceDiscord= require("#discordjs/voice");
const Client = new Discord.Client({
intents: [
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildMessages,
Discord.GatewayIntentBits.MessageContent,
Discord.GatewayIntentBits.GuildMessageReactions,
Discord.GatewayIntentBits.GuildMembers
]
});
Client.on("ready",() => {
const connection =VoiceDiscord.joinVoiceChannel({
channelId: 980878176928550912,
guildId: 980878092849528832,
adapterCreator: Client.guilds.cache.find(guild => guild.id == 980878092849528832).voiceAdapterCreator
})
});

Discord.js#14.6 cant create channel

Ahhhhh I am going insane. I have asked many questions about creating channels, but i still dont manage to understand, wtf am i doing wrong????
This is the code i am using:
const Discord = require('discord.js');
const { Client, GatewayIntentBits, messageLink, InteractionResponseType, ChannelType } = require('discord.js');
const logger = require('winston');
const discord = require("discord.js");
const { EmbedBuilder, SlashCommandBuilder } = require('discord.js');
const bot = new Discord.Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildEmojisAndStickers,
GatewayIntentBits.GuildMessageReactions,
],
});
//
const fs = require("node:fs");
const guild = bot.guilds.cache.get("1037081018500399154");
const path = require("node:path");
const token = "you tought"
//text
bot.on('ready', () => { // when the bot is ready and online
console.log('bot is now online!')
bot.user.setActivity("Servers");
});
guild.channels.create({
name: "hello",
type: ChannelType.GuildText,
// your permission overwrites or other options here
});
It throws this msg everytime i start my bot
Uncaught TypeError TypeError: Cannot read properties of undefined (reading 'channels')
Pleas help me, i am going insane ngl. And yes, i have read the docs.
{Edit : There is usually more code, but i have just deleted the code as of now}
Put it in a ready event. It should be like this:
bot.on("ready" , () => {
const guild = bot.guilds.cache.get("1037081018500399154");
guild.channels.create({
name: "hello",
type: ChannelType.GuildText,
// your permission overwrites or other options here
});
});
I hope to be of help to you.

noblox.js and discord.js, currently using discord.js and got a intent error, v13

Was watching a tutorial on making a discord.js and noblox.js bot. error is something about intents not being a constructor or whatever. tutorial playlist isnt even helpful like the discord server. im just following the tutorial to set up a bot account and some slash commands with noblox.js (a roblox js API i guess or a wrapper idk)
code:
const { Client, Intents } = require('discord.js')
const noblox = require('noblox.js')
const { Token } = require('./config.json')
// Create a new client instance
const intents = new Intents([
'GUILDS',
'GUILD_MEMBERS',
'GUILD_MESSAGES'
])
const bot = new Client({ Intents : intents })
bot.login(Token)
bot.on('ready', () => {
console.log(`${bot.user.tag} has booted up.`)
})
error:
TypeError: Intents is not a constructor
at Object.<anonymous> (/home/runner/ImpishDefiantClasses/index.js:7:17)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
IDE: Replit
First of all, You should require intents:
const { Client, Intents } = require("discord.js");
const { Token } = require("./config.json")
const bot = new Client({
intents: [Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.MESSAGE_CREATE]
});
Then, the things we want to do when the bot is ready:
bot.on('ready', () => {
console.log(`${bot.user.tag} is now online!`)
})
Also, Make sure you put bot.login(Token) at the end of your code.
At the end, If you want to receive messages, you need to listen to messageCreate event:
client.on("messageCreate", async (message) => {
if (message.author.bot) return;
if (message.content.startsWith('!ping')) {
message.reply('Pong!')
}
});
So your code should look like this:
const { Client, Intents } = require("discord.js");
const bot = new Client({
intents: [Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.MESSAGE_CREATE]
});
bot.on('ready', () => {
console.log(`${bot.user.tag} is now online!`)
})
client.on("messageCreate", async (message) => {
if (message.author.bot) return;
if (message.content.startsWith('!ping')) {
message.reply('Pong!')
}
});
bot.login(Token);
More info on intents.

Discord Api Valid intents must be provided for the Client but the code dosent work

I set up the
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
for my discord.js bot.
But now I just get the error message:
Uncaught c:\Users\niko\Documents\bot\receptionist\index.js:8
The line 8 in my index.js is this:
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
remove the first line of the code which states:
const Discord = require('discord.js')
and also delete the third line of the code which follows :
const client= new Discord.Client()
because you're declaring the client with the intents below after declaring the
prefix.
And also delete
const bot = client
const { Client, Intents, MessageEmbed, Presence, Collection, Interaction } = require ('discord.js');
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]
});

How can I upgrade to Discord.js V13?

Im using Discord.js V13 and when i try to run the bot i get this error everytime.
Main File:
const { Discord, Intents } = require('discord.js');
const client = new Discord.Client({
partials: ["CHANNEL","MESSAGE","REACTION"],
intents: [Intents.ALL]
});
The error:
const client = new Discord.Client({
^
TypeError: Cannot read properties of undefined (reading 'Client')
The solution here is that i can`t deconstruct the library from itself, and my mistake is that i needed to put only the intents that my bot needs.
My solution:
const Discord = require('discord.js');
const client = new Discord.Client({
partials: ["CHANNEL","MESSAGE","REACTION"],
intents: [
Discord.Intents.FLAGS.GUILDS, // <--line 5 here
Discord.Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Discord.Intents.FLAGS.GUILD_MESSAGES,
Discord.Intents.FLAGS.GUILD_INVITES,
Discord.Intents.FLAGS.GUILD_MEMBERS,
Discord.Intents.FLAGS.GUILD_PRESENCES
]
});
You cannot deconstruct the library from itself.
Either deconstruct the client:
const { Client, Intents } = require('discord.js');
const client = new Client(...);
// ...
Or utilitize the library entirely:
const Discord = require('discord.js');
const client = new Discord.Client(...);
// ...

Categories

Resources