I want to make a discord bot program. when someone new goes to the server they have to type !daftar to be able to enjoy the server. and when they type !daftar message list will appear on #welcome channel. but I get an error that is in the title. here is my code
const { GuildMember } = require("discord.js");
module.exports = {
name: 'daftar',
description: "This is for add roles to a member",
execute(message, args) {
let role = message.guild.roles.cache.find(r => r.name === "Members")
if (message.member.roles.cache.some(r => r.name === "Members")) {
message.channel.send('Kamu sudah menjadi MEMBER Di grup ini');
} else {
message.member.roles.add('817360044622217276');
message.member.roles.remove('817965925122048010');
message.channel.send('Baiklah silahkan menikmati Server');
GuildMember.guild.channels.cache.get('817957997312737290').send(`Selamat Datang <#${GuildMember.user.id}>`)
}
}
}
GuildMember is not exactly defined. Yes, you are destructuring it as the property of discord.js, but it's not exactly defined as who the member actually is. Right now it's just an empty object that belongs to no one, meaning that it also has no properties itself as it does not know what it refers to.
Assuming that you're wanting to give the role to the member who typed this command, you'd have to make the GuildMember object the property of the Message object that is defined as message in your parameters. We can get this object using the member property of message => message.member.
Now, assuming that the channel you're looking to send the message to is found in the same guild as the message object, there is no sense behind using a GuildMember object to find a certain channel, when we can instead use the Message object, as seen below:
Final Code
module.exports = {
name: 'daftar',
description: "This is for add roles to a member",
execute(message, args) {
let role = message.guild.roles.cache.find(r => r.name === "Members")
if (message.member.roles.cache.some(r => r.name === "Members")) {
message.channel.send('Kamu sudah menjadi MEMBER Di grup ini');
} else {
message.member.roles.add('817360044622217276');
message.member.roles.remove('817965925122048010');
message.channel.send('Baiklah silahkan menikmati Server');
message.guild.channels.cache.get('817957997312737290').send(`Selamat Datang <#${GuildMember.user.id}>`)
}
}
}
Related
For this piece of code, I aim for the author of the message that sent the ban command to be checked for whether they have the permissions necessary to ban the member, instead of instantly being allowed to, however I am unsure of what the const mod should be equivalent to to do so. Any help would be appreciated, thanks.
module.exports = {
name: 'ban',
description: "This command ban a member!",
execute(message, args) {
const target = message.mentions.users.first();
const mod = message.author();
if (mod.hasPermission('BAN_MEMBERS')) {
if (target) {
const memberTarget = message.guild.members.cache.get(target.id);
memberTarget.ban();
message.channel.send("User has been banned");
}
} else if (mod.hasPermission(!'BAN_MEMBERS')) {
message.channel.send(`You couldn't ban that member either because they don't exist or because you dont have the required roles!`);
}
}
}
As it says, Message.author is not a function. It is a property. You access it without the brackets
const mod = message.author
Another problem you may get is you can't get permissions. You will need Message.member for that
const mod = message.member
I'm trying to add a role to a member when they join my server, here is the code I have:
The problem I'm getting is "ReferenceError: roles is not defined" can someone please help me solve this?
client.on("guildMemberAdd", (member) => {
console.log("User " + member.user.username + " has joined the server!");
var role = member.guild.roles.cache.find((role) => role.name === "Javjajjaj");
roles.add(role);
});
The problem is that the roles variable is never defined within your piece of code.
client.on("guildMemberAdd", GuildMember => {
// Logging when a GuildMember enters the Guild.
console.log(`${GuildMember.user.tag} joined ${GuildMember.guild.name}!`);
const Role = GuildMember.guild.roles.cache.find(role => role.name == "Javjajjaj"); // Finding the Role by name in the GuildMember's Guild.
// Checking if the role exists.
if (!Role) return console.error("Couldn't find the role!");
// Trying to add the Role to the GuildMember and catching any error(s).
GuildMember.roles.add(Role).catch(error => console.error(`Couldn't add the Role to the GuildMember. | ${error}`));
});
Hi I am following a tutorial on Youtube but I can't figure out why it's not working. Can someone explain to me why it isn't working?
There are no errors in the console but the role doesn't get added
Since you're using discord.js^12.x module, I can still see you use member.removeRoles() and member.addRoles(), which are deprecated. And also, the member variable you made was a user property not a guildMember property. Try to follow the code below:
const Discord = require('discord.js');
const { prefix } = require('../config.json');
module.exports = {
name: 'coloradd',
description: 'Give You Your Color',
execute(message, args, client) {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const colors = message.guild.roles.cache.filter(c => c.name.startsWith('#'))
let str = args.join(' ')
let role = colors.find(role => role.name.slice(1).toLowerCase() === str.toLowerCase());
if (!role) {
return message.channel.send('That color doens\'t exist')
} try {
const member = message.member; // Use `message.member` instead to get the `guldMember` property.
// `addRoles()` and `removeRoles()` are deprecated.
member.roles.remove(colors);
member.roles.add(role);
message.channel.send('Your Have Received Your Color !')
} catch (e) {
let embed = new Discord.MessageEmbed()
.setTitle('Error')
.setColor('RED')
.setDescription('There is an error with the role Hierarchy Or my permissions. Make Sure I am above the color roles')
return (embed)
}
}
}
Visit these guides to help you understand more about the subject:
Discordjs.guide - Guide - Changes from v11 to v12
Discordjs.guide - Guide - Adding a role to a guildMember
Discord.js.org - Docs - guildMember Property
Discord.js.org - Docs - User Property
I'm working on my ban command and it's fully functional, but I was wondering, how I would log the ban reason into the ban section of discord (example in the attachment)? My code looks like this:
const { DiscordAPIError } = require("discord.js");
const Discord = require('discord.js');
module.exports = {
name: 'ban',
description: "Mentioned user will be banned",
execute(message, args){
if (!message.member.hasPermission("BAN_MEMBERS")) return message.channel.send("Invalid Permissions")
let User = message.guild.member(message.mentions.members.first()) || message.guild.members.cache.get(args[0])
if (!User) return message.channel.send("Invalid User")
if (User.hasPermission("BAN_MEMBERS")) return message.reply("Invalid Permissions a")
let banReason = args.join(" ").slice(22);
if (!banReason) {
banReason = "None"
}
console.log(`USER = ${User}`)
User.ban
console.log(`Ban reason = ${banReason}`)
var UserID = User.id
console.log(`USER ID = ${UserID}`)
}
}
Any help? Thanks :)
It's actually quite easy, just add ({reason: banReason}) after the User.ban part, making it:
User.ban({reason: banReason})
That should log it there properly :)
You can use the reason property of the options parameter in the GuildMember.ban() method:
User.ban({ reason: 'real life danger' })
.then(member => console.log(`${member.name} was banned.`))
.catch(console.error)
Also, in your code, there's no reason for using the Guild.member() method when defining User. Guild.member(user) is used to turn a User object into a GuildMember object.
However, MessageMentions.members.first() already returns a GuildMember object, making it obsolete.
So I have this inside my main index.js to check for any blacklisted users to prevent them from joining if they tried to join back again.
bot.on("guildMemberAdd", member => {
if(member.user.bot)
if (!db.get("blacklist_users").find({ user_id: member.id }).value()) {
return;
} else {
member.ban();
member.send("**You are blacklisted.**")
}
})
bot.on('message', message => {
let member = message.author;
if(!db.get("blacklist_users").find({ user_id: member.id }).value()) {
return;
}else {
member.ban();
member.send("**You are blacklisted.**")
}
})
Always be sure to check the API documentation for types. In this case, message.author returns a User object, not a GuildMember object. User does not have a ban function because it isn't associated with a guild. If you need the member object you will need to retrieve it from the member property.
let member = message.member;
Just be wary that this may be undefined if the message did not originate in a guild (i.e. Direct Message) or the member has left the guild before it is executed.
So i got it solved now by moving the code to my event instead of leaving it in the main file. And of course i had to define some other stuff in order to read where the blacklisted user is.
if (!db.get("blacklist_users").find({ user_id: member.id }).value()) {
return;
} else {
member.ban()
console.log(`User "${member.user.username}" has joined tried to join "${member.guild.name} and is probably blacklisted` )
}
i hope this will help you guys