So, I'm programming a Discord bot, and one of the things I want it to do is to assign roles to members given certain conditions. After looking through the documentation, specifically here, I figured bot.addMemberToRole would be a good command to use. However, when I ran it, I got this error message:
TypeError: bot.addMemberToRole is not a function
I was understandably confused, as the documentation clearly says that this IS a function. I have tried doing bot.addMemberToRole(member, role);, addMemberToRole(member, role);, and several other iterations. This is my most recent attempt:
const Discord = require('discord.js');
const bot = new Discord.Client();
bot.addMemberToRole(member, role, function(err){
if(err){
console.log(err);
}
});
I have also done just this:
bot.addMemberToRole(member, role);
Both gave the same TypeError as above.
I have no idea why it doesn't work. I followed the documentation exactly, the member and role variables I pass into it are the proper type, and other Discord.js commands work just fine in my bot. Any help would be appreciated.
You're using an old version of the docs so that function doesn't exist anymore. They should really get rid of those. You're looking for GuildMember.addRole(Role or String).
To add a member to a role, you need a GuildMember and a Role object (or the name of the role). Assuming you have the User object and the Guild object (your bot has a list of the guilds/servers it's joined and most events will have the guild they're associated with), you can get the GuildMember by using Guild.fetchMember(User). From there, you can add the role on the GuildMember using either the string or object based version of addRole.
Here's an example of how to do it upon receiving a message from a user which is very easy since the message has a GuildMember associated with it.
const Discord = require('discord.js');
const bot = new Discord.Client();
bot.on('message', (message) => {
const guildMember = message.member;
guildMember.addRole('bot-added-role');
});
Related
How do I check if the bot (Self) has permissions to send messages and make it an if statement? Im using discord js and I've seen forums like member.roles.cache has, but none cover the bot itself, so how do I check that?
As Elitezen said, you can get GuildMember object using Guild#me but to check if your bot can send messages for example in guild where the command was executed you have to use this:
if(message.guild.me.permissions.has("SEND_MESSAGES")) { // check if bot has SEND_MESSAGES permission
// your code
}
You can use Guild#me to get the client's GuildMember object in that guild.
// guild = message.guild or any guild object
if (guild.me.roles.cache.has(...)) {
}
Update: in Discord.JS V14 you now have to use guild.members.me
My server has experienced massive raids and so I wish to make a command that can ban all members whose usernames contain a certain word.
The main problem is: I don't understand what method to use to ban everyone whose username contains argument.
I don't ask for you to create a command for me.
Code
let target_word = args[0];
message.guild.members.fetch().then(fetchedMembers => {
const target_users = fetchedMembers.filter(member => member.user.username.includes(target_word))
target_users.ban({reason:reason})
})`
Return an error:
(node:673) UnhandledPromiseRejectionWarning: TypeError: target_users.ban is not a function
For example
!ban_all raid to ban all members whose names contain the word raid
According to discord.js documentation, you can use the function "ban" on a guildmember object:
https://discord.js.org/#/docs/main/stable/class/GuildMember?scrollTo=ban
I found this question on SO to show you how to get and filter your guild members:
Get guild members and filter them (Discord.js)
Get members, filter on username, ban them
EDIT:
Your provided code looks good, now you have to loop through your target users and ban them:
let target_word = args[0];
message.guild.members.fetch().then(fetchedMembers => {
const target_users = fetchedMembers.filter(member => member.user.username.includes(target_word));
target_users.forEach(user => user.ban());
}
A side note: Make sure your bot only acts on messages from your Mods otherwise everyone could ban anyone
When I’m trying to get the member list from my discord server, djsv12 returns me a list, but it has only 1 entry.
In fact, there should be 23 entries instead because that's the amount of members on my server.
I don't understand what's wrong. You can find my code below:
const Guild = client.guilds.cache.first(); // My one server
const Members = Guild.members.cache.map(member => member.id);
console.log(Members);
Console:
[ '578565698461106204' ]
Maybe it's because only one member is cached. Try to fetch all the members first.
members.fetch() fetches members from Discord, even if they're offline and returns promise that resolves to a collection of GuildMembers:
const guild = client.guilds.cache.first(); // My one server
const members = await guild.members.fetch();
const memberIDs = members.map((member) => member.id);
console.log(memberIDs);
PS: As I'm using the await keyword, make sure that you're in an async function.
I believe you're encountering the same problem as mentioned in this question.
Since Discord has been implementing privileged intents, that means that you have to grant your bot permission access to certain data. This privilege is turned off by default.
To turn it on, simply go to the Discord Developer Portal. Then select your Application, then go to the Bot section on the left side and scroll down. You should see the two options "Presence Intent" and "Server Members Intent".
Resources:
None of my discord.js guildmember events are emitting, my user caches are basically empty, and my functions are timing out?
Discord.js Guide - Gateway Intents
I'm pretty new to working on Discord bots, but I'm trying to create a bot for Discord that gets all the members in a guild, so they can be used later on. But everytime I try to get the guild with its ID it gives an undefined back, even though I know I put in the right ID.
const Discord = require("discord.js");
const client = new Discord.Client();
const members = client.guilds.cache.get("guildID");
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
console.log(members);
});
client.login('token');
Am I doing something wrong?
You aren't being very clear. You stated you want your bot to get all members in a guild, but you proceed to get a guild ID instead.
Assuming you want to get all members in a guild. First thing you need is Privileged Gateway Intents enabled in your bot and implemented in your code.
Step 1: Enable Intents
Go to your Bot App Dev
Select your bot
Head to the Bot section of your bot and scroll down till you see "Privileged Gateway Intents" and select both "PRESENCE INTENT" and "SERVER MEMBERS INTENT
"
Example
Step 2: Implementing In Code
const Discord = require('discord.js');
const client = new Discord.Client({ ws: { intents: new Discord.Intents(Discord.Intents.ALL) }});
^^^^^^^
This is what is making you access all server members within a guild
Code for getting all server members:
// If prefix not set: do this: const prefix = "!"; whatever you want
const prefix = "!";
// Usernames
const members = message.guild.members.cache.map(member => member.user.tag);
// ID's
const members = message.guild.members.cache.map(member => member.user.id);
if(message.content.startsWith(prefix + "test")){ // Gets all members usernames within the guild this message was sent on.
console.log(members) // Logs all each members username
}
In depth but I hope it helps.
I believe it's just:
client.guilds.get("guildID");
and not:
client.guilds.cache.get("guildID");
Try using this instead:
client.guilds.fetch("guildID");
Also, I recommend you put that line in your ready event or somewhere where you are sure the client has fully logged in when ran to make sure that it is logged in when you run that.
Before client is ready, use client.guilds.fetch('<guild id>') which returns a promise. Note that before the ready event is triggered many things on the client are uninitialized such as client.user
Put the const members = client.guilds.cache.get("guildID"); inside the ready event.
Try:
const members = client.guilds.cache.get("guildID");
list.members.cache.forEach(member => console.log(member.user.username));
I'm making a discord bot, and I'm trying to make use of the createChannel function shown here in the documentation. For some reason, I am getting the following error:
TypeError: bot.createChannel is not a function.
My code is within a function which I pass a message to, and I have been able to create roles and add users to roles within the same function. It's just the createChannel function that's not working. Below is the relevant portions of the code.
const bot = new Discord.Client();
function makeChannel(message){
var server = message.guild;
var name = message.author.username;
server.createRole(data);
var newrole = server.roles.find("name", name);
message.author.addrole(newrole);
/* The above 3 lines all work perfectly */
bot.createChannel(server,name);
}
I have also tried bot.addChannel, and bot.ChannelCreate, since ChannelCreate.js is the name of the file which contains the code for this command. Also, I have attempted specifying channel type and assigning a callback function as well, but the main issue is the TypeError saying that this isn't a function at all. Any idea what I'm doing wrong?
Additionally, I plan to use ServerChannel.update() at some point in the future, so any advice on getting that to work once the previous problem is resolved would be greatly appreciated.
Alright, after a few days of trying things and going through the docs, I have discovered the solution. I am using a more recent version of Discord than the docs I was reading were written for. In the newer version, channels are created with a method in the server, not a client method. so, the code should be:
const bot = new Discord.Client();
function makeChannel(message){
var server = message.guild;
var name = message.author.username;
server.createChannel(name, "text");
}
The "text" value is the type of channel you are making. Can be text or voice.
I'll post a link to the most recent documentation for anyone else who encounters this problem here.
The answer should update documentation link to the GuildChannelManager which is now responsible for creating new channel.
(Example from docs)
// Create a new text channel
guild.channels.create('new-general', { reason: 'Needed a cool new channel' })
.then(console.log)
.catch(console.error);
https://discord.js.org/#/docs/main/stable/class/GuildChannelManager
#Jim Knee's I think your answer is v11, I'm new in discord.js, using Visual Studio Code's auto-code thingy. You can do all the same things except your thing must be this. If you are poor people, getting errors on doing #Jim Knee's answer, this is the place for "YOU!"
Get rid of server.createChannel(name, "text/voice");
And get it to THIS server.channels.create(name, "text/voice");
Hope I can help at least ;)
I'm just a new guy here too
I think you have not logged in with your bot.
From the docs:
const Discord = require('discord.js');
var client = new Discord.Client();
client.login('mybot#example.com', 'password', output); // you seem to be missing this
function output(error, token) {
if (error) {
console.log(`There was an error logging in: ${error}`);
return;
} else
console.log(`Logged in. Token: ${token}`);
}
Alternatively, you can also login with a token instead. See the docs for the example.