I am trying to grant users a role once they submit a form but I am getting this in nodejs
TypeError: Cannot read property 'roles' of undefined
I have the code set up like this:
const guild = discordClient.guilds.cache.get('1234567890');
const member = await guild.members.cache.get('22222222')
const role = await guild.roles.cache.find(role => role.id === '111111');
member.roles.add(role);
what am I doing wrong?
Try using this declaration for the guild.
const guild = message.guild;
Please refer to the docs before asking questions. Anyways the answer would be
let guild = client.guilds.cache.fetch('id');
const role = await guild.roles.cache.find(role => role.id === 'ROLEID');
const member = await guild.members.fetch(req.user.discord_id);
member.roles.add(role);
as seen here on the docs
Related
I am trying to add a mute role to a user in a slash commands but it does not work.
Here is my code:
module.exports = {
data: new SlashCommandBuilder()
.setName('mute')
.setDescription('Mute someone')
.addUserOption(option => option.setName("member").setRequired(true).setDescription("Member to mute")),
async execute(interaction) {
if (interaction.member.roles.cache.some(r => r.name === "Admin")) {
const membre = interaction.options.getUser("member");
const guild=interaction.member.guild
const role = await guild.roles.cache.find(role => role.name === "MUTED");;
membre.roles.add(role);
}
else {
await interaction.reply({content: "**YOU DON'T HAVE THE PERMISSIONS !**", ephemeral: true });
}
},
};
Here is my error :
TypeError: Cannot read properties of undefined (reading 'add')
at Object.execute (C:\Users\sunda\Downloads\chabibot-main\commands\mute.js:17:26)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Client.<anonymous> (C:\Users\sunda\Downloads\chabibot-main\index.js:29:3)
const membre = interaction.options.getUser('member'); // user
const membre = interaction.options.getMember('member'); // member
Role adding is only available on member not user.
Docs
I'm trying to set up a command that when mentioning a member gives them a specific role. However every time I get the same roles undefined error. Can anyone help me out?
My code:
client.on('messageCreate', (message) => {
if (message.content == '!brig'){
const role = message.guild.roles.cache.find(role => role.name === 'BRIG')
const target = message.mentions.members.first();
target.roles.add('757816527478325391')
}
})
I made a small modification to your code changing message.content === '!brig' to message.content.includes("!brig") and it seems like it works fine for me. You can try my code and let me know if anything changes:
const { Client } = require("discord.js");
const client = new Client({
intents: ["GUILDS", "GUILD_MESSAGES"],
});
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}`);
});
client.on("messageCreate", (message) => {
if (message.content.includes("!brig")) {
const role = message.guild.roles.cache.find(
(role) => role.name === "BRIG"
);
const target = message.mentions.members.first();
target.roles.add(role.id);
}
});
client.login("YOURTOKEN");
Edit: This code is working with discord.js#13.5.0 but I can't see why it wouldn't work with the latest version (13.6.0 at this moment).
Now I believe this is a bit of an older version of discord.js but I'm not sure. I'm attempting to add a "Muted" role when you say a certain word, which in this case is "Heck". In the console I'm getting and error saying "TypeError: Cannot read property 'add' of undefined" and I'm not sure what to do here. Here is my code:
client.on('message', (message) => {
if(message.content.includes('Heck')) {
message.reply('Not allowed');
let role = message.guild.roles.cache.find(role => role.name === "Muted");
console.log('works')
let member = message.mentions.members.first();
console.log('works')
member.roles.add(role)
console.log('works')
}
});
Bot can't find Muted role in guild or there is no member mention to add role
client.on('message', (message) => {
if(message.content.includes('Heck')) {
message.reply('Not allowed');
let role = message.guild.roles.cache.find(role => role.name === "Muted");
if (!role) return console.log("no role found");
console.log('works')
let member = message.mentions.members.first();
if (!member) return console.log("no member mentioned");
console.log('works')
member.roles.add(role)
console.log('works')
}
});
This will log no role found if Muted role not found in guild and if role exists & no member mention then it will log no member mentioned in console
Here's a solution without having a mute role
client.on('message', (message) => {
if(message.content.includes('Heck')) {
let member = message.member;
for (var [snowflake, channel] of msg.guild.channels.cache) {
channel.overwritePermissions(member, { SEND_MESSAGES: false })
.then()
.catch(console.error);
}
}
}
I would like to log when my bot joins servers (i already have the base code) I just need to know how to define the owner of the server! if you know how to do this please help me!
Well, from the "guildCreate" event, you recieve a Guild class. From which you can access the owner GuildMember class. Then you can get the information about the owner that.
Here is an example:
const Discord = require("discord.js");
const client = new Discord.Client();
client.on("guildCreate", (guild) => {
console.log(`I have been added to a new guild! ${guild.name}
Owner name: ${guild.owner.user.username}`);
})
client.login("yourToken")
It's the best to read thru the docs.
For checking who invited :
const Discord = require("discord.js");
const client = new Discord.Client();
client.on("guildCreate", async guild => {
const fetchedLogs = await guild.fetchAuditLogs({
limit: 1,
type: "BOT_ADD"
});
const auditlog = fetchedLogs.entries.first();
let myChannel = client.channels.cache.find(channel => channel.id == YOUR_CHANNEL_ID);
myChannel.send(auditlog.executor.tag + " added bot in "+ guild.name+"\nOwner is:
"guild.owner.user.username);
});
Also you can check for VIEW_AUDIT_LOGS permission
I am creating a bot for discord and I try to create a command to be able to play a role, already in several places but I can't understand it well
guild.createRole({
name: 'role',
color: 'RED'
});
putting this code tells me "guild is not defined"
if you could help me I would be very grateful
You have to define the guild to be able to call methods on it. Here are a few ways you can define the guild and create a role
Retrieve the guild from a message
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('message', (message) => {
let guild = message.guild;
guild.createRole(...) ///put in your data here
.then(role => console.log(`Created new role with name ${role.name}`)) //What to do when it has been created
.catch(console.error); //Handle an error
}
Get the guild from guild id
const Discord = require('discord.js');
const client = new Discord.Client();
client.on("ready", () => {
let guild = client.guilds.get("GUILD-ID"); //Replace "GUILD-ID" with the id of the guild you want
guild.createRole(...) ///put in your data here
.then(role => console.log(`Created new role with name ${role.name}`)) //What to do when it has been created
.catch(console.error); //Handle an error
}