Discord.js: Verify channel - node.js

I'm trying to make my own bot for my server, for now i'm focusing on the verify. By acting to the check mark emoji it'll add the verified role, and then it should remove only the user reaction, but instead it'll remove every reaction right away
client.on('messageReactionAdd', async (reactionReaction, user) => {
const message = reactionReaction.message;
const verifyChannel = message.guild.channels.cache.find(c => c.name === 'approvazione');
const member = message.guild.members.cache.get(user.id);
if (member.user.bot) return;
const verify = message.guild.roles.cache.get('728000975046180988');
if (reactionReaction.emoji.name === '✅' && message.channel.id === verifyChannel.id) {
member.roles.add(verify).catch(console.error);
await reactionReaction.remove(member).catch(console.error);
}
here is the message sent by the bot with it's own reaction
and here is the same message after i reacted, and both mine and the bot reaction are removed, i just want my reaction to be removed

If you look at the docs it takes no parameter for the user:
https://discord.js.org/#/docs/main/stable/class/MessageReaction?scrollTo=remove
This was changed in v12, the method now is to use .users.remove:
reactionReaction.users.remove(member);

Related

How edit messages in Discord.js V12 using message ID

There is the way to edit a message on DiscordJS
To do this, we just need to pass it to the function, the client variable.
exports.myNewFunction = async function myNewTestFunction(client)
First, in order to edit a message using the Discord.JS API, we will need to find the server's GuildID.
const guild = client.guilds.cache.get(`Your Guild ID`);
Now, we're going to need to find the channel the message is on. For this, we will use the channel ID
const channel = guild.channels.cache.find(c => c.id === `Your message Channel` && c.type === 'text');
Finally, let's go to the editing part of the message. For this, we will only need to provide the ID of the message that you want to be edited.
channel.messages.fetch(`Your Message ID`).then(message => {
message.edit("New message Text");
}).catch(err => {
console.error(err);
});

Discord.js get user by nickname

I'm having a hard time figuring out
Is is possible to find a user by his or her nickname via the discord.js npm package.
How can I query a user by his or her nickname?
i've tried several things but nothing returns the nickname and I can't quite figure out how their documentation works.
So far I haven't been able to tell how to do it.
I've done as so in my code.
const { Client } = require('discord.js')
const discordClient = new Client()
discordClient.on('message', message => {
if (message.author.bot) return
if (message.content.startsWith('!gpwarn')) {
// The command is something like '!gpwarn thomas'.
// If his nick name is thomas but his username is john it doesn't work.
})
Yes, and it's pretty simple. Do this:
const user = client.users.cache.find(user => user.username == "The user's name");
Reference:
UserManager
To meet your exact requirements you would search the guild members cache for the provided nickname. However, I personally would suggest using either a direct tag or a UserID for this, as multiple people can have the same nickname in the server.
const U = message.guild.members.cache.find(E => E.nickname === 'NICKNAME')
Getting by Tag:
const U = message.mentions.members.first()
Getting by ID:
const U = message.guild.members.cache.find(U => U.id === 'IDHERE')
First you need to identify the nickname argument, you can do this by splitting, slicing, and joining message.content.
Next I recommend you fetch all of the GuildMembers from the guild using GuildMemberManager#fetch(), This way you don't run into the error of a member being uncached.
Handle the promise using async/await and use Collection#find() to return the member.
const { Client } = require('discord.js')
const discordClient = new Client()
discordClient.on('message', async message => {
if (message.author.bot) return
if (message.content.startsWith('!gpwarn')) {
// Returns all of the text after !gpwarn
const query = message.content.split(' ').slice(1).join(' ').toLowerCase()
const members = await message.guild.members.fetch()
const memberToWarn = members.find(m => m.nickname.toLowerCase() === query)
if (!memberToWarn) // No member found error
console.log(memberToWarn) // [GuildMember]
}
})

Why is my reactioncollector working only for me

I am currently trying to create a bot that adds a verification system that adds a specific role to the user upon reacting. This works, but when another user tries it does not work. Even removing the reaction upon reacting only works for me. Bear in mind, I have only just got back into the discord bot game so I am a little rusty. Below is the snippet of code, it just does not work for other users, but does for me. This is written in NodeJS - discord.js
if (message.content.startsWith(config.prefix + 'verify')) {
message.delete()
const embed = new MessageEmbed()
.setColor(config.embedcolor)
.setTitle('✅ Verification Board ✅')
.setFooter('Copyright © 2020 History GFX', 'https://i.imgur.com/1HkdOVY.png')
.setDescription('')
message.channel.send(embed).then(async message => {
await message.react('✅');
const collector = message.createReactionCollector((reaction, user) => user !== client.user)
collector.on("collect", (reactionCollect, user) => {
if(reactionCollect.emoji.name === "✅"){
let verify = message.guild.roles.cache.find(r => r.name === "🍃 Client");
message.guild.member(user).roles.add(verify);
reactionCollect.users.remove(user);
}
});
})
};
Thank you for your time!

Is there a way to get invites from ALL the guilds my bot is in?

Any ideas on how to get invites from a discord you aren't in but my bot is in?
I guess you want to get invited to every guild your bot is in
client.guilds.cache.forEach(guild => {
guild.channels.cache.filter(x => x.type != "category").random().createInvite()
.then(inv => console.log(`${guild.name} | ${inv.url}`));
});
Rez's answer is a common one but not a secure one, the random channel you get could be a channel you don't have permission to create an invite in.
This approach is safer, (might be a bit slower since async/await):
(This needs to be in an async function)
const invites = [];
//cant use async inside of forEach
//https://www.coreycleary.me/why-does-async-await-in-a-foreach-not-actually-await/
for (const [guildID, guild] of client.guilds.cache) {
//if no invite was able to be created or fetched default to string
let invite = "No invite";
//fetch already made invites first
const fetch = await guild.fetchInvites().catch(() => undefined);
//if fetch worked and there is atleast one invite
if (fetch && fetch.size) {
invite = fetch.first().url;
invites.push({ name: guild.name, invite });
continue;
}
for (const [channelID, channel] of guild.channels.cache) {
//only execute if we don't already have invite and if the channel is not a category
if (!invite && channel.createInvite) {
const attempt = await channel.createInvite().catch(() => undefined);
if (attempt) {
invite = attempt.url;
}
}
}
invites.push({ name: guild.name, invite });
}
console.log(invites)

Check if the user adding a reaction has a role

I want to check if the user reacting to a message has a role or not, so that if the user dont have a rank the bot will ignore the rest of the command, but I dont know how.
example: if a user with the admin rank reacted with :eggsa: emoji, the bot would continue with the command, but if i only had member rank the bot would ignore me.
client.on('messageReactionAdd', reaction => {
const eggsa = client.emojis.find(emoji => emoji.name === "eggsa");
if (reaction.emoji.name === 'eggsa') {
const message = reaction.message;
const kanal = reaction.message.guild.channels.find('name', 'sitater');
var embed = new Discord.RichEmbed()
.setAuthor(reaction.message.content)
.setTimestamp()
.setFooter(reaction.message.author.username, reaction.message.author.avatarURL)
kanal.send({embed});
}
});
the code works in this stage, only everyone can use it
I would be really grateful if someone could help me >:)
If you look at the messageReactionAdd docs you can see that along with the reaction, you can also get the user which added the reaction. So instead of
client.on('messageReactionAdd', reaction => {
You'd have
client.on('messageReactionAdd', (reaction, user) => {
So then you have the user and which is of type User. Because the user isn't of type GuildMember you first need to fetch the correct Guild member before you can check his/her role.
The easiest way to do this is by getting the Message to which the reaction was added with const msg = reaction.message;. Then you can get the guild from the message with const guild = msg.guild;. Now you can access the guild members with const guildMembers = guild.members;. Lastly you'd need to find the correct member with const guildMember = guildMembers.get(user.id);.
Now that you have your Guild Member you can access his/her Roles and thus check whether he/she has or does not have a certain role

Resources