Remove a users reaction from fetchMessage? - Discord JS - node.js

I have a question for Discord.js.
How would I remove a specific user's reaction from a message?
I have tried to do so with this code:
// Now known as 'messages.fetch'.
message.channel.fetchMessage(MessageID).then(m => {
m.reactions.remove(UserID);
});
But it doesn't remove the user's reaction at all. Am I doing something wrong?
Any help would be appreciated.

Since this question is getting a lot of attraction, I decided to post what worked for me
Remove Specific User's Specific Reaction
// Channel = the channel object of the message's original channel
// MessageID = ID of the message, if hard coding, put around quotations eg: "1234"
const msg = await channel.messages.fetch(MessageID);
msg.reactions.resolve("REACTION EMOJI,
REACTION OBJECT OR REACTION ID").users.remove("ID OR OBJECT OF USER TO REMOVE");
Note: If using an older version of discord.js, simply replace channel.messages.fetch with channel.fetchMessage

The chosen option works, I would like to explain it. You need to grab the MessageReaction object from the message for the reaction you want to remove. How you do that depends on your code, in my case I was working with a reaction collector so on my collect call I already had the MessageReaction.
You then need to access the users property (a ReactionUserManager object). With this object, you can call .remove().
Code is below:
collector.on('collect', async (reaction, user) => {
...
// delete the reaciton
reaction.users.remove(user.id);
});

message.reactions is a collection of messageReactions. I think you need to loop through the collection and then remove the messageReaction required.
message.channel.fetchMessage(messageID).map(r => r).then(message => {
message.reactions.forEach(reaction => reaction.remove(UserID))
})

If you look at the documentation for reactions you can see it is a Collection, and it mentions that they are mapped by reaction ID's , and not the user ID's. The way you could remove them is get the reaction, filter the users and then maybe do something with that? I'm not sure how to remove those specifically, but that should get you the users, then filter that to the ID you want.
message.channel.fetchMessage(messageID).then(msg = m.reactions.get(reactionID).users); // Gets the users that reacted to a certain emote, I think.

Related

Joined member avatar displayed in thumbnail of embed - Discord.js

So I've created a message embed that get's sent to a channel whenever a member joins the server. I used the following code for this.
client.on('guildMemberAdd', guildMember => {
let welcomeRole = guildMember.guild.roles.cache.find(role => role.name === 'ROLE');
guildMember.roles.add(welcomeRole);
let welcomeEmbed = new Discord.MessageEmbed()
.setColor('#202225')
.setTitle('New member spotted!')
.setDescription(`Welcome ${guildMember}!`)
.setThumbnail(guildMember.displayAvatarURL())
guildMember.guild.channels.cache.get('CHANNEL_ID').send(welcomeEmbed);
So the user's avatar doesn't show, although a normal image does of course. I'm suspecting it's because I'm not using the user class, could be something else, I have no idea.
My question is then, how can I display the joined user's avatar in the embed's thumbnail?
I would recommend doing:
.setThumbnail(guildMember.user.displayAvatarURL({ dynamic: true }))
This will show the users avatar and if it's animated, it will be displayed animated.
You cannot get the avatar of a GuildMember object, but would first have to convert it into a User object.
This can be simply done using:
.setThumbnail(guildMember.user.displayAvatarURL())

How to update user permission discord js

So, I want to update a permission for some users.
First, I tried to create a text channel and overwrite a user Permission. Then I do a for loop to update the permission, but I dont know how.
Things I've tried to update the permission :
channel.overwritePermissions({
permissionOverwrites: [
{
id: guild.id,
deny: ['VIEW_CHANNEL'],
},
{
id: playerData[i],
allow: ['VIEW_CHANNEL'],
},
],
});
//it said channel not defined?
message.guild.channels.cache.find('🐺werewolves').overwritePermissions({
permissionOverwrites: [
{
id: playerData[i],
allow:['VIEW_CHANNEL'],
}
]
})
//it said : fn is not a function
I've I have seen this solution and read the documentation, but the instruction isn't clear.
PS : The permission update must be a loop because the number of users who get the permission always changing.
Regarding first codeblock
channel is not defined, because it's not defined. You cannot use variables without defining them, that's how JavaScript works. To solve it, you would have to define it using for example const channel = ..., or access it from other variables you have, just like you are trying to use find in your second codeblock - you access it from message, as you're most likely in the message event.
Regarding second codeblock
This is not a proper way to use find, neither in old - removed in your version - way, nor the new way. The old way was to do find(property, value), to which you wouldn't provide a value ('🐺werewolves' would be treated as property you're trying to search by). New way you have to use, allows way more flexibility by requiring to pass it a function, just like in the example for the method. Since what you passed was a string and not a function, internal code throws an error fn is not a function.
For your example above, correct way to use that find would be
message.guild.channels.cache.find(channel => channel.name === '🐺werewolves');
Additionally, note that ideally you shouldn't try to call any methods on that directly, as in case when no channel with that name would be found, your code will throw an error. Snippet below should avoid that possibility.
const channel = message.guild.channels.cache.find(channel => channel.name === '🐺werewolves');
if (channel) channel.overwritePermissions(...)

Finding if someone got a new role on discord.js

I would like to give a suffix every members on my server. The nickname should be for example Name (role).
I tried the simple
client.on("guildMemberUpdate", function(oldMember, newMember){
//Here the setting
});
But it wasn't good. Can you help me?
I think, it should be like this:
client on guildMemberUpdate:
check if someone's role is xy:
give him a suffix, like name (role).
If you are assigning the role programatically it would be easier if you handle the name change of the role in the same part of the code since you already know which role is the new one, if this is not the case you are right for using the event, what you want is to find the new role that has been added and change the name of the member after
client.on("guildMemberUpdate", (oldMember, newMember) => {
// Get the role which has been added
const newRole = newMember.roles.cache
.filter(r => !oldMember.roles.cache.has(r.id))
.first()
// Change username
newMember.setNickname(`${newMember.displayName} (${newRole.name})`)
})
See Collection methods

Announce a members role ADDED to a specific role in a specific channel

I'd like to have my bot announce in our specific channel called family-talk, which I do have the channel ID of as well but not sure where to put it, but I'd want this to only happen when a role has been added to a member, is my below code correct or wrong? I don't have a lot of good ways of testing this so i'm hoping for some big help here. I also would like to know where the best place would be to place the code. Thank you!
if(!oldMember.roles.has('539208166563643407') && newMember.roles.has('561773668439687179'))
client.channels.get("550197572178935809").send("This member got the special role!");
Your code should work, BUT you have 2 diffrent IDs in the if, so to make this a bit cleaner just do:
const roleID = '539208166563643407';
const channelID = '550197572178935809';
client.on('guildMemberUpdate', (oldMember, newMember) => {
if(!oldMember.roles.has(roleID) && newMember.roles.has(roleID)) {
client.channels.get(channelID).send(newMember.displayName + ' got the special role!');
}
});

How to loop through groups with now.js

is there a way to loop through a user's groups with now.js?
When a user disconnects, I want to run some functions on the groups that they were a part of.
something like answer 1 here (1), but for some reason, that code doesn't work.
Code:
nowjs.on('disconnect', function() {
var that = this;
this.getGroups(function(groups){
for (i=0;i<groups.length;i++){
nowjs.getGroup(groups[i]).removeUser(that.user.clientId);
console.log('user removed');
}
})
})
I forgot that I had already created an array called serverRoomsList. That held, oddly enough, a list of all the chat rooms I had already created. I just looped through that to get all of the group names.
However, I would still like to know if Now itself keeps track of groups in an array, or if I have to do it.

Resources