I am new to coding a discord bot with JS and I am not sure why I am getting an error when I am trying to make a collection for a library of other .js files. I am instead greeted with the image below:enter image description here
Broken code:
https://sourceb.in/x2EFkdb5sV
Line 14.
I tried defining Collections using another question on this site with similar problems:"Collection is not defined" when trying to create a discord bot .
It wasnt the answer I was looking for as it deviates from making a collection.
You must require the class Collection from discord.js to be able to use the Collection class.
At the beggining of your code, change const { Client, GatewayIntentBits } = require('discord.js'); to const { Client, GatewayIntentBits, Collection } = require('discord.js');
In addition, its Collection, not Collections (there is no s). So you will have to write new Collection(); instead of new Collections();.
Related
I want to get number of members with a role, but always give me "1":
const server_roles = client.guilds.cache.get('server ID').roles.cache.get('role ID').members.size;
console.log(server_roles)
Log:
Real role member count = 4, not 1
The issue isn't within your code, it's to do with the information bots can access after a recent update. In order to fix this:
Go to your Discord Developer Application page https://discord.com/developers/applications/
Open your Discord bot application
In the left side menu, select Bot
Scroll down to Privileged Gateway Intents
Turn both toggles on next to PRESENCE INTENT and SERVER MEMBERS INTENT
Then, in the code, when initialising your Discord client, add this:
Discord.Client({ ws: { intents: Discord.Intents.ALL } });
That code assumes you've imported the module as Discord
It's quite complex, but after the recent update it's the only way to achieve what you want
My code has been working just fine for weeks, but a few events and functions have randomly stopped working!
Firstly, my guildMemberAdd, guildMemberRemove, and guildMemberUpdate events simply stopped doing anything. No errors are appearing, and when I debugged my code, I realized the event wasn't even being emitted when the corresponding action took place.
// const client = new Discord.Client();
client.on('guildMemberAdd', (member) => // not triggering!
client.channels.cache.get('channel-id').send(`${member.tag} joined!`); // not sending!
Secondly, when trying to get a member from the GuildMemberManager cache, it always returns undefined:
const member = message.guild.members.cache.get(targetID); // undefined
When I then tried to display every member in that guild's member cache, it only showed me and my bot instead of the usual 100+ members.
Then I tried to fetch every member in my guild using GuildMemberManager.fetch():
const members = await message.guild.members.fetch();
But I got this error:
[GUILD_MEMBERS_TIMEOUT]: Members didn't arrive in time.
Again, I'm sure my syntax is correct, as it's been working perfectly for a while, and I haven't updated anything recently that would affect this code.
🤖 Discord is now enforcing privileged intents
What are intents?
Maintaining a stateful application can be difficult when it comes to the amount of data you're expected to process, especially at scale. Gateway Intents are a system to help you lower that computational burden.
Gateway intents allow you to pick and choose what events you choose to "subscribe" to so that you don't have to use up storage on events you're not using. This feature was introduced by Discord in 2020 and was supported by discord.js in v12.
What are privileged intents?
Some intents are defined as "Privileged" due to the sensitive nature of the data. Those intents are:
GUILD_PRESENCES
GUILD_MEMBERS
As of October 27th 2020, these intents have been turned off by default.
Some problems you might be facing because of this
GUILD_PRESENCES
member and user caches are empty (or very close to it) on startup
Guild.memberCount returns count as of ready
All events involving Presences do not trigger (presenceUpdate)
Some Presence data returns null or undefined
All GuildMembers appear to the bot as offline.
client.login() times out if you specified the fetchAllMembers option in your ClientOptions
GUILD_MEMBERS
member and user caches are empty (or very close to it) on startup
GuildMemberManager.fetch() and UserManager.fetch() methods time out
All events involving GuildMembers do not trigger (guildMemberAdd, guildMemberRemove, guildMemberUpdate, guildMemberSpeaking, and guildMembersChunk)
How do I enable intents?
Through the Discord Developer Portal:
Firstly, you must manually enable the intents from the Discord Developer site. Go to applications, select your app, and find the "bot" tab on the sidebar. Then you can scroll down until you see this:
As shown in the screenshot, your bot will require verification if in more than 75 guilds.
If your bot is verified:
Once your bot is verified, you won't be able to manually flip the intent switches in the Developer Portal. To request whitelisted access to an additional privileged gateway intent for a verified bot, please send our support team a ticket here!
Make sure to include your bot's ID, which intents you're requesting, a basic description of your use case for the requested intent, and screenshots or video of that use case in action (or code snippets, if not user facing!).
Through the discord.js module:
Once you have either/both intents checked off, you just have to enable them through discord.js. The discord.js intents guide thoroughly explains how to do this, but I will paraphrase it here.
You don't need to go through these steps if you want every intent. Discord enables all intents (except these two, obviously) by default. As long as you checked off both intents in the Developer Portal, you can stop here if you don't care about blocking any other intents. If you do, just remember intents are only supported by discord.js v12+, so you might have to upgrade.
One of the ClientOptions (ClientOptions is a typedef of potential options to pass when creating your client) is ws (yet another typedef of potential websocket options). In there, you'll find the intents property.
intents accepts a IntentsResolvable, which can be a string or an array of strings of an intent(s) (such as 'GUILD_PRESENCES'. All available intents), a bitfield (a number corresponding to an intent(s)), an instance of the Intents class.
Examples:
// using a string
const client = new Discord.Client({ ws: { intents: 'GUILD_PRESENCES' }});
// using an array
const client = new Discord.Client({ ws: { intents: ['GUILD_PRESENCES', 'GUILD_MEMBERS'] }});
// using a bitfield value
const client = new Discord.Client({ ws: { intents: 32509 }));
// using Intents class
const client = new Discord.Client({ ws: { intents: Discord.Intents.PRIVILEDGED }});
const client = new Discord.Client({ ws: { intents: new Discord.Intents(Discord.Intents.ALL) }});
Resources:
Discord.js Official Guide - Gateway Intents
Discord Developer Documentation - Gateway Intents
Gateway Update FAQ
Discord API Github - Issue 1363 - Privileged Intents
Discord Blog - The Future of Bots on Discord
TL;DR
To fix this issue, go to:
Discord Developer Portal > Applications > Your Application > Bot > Check both/either intent(s) (screenshot above)
My code has been working just fine for weeks, but a few events and functions have randomly stopped working!
Firstly, my guildMemberAdd, guildMemberRemove, and guildMemberUpdate events simply stopped doing anything. No errors are appearing, and when I debugged my code, I realized the event wasn't even being emitted when the corresponding action took place.
// const client = new Discord.Client();
client.on('guildMemberAdd', (member) => // not triggering!
client.channels.cache.get('channel-id').send(`${member.tag} joined!`); // not sending!
Secondly, when trying to get a member from the GuildMemberManager cache, it always returns undefined:
const member = message.guild.members.cache.get(targetID); // undefined
When I then tried to display every member in that guild's member cache, it only showed me and my bot instead of the usual 100+ members.
Then I tried to fetch every member in my guild using GuildMemberManager.fetch():
const members = await message.guild.members.fetch();
But I got this error:
[GUILD_MEMBERS_TIMEOUT]: Members didn't arrive in time.
Again, I'm sure my syntax is correct, as it's been working perfectly for a while, and I haven't updated anything recently that would affect this code.
🤖 Discord is now enforcing privileged intents
What are intents?
Maintaining a stateful application can be difficult when it comes to the amount of data you're expected to process, especially at scale. Gateway Intents are a system to help you lower that computational burden.
Gateway intents allow you to pick and choose what events you choose to "subscribe" to so that you don't have to use up storage on events you're not using. This feature was introduced by Discord in 2020 and was supported by discord.js in v12.
What are privileged intents?
Some intents are defined as "Privileged" due to the sensitive nature of the data. Those intents are:
GUILD_PRESENCES
GUILD_MEMBERS
As of October 27th 2020, these intents have been turned off by default.
Some problems you might be facing because of this
GUILD_PRESENCES
member and user caches are empty (or very close to it) on startup
Guild.memberCount returns count as of ready
All events involving Presences do not trigger (presenceUpdate)
Some Presence data returns null or undefined
All GuildMembers appear to the bot as offline.
client.login() times out if you specified the fetchAllMembers option in your ClientOptions
GUILD_MEMBERS
member and user caches are empty (or very close to it) on startup
GuildMemberManager.fetch() and UserManager.fetch() methods time out
All events involving GuildMembers do not trigger (guildMemberAdd, guildMemberRemove, guildMemberUpdate, guildMemberSpeaking, and guildMembersChunk)
How do I enable intents?
Through the Discord Developer Portal:
Firstly, you must manually enable the intents from the Discord Developer site. Go to applications, select your app, and find the "bot" tab on the sidebar. Then you can scroll down until you see this:
As shown in the screenshot, your bot will require verification if in more than 75 guilds.
If your bot is verified:
Once your bot is verified, you won't be able to manually flip the intent switches in the Developer Portal. To request whitelisted access to an additional privileged gateway intent for a verified bot, please send our support team a ticket here!
Make sure to include your bot's ID, which intents you're requesting, a basic description of your use case for the requested intent, and screenshots or video of that use case in action (or code snippets, if not user facing!).
Through the discord.js module:
Once you have either/both intents checked off, you just have to enable them through discord.js. The discord.js intents guide thoroughly explains how to do this, but I will paraphrase it here.
You don't need to go through these steps if you want every intent. Discord enables all intents (except these two, obviously) by default. As long as you checked off both intents in the Developer Portal, you can stop here if you don't care about blocking any other intents. If you do, just remember intents are only supported by discord.js v12+, so you might have to upgrade.
One of the ClientOptions (ClientOptions is a typedef of potential options to pass when creating your client) is ws (yet another typedef of potential websocket options). In there, you'll find the intents property.
intents accepts a IntentsResolvable, which can be a string or an array of strings of an intent(s) (such as 'GUILD_PRESENCES'. All available intents), a bitfield (a number corresponding to an intent(s)), an instance of the Intents class.
Examples:
// using a string
const client = new Discord.Client({ ws: { intents: 'GUILD_PRESENCES' }});
// using an array
const client = new Discord.Client({ ws: { intents: ['GUILD_PRESENCES', 'GUILD_MEMBERS'] }});
// using a bitfield value
const client = new Discord.Client({ ws: { intents: 32509 }));
// using Intents class
const client = new Discord.Client({ ws: { intents: Discord.Intents.PRIVILEDGED }});
const client = new Discord.Client({ ws: { intents: new Discord.Intents(Discord.Intents.ALL) }});
Resources:
Discord.js Official Guide - Gateway Intents
Discord Developer Documentation - Gateway Intents
Gateway Update FAQ
Discord API Github - Issue 1363 - Privileged Intents
Discord Blog - The Future of Bots on Discord
TL;DR
To fix this issue, go to:
Discord Developer Portal > Applications > Your Application > Bot > Check both/either intent(s) (screenshot above)
I am trying to make a discord bot. However, i am stuck on a part where i have to check every hour if a discord username is equal to the contents of an array. If it is, I have to make the bot remove a role. However, doing so would require using the discord.js module, which I don't know how to add into the code (I already have the bot set up in index.js).
I've tried to require discord.js-commando but it returns:
"Error: A client must be specified"
This is the place of the problem:
class checkDet extends commando.Command {
This is where I want to use the discord.js module:
var guild = bot.guilds.get("GUILD ID HERE");
for (let [k, v] in Object.entries(guild.members)) {
if (v.user.username == userName)
{
v.removeRoles(v.roles).then(console.log).catch(console.error);
}
}
Multiple solutions. Solution 1: if you're just using Discord.JS and not the whole Commando module, try using const Discord = require('discord.js'); at the top of that file and use
it as normal!
Another method would be to pass the whole commando module through. There's many ways to do it, however the easiest is probably to add let commando; at the top of the file, then create a module.exports.define function to set commando to the first argument. Then in the main bot file, run require('path/to/my/command.js').define(commando);.
Or even simpler, if all you need is a recursive check, why not put it in the main bot.js file, or even another file? Either invoke the file including the commando object, or just run a setInterval in the original file with the normal code.
I create a slackbot fusing the following from this guide:
var util = require('util');
var path = require('path');
var fs = require('fs');
var SQLite = require('sqlite3').verbose();
var Bot = require('slackbots');
The slackbot I created is basic by replying to keywords and posting messages back in the channel using: this.postMessageToChannel(...)
What I would like is to use functions I see from the slack API such as the ability for the slackbot to leave a channel by itself. The channel.leave function found here in the slack API looks to be able to do this however I am not sure how to get it to work.
How am I able to use this Slack api correctly? Specifically starting with the channel.leave method?
To use any of Slack's API method you need a token. If you followed the instruction from the link you provided you can get your token from the page of installed apps, where you also find your bot.
If will look like this:
Just take the "API Token" and use it in your code to call any of the web methods. If you are unsure how to make an API call in node.js, check out this question.
There is one caveat to your specific problem though. This particular method does not work with a bot token (which is what you got), only with a user token. I don't think it is possible for a bot to leave a channel by itself. Only a real user can do that.