I was trying to make a command in discord.js in which the bot finds the highest role it can give to a person and gives it to the person.
const myrole = message.guild.me.roles.highest.rawPosition
const therole = message.guild.roles.cache.find(r => r.rawPosition = myrole-1)
const person = message.guild.member(client.users.cache.get("the id"))
person.roles.add(therole.id);
And I get the following error:
(node:18926) UnhandledPromiseRejectionWarning: DiscordAPIError: Unknown Role
at RequestHandler.execute (/rbd/pnpm-volume/d8568466-4a2a-4c3a-ac47-cee06cded9bb/node_modules/.registry.npmjs.org/discord.js/12.2.0/node_modules/discord.js/src/rest/RequestHandler.js:170:25)
at processTicksAndRejections (internal/process/task_queues.js:88:5)
Can anyone help me out with this?
Your current code is not checking if r.rawPosition is equal to myrole - 1 it is defining r.rawPosition as myrole - 1
const myrole = message.guild.me.roles.highest.rawPosition
const therole = message.guild.roles.cache.find(r => r.rawPosition === myrole-1)
const person = message.guild.member(client.users.cache.get("the id"))
person.roles.add(therole.id);
Related
I am using Web3.js with Node JS. I need to get the transaction details for a smart contract under BNB. I developed my codes, but no matter what I did, I couldn't get it to work. Can you help me? Where am I doing wrong?
.env
bnbProvider = https://bsc-dataseed1.binance.org:443
web3crypto.js
const Web3 = require('web3')
const thisWeb3 = new Web3(new Web3.providers.HttpProvider(process.env.bnbProvider))
const minimumABI = [ABI OBJECT]
const tb = new thisWeb3.eth.Contract(minimumABI, process.env.contractId)
my function
exports.checkTransaction = async transactionId => {
const transactionData = await thisWeb3.eth.getTransaction(transactionId)
console.log(transactionData)
return transactionData
}
and Error
Error: Returned error: invalid argument 0: hex string has length 40, want 64 for common.Hash
I'm trying to create a command where if you say a slash command with a use parameter then it will give that user the role. I keep receiving this error even though I know that the member exists.
TypeError: Cannot read properties of undefined (reading 'roles')
My code:
const { commandName, options } = interaction;
const user = options.getUser('user');
if (commandName == 'givebetatester'){
console.log(user);
const role = interaction.guild.roles.cache.get('917609388154425374');
interaction.reply('Success');
user.member.roles.add(role);
}
I've double-checked that I have the role and the user exist and I have no idea what's wrong at this point. Any help would be appreciated.
You can only go from a GuildMember to User and not the other way around. You're trying to go from a User to a GuildMember by using user.member
Either change your slash command options to accept a Member instead of a User
Or ensure you have the Guild Member's Intent enabled and fetch the GuildMember object with the User id:
// Async/Await
const member = await interaction.guild.members.fetch(user.id);
Fixed! I switched from user to mentionable which might break if someone tries to type something other than a role but it does the trick.
Code:
const { commandName, options } = interaction;
const user = options.getMentionable('user');
if (commandName == 'givebetatester'){
const role = interaction.guild.roles.cache.get('917609388154425374');
user.roles.add(role);
interaction.reply('<a:ncheckmark:917609071195074600>');
}
CODE
let role = message.guild.roles.cache.find(role => role.name === args[1]);
const member = message.mentions.members.first();
member.roles.remove(role).catch((e) => console.log(e));
Here args[1] is the name of the role.
This code was working before but now it shows this error.
ERROR:
TypeError [INVALID_TYPE]: Supplied roles is not an Array or Collection of Roles or Snowflakes.
at GuildMemberRoleManager.remove
[Symbol(code)]: 'INVALID_TYPE'
EDIT: role is UNDEFINED
When using the .remove(roleOrRoles, [reason)] method of GuildMemberRoleManager you need to ensure that you either pass an array of roles, collection of snowflakes or a snowflake as stated in the error that you have. Therefore the correct solution would be:
let role = message.guild.roles.cache.find(role => role.name === args[1]);
const member = message.mentions.members.first();
member.roles.remove(role.id).catch((e) => console.log(e));
so i'm coding a discord bot, and there's a command, this command know when the user changes is custom status, and if the special status is "hello" the bot will send a message in a specific channel, and add a role to the user. But the code gives my an error
Here's the code :
bot.on('presenceUpdate', (oldMember, newMember) => {
console.log(newMember.activities[0].state)
let guildChannels = newMember.guild.channels;
var Cha = guildChannels.cache.find(channel => channel.name === "general")
if(newMember.activities[0].state === "hello") {
Cha.send('hello !')
.then(msg => {
let GradeUser = bot.users.cache.find(user => user.id == newMember.userID);
let role = msg.guild.roles.cache.find(r => r.id == "742423876004216883");
GradeUser.roles.add('742423876004216883')
})
.catch(console.error)
}
});
And here's the error :
TypeError: Cannot read property 'add' of undefined
at C:\Users\Nathan\Desktop\Dev\Effectivement Bot\index.js:320:29
at processTicksAndRejections (internal/process/task_queues.js:93:5)
I just want to solve the error with the role ( add the role )
You need to use a GuildMember object, not a user object.
From discord.js guide
What is the difference between a User and a GuildMember?
A lot of users get confused as to what the difference between Users and GuildMembers is. The simple answer is that a User represents a global Discord user and a GuildMember represents a Discord user on a specific server. That means only GuildMembers can have permissions, roles, and nicknames, for example, because all of these things are server-bound information that could be different on each server that user is in.
Replace bot.users.cache.find() with msg.guild.members.cache.find()
i am currently working on some updates for my discord bot, I am using discord.js
and trying to get all the usersnames from a current role via dm,
For example, if 3 users have the role Admin,
then the 3 usernames will be returned via message,
so far i have this
bot.on('message', msg => {
if(msg.channel instanceof Discord.DMChannel)
{
if(msg.content == prefix + "des"){
let RoleName = "Admin";
let guildid = "idwashere";
let memberWithRole =
bot.guilds.get(guildid).roles.get("name",
RoleName).members;
console.log(memberWithRole);
msg.reply("Feature coming soon");
}
}
});
i get a error
let memberWithRole =
bot.guilds.get(guildid).roles.get("name",
RoleName).members;
^
TypeError: Cannot read property 'members' of
undefined
i feel i'm close but yet not sure what im doing wrong :)
Managed to fix it
i changed memberswithrole to
let memberWithRole = bot.guilds.get(guildid).roles.find("name", RoleName).members.map(m=>m.user.username);
this then returns the username
If anyone has any better way or imrpovents please let me know.