I am trying to make a command were it creates a role but gives me the error saying that guild is not defined:
guild.roles.create({
^
ReferenceError: guild is not defined
while this is my code:
guild.roles.create({
data: {
name: 'Boots',
color: 'BLUE',
},
reason: 'very cool',
})
.then(console.log)
.catch(console.error);
let role = message.guild.roles.find(r => r.name === "Boots");
member.roles.add(role)
message.channel.bulkDelete(1);
message.channel.send('Mhm, very cool');
You have to use
message.guild.roles [...]
Because if you type
guild.roles [...]
the program wants to access the roles property of the variable guild
Did you define guild? If you want to use guild, just write something like const guild = message.guild;
Related
I want to do a setup command so I can create a role and give it to myself via a command. The role is created but I cannot give myself the role. I always get an error code after the role was created.
My Code:
case 'setup':
if(!message.author.id == '416940852291045376') return message.channel.send('Youre not me')
message.guild.roles.create({
name: 'Bot dev',
color: '#0080FF',
permissions: 'ADMINISTRATOR'
})
.catch(console.error);
let role = message.guild.roles.cache.find(r => r.name === "Bot dev")
let member = message.member
member.roles.add(role)
The error code i'm getting:
C:\Discord Bots\Bot v13\node_modules\discord.js\src\managers\GuildMemberRoleManager.js:110
throw new TypeError('INVALID_TYPE', 'roles', 'Role, Snowflake or Array or Collection of Roles or Snowflakes');
^
TypeError [INVALID_TYPE]: Supplied roles is not a Role, Snowflake or Array or Collection of Roles or Snowflakes.
RoleManager#create() returns a promise with the newly created role, you must await it or resolve it before searching for the role.
message.guild.roles.create({
name: 'Bot dev',
color: '#0080FF',
permissions: 'ADMINISTRATOR'
})
.then(role => {
const { member } = message;
member.roles.add(role);
})
.catch(console.error);
I am trying to find if my bot has a specific role so it can do something else. For example:
client.user.roles.cache.find(r => r.name === 'specific role');
My error:
TypeError: Cannot read property 'cache' of undefined
Let's start of by stating that users do not have roles, therefore you have to fetch the guildMember.
Fetching the guild member is easy;
First of all, you will have to fetch the guild the user is in. For example:
var guild = client.guilds.cache.get('guild-id');
Secondly, you will have to find the user in that guild. For example:
var member = guild.members.cache.get('user-id');
Then you will be able to move forward and fetch the roles
member.roles.cache.find(r => r.name === 'role-name');
The full example:
const Discord = require('discord.js'); //Define discord.js
const client = new Discord.Client(); //Define the client
client.on('message' message => { //Event listener
//Do something here
var guild = client.guilds.cache.get(message.guild.id);
var member = guild.members.cache.get(client.user.id);
member.roles.cache.find(r => r.name === 'role-name');
});
client.login('token'); //Login
So maybe I didnt explain my problem very well but here is what I found working for me message.guild.me.roles.cache.find(r=> r.name === 'a role') thanks to everyone for helping me !!!
You need to resolve the user into a GuildMember, since users don't have roles.
To do that you need a Guild class,after that you can use the guild.member() method to convert it to a member
const guild = client.guilds.cache.get('id of the guild'),
member = guild.member(client.user.id);
member.roles.cache.find(r=> r.name === 'name')
Another issue you might run into here is, the member constant being undefined, this can be solved by using guild.members.fetch(id) or enabling the Privileged Member intent in the Discord Developer Portal
So when bot sends a certain message, it reacts to it too. Now m tryin to check a user who reacted to that reaction has a certain role or not but so far I haven't been able to do so.
bot.on('messageReactionAdd', (reaction, user) => {
//var member = reaction.message.guild.members.cache.find(member => member.id = user.id);
//if(message.member.roles.cache.some(role => role.name === 'Bronze')) return(message.channel.send("you dont have role."));
if (reaction.message.members.roles.cache.has('729452091168260188')) return(message.channel.send("you have the role"));
else { console.log("you dont have role"); }
})
You made a mistake in the following line of code:
reaction.message.members.roles.cache.has('729452091168260188')
The Message object has no property members, it should be member:
reaction.message.member.roles.cache.has('729452091168260188')
When I try
message.member.roles.add('695699359634817094');
I get the following error:
TypeError: Cannot read property 'add' of undefined
Is there a specific way to add my guild ID and update their role to that specific server through DM?
My function works within the guild by calling the command, however, through DM it doesn't.
You could do this by using message.author.id to get the user's ID and then using guild.get(guild_ID).members.fetch(user_ID) to access the GuildMember of that user.
You also mentioned that you give users the ability to run that command either in DM or a text channel in your guild.
If that is the case I would suggest adding a check to see if the command is being sent to a text channel or dm channel.
if (message.channel.type === "dm") {
const member = await client.guilds.get(guild_ID).members.fetch(message.author.id);
member.roles.add(role_ID);
} else {
message.member.roles.add('695699359634817094');
}
Ignore the if statement if you intend on having the command only run from dm.
By using the info given from Syntle and tipakA this is the solution.
if (message.channel.type === "dm") {
client.guilds.get('[SeverId]').members.fetch(message.author.id).then(async () => {
await client.guilds.get('[ServerId]').members.fetch(message.author.id).then((memberid) => {
memberid.roles.add('[roleid]');
}).catch((err) => {
console.log("ERROR");
});
});
}
else
{
message.member.roles.add('[roleid]');
}
I want to create a command as Zira Bot. If you don't know Zira, the command is reaction role. So, at reaction add, if reaction emoji is 'bla bla', add role 'bla bla'. But I need to learn the guild's id.
I have tried reaction.guild.id and user.guild.id, but it hasn't worked.
My code is:
Bot.on ('messageReactionAdd', (reaction, user) => {
const fs = require ('fs')
const reactionRole = JSON.parse(fs.readFileSync('./Util/Reaction Role.json'))
const reactionRoleEmoji = reactionRole[reaction.guild.id].reaction
const reactionRoleRole = reactionRole[reaction.guild.id].role
if (reaction.emoji.id === reactionRoleEmoji) user.addRole(reactionRoleRole)
})
It errors "the property "id" of undefined".
reaction has a message property, and message has guild.
reaction.message.guild.id