I'm trying to make a command where you can set up an LFG role (LFG = looking for game).
Right now, I need the bot to find a role by its name, but it doesn't work. I have no idea why, I have tried many other thing like finding the role by its ID or structuring the code differently but nothing... Heres is the code:
collector1.on('collect', () => {
// Si l'utilisateur a cliqué sur 1️⃣ LFG
message.reactions.removeAll();
const embed1 = new MessageEmbed()
.setTitle(
`**--------------------LFG role configuration--------------------**`,
)
.addField(
`You clicked on 1️⃣`,
`Send in the next 30 the name of the new LFG role.`,
true,
)
.addField(
`Missclicked?`,
`Wait 30 seconds for the bot to send a timeout message and try again.`,
true,
);
let filter = (m) => m.author.id === message.author.id;
m.edit(embed1).then(() => {
message.channel
.awaitMessages(filter, {
max: 1,
time: 30000,
errors: ['time'],
})
.then((message) => {
message.reactions.removeAll();
message = message.first();
let role = message.guild.roles.cache.find((r) => r.name === message);
message.channel.send(`Alright, The new lfg role is ${role}!`);
})
.catch((collected) => {
message.channel.send('Timeout.');
});
});
});
Also, I have a second problem which is the bot doesn't remove all the reactions.
message is not a string- it's a Message object. I think you meant message.content
let role = message.guild.roles.cache.find((r) => r.name === message.content);
Also, I have a second problem which is the bot doesn't remove all the reactions
Maybe you meant m.reactions.removeAll()?
To find a role by name you can simply use
message.guild.roles.find(role => role.name === "Rolename");
Related
This question already has an answer here:
How to use awaitReactions in guildMemberAdd
(1 answer)
Closed 1 year ago.
I'm trying to create a calendar and depending on the emoji which one we react something happens but I don't find the good function . I try to find in another post but nothing helped me.
Thanks for your help.
This is the code :
if (message.content.startsWith('!agenda')){
var embed = new Discord.MessageEmbed()
.setColor('YELLOW')
.setTitle('Matter')
.addFields(
{name : 'Math :', value: '📏'},
)
var Msg = await message.channel.send(embed);
Msg.react("📏");
var emoji = await Msg.awaitReactions;
if (emoji === '📏'){
message.channel.send('test')
}
}
})
Here is what you are looking for:
message.channel.send(embed).then((m) => {
m.react('📏'); //reacts with 📏
const filter = (reaction, user) => {
return user.id != 'put bot id here' || user.id === message.author.id && reaction.emoji.name === '📏';
};
//this filter is to make sure only the user that called the command can react, and the only emoji collected is 📏
m.awaitReactions(filter, { max: 1, time: 15000, errors: ['time'] })
.then(collected => {
if (collected.first().emoji.name === '📏') {
m.channel.send(`:white_check_mark: you reacted with "📏"`).then(m => m.delete({ timeout: 3000 }));
//when the user reacts with 📏 - this code is executed
} else {
//if the user reacts with any other emoji - remove if you dont want this
m.channel.send(":x: Command cancelled").then(m => m.delete({ timeout: 3000 }));
};
})
.catch(collected => {
//if the user does not react in time
m.channel.send(":x: Command cancelled").then(m => m.delete({ timeout: 3000 }));
});
});
please ask questions if you're confused about any part.
Basically this code, if user gave a role or lost a role log this condition
client.on('guildMemberUpdate', (oldMember, newMember) => {
let channel = oldMember.guild.channels.cache.find(channel => channel.name === "member-log");
let addedRoles = newMember.roles.cache.filter(role => !oldMember.roles.cache.has(role.id));
let removedRoles = oldMember.roles.cache.filter(role => !newMember.roles.cache.has(role.id));
if (removedRoles.size > 0) {
const removeRoleEmbed = new Discord.MessageEmbed()
.setColor('#FF0000')
.setAuthor(`${oldMember.user.tag}` , oldMember.user.avatarURL())
.setDescription(`<#!${oldMember.id}> kişisinden <#&${removedRoles.map(r => r.id)}> rolü alındı.`)
.setTimestamp()
.setFooter(`Kullanıcı ID : ${oldMember.id}`)
channel.send(removeRoleEmbed)
}
if (addedRoles.size > 0) {
const addRoleEmbed = new Discord.MessageEmbed()
.setColor('#80FF00')
.setAuthor(`${oldMember.user.tag}` , oldMember.user.avatarURL())
.setDescription(`<#!${oldMember.id}> kişisine <#&${addedRoles.map(r => r.id)}> rolü verildi.`)
.setTimestamp()
.setFooter(`Kullanıcı ID : ${oldMember.id}`)
channel.send(addRoleEmbed)
}
});
I have this code and i want who give role a user
Note: Sorry for my english and thank in advance
For that, I believe you need to look at the Audit logs. Now, in Djs v12, you can check audit logs easily. You're looking for the MEMBER_ROLE_UPDATE action. Here are the list of all possible actions.
Here's from the Guide on how to listen to Audit Logs:
https://discordjs.guide/popular-topics/audit-logs.html
(Assuming async function)
const log = await message.guild.fetchAuditLogs({ limit: 1, type: 'MEMBER_ROLE_UPDATE' }).then(logs => logs.entries.first());
// This should log the id of the person who gave out that role.
console.log(log.executor.id);
That's it.
This is the current code that I have, I would like to make it where if you have a certain role then you can bypass the cooldown, also if anyone knows how to make a command that restricts a certain command to a certain channel, instead of having this really long message.channel.id.
const Discord = require('discord.js');
const fetch = require('node-fetch');
const talkedRecently = new Set();
module.exports.run = async(client, message, args, queue, searcher, ) => {
if (talkedRecently.has(message.author.id)) {
message.channel.send("Wait 1 minute before getting typing this again. " +'<#'+ message.author.id + '>');
} else {
switch(args[0].toLowerCase()){
case 'neko':
if(message.channel.id === '739002385531404288'||
message.channel.id === '646849145289834506'||
message.channel.id === '785079847763574794'||
message.channel.id === '782891383361896469'||
message.channel.id === '784417039425994772'){
fetch('https://nekos.life/api/v2/img/lewd')
.then(res => res.json())
.then(json => {
let nekoEmbed = new Discord.MessageEmbed()
.setTitle('Lewd Nekos! (=^・ω・^=)')
.setImage(json.url)
message.channel.send(nekoEmbed)
})
}else{
return}}
talkedRecently.add(message.author.id);
setTimeout(() => {
talkedRecently.delete(message.author.id);
}, 60000);
}
}
module.exports.config = {
name: "hentai",
aliases: ['ht']
}
```
Answering Your First Question:
Simply check if the member has a certain role. If they do, construct your if statement so that it will not fire if they have that role
Make sure to use message.member when checking roles
if (talkedRecently.has(message.author.id) && !message.member.roles.cache.has('bypass role id here')) {
// Your cooldown message
}
Learn more about Roles#has
Answering your 2nd question:
You can have an array of channel id's then use includes to check if any of the id's in the array match the current channel id
const ids = ['id1', 'id2', 'id3', 'id4'] // And so on
if (ids.includes(message.channel.id)) {
// Your Code
}
Learn more about Array.prototype.includes
Is there anything remotely wrong with this code?
no errors pop up and there isn't a reaction once the bot sends the messages.
any help would be appreciated, thank you in advance!
const a = msg.guild.roles.get('666712822985654322'); //Verified User
// the role constants are in the same chronological order as below.
const filter = (reaction,user) => ['668236468384169986'].includes(reaction.emoji.name);
const embed = new Discord.RichEmbed()
.setColor(0x00FF00)
.setTitle('Rules')
.setDescription(`
In order to gain access to the rest of the server you must read and abide by these rules:
By reacting with :white_check_mark: you agree to these rules
Roles:
:white_check_mark: ${a.toString()}`)
.setThumbnail(msg.author.avatarURL)
.addField('Rule #1:You do not talk about fight club', 'Second Rule: You do not TALK about fight club')
.setFooter("Use \'!command list\' to get aquainted with Peb 3000");
msg.channel.send(embed).then(async message => {
await message.react('668236468384169986'); //white check mark
message.awaitReaction(filter, {})
.then(collected =>{
const reaction = collected.first();
switch(reaction.emoji.name) {
case('\:white_check_mark:'):
message.member.addRole(a).catch(err => {
console.log(err);
return message.channel.send(`Error adding you to this role: **${err.message}**`);
});
message.channel.send(`You have been added to the **${a.name}** role!`).then(m => m.delete(3000));
break;
}
}).catch(collected => {
return msg.collected.send(`I couldn't add you to this role!`)
})
});
I'd recommend reading both An Idiot's Guide - Using Emojis as well as Discord.js Guide - Reactions, as your current approach for Unicode emojis won't work.
In your case, :white_check_mark: should be ✅ or an equivalent.
The collected options will triggeret when collector end collect, you no provide any options to stop collector, so its will never happen.
The 1 way to give member role on react follow this code. But when bot restart, you need again use this command and create collector.
const roleToAdd = msg.guild.roles.get('666712822985654322'); //Verified Role
if(!roleToAdd) return
let embed = new Discord.RichEmbed()
.setColor(0x00FF00)
.setTitle('Rules')
.setDescription(`
In order to gain access to the rest of the server you must read and abide by these rules:
By reacting with :white_check_mark: you agree to these rules
Roles:
:white_check_mark: ${a.toString()}`)
.setThumbnail(msg.author.avatarURL)
.addField('Rule #1:You do not talk about fight club', 'Second Rule: You do not TALK about fight club')
.setFooter("Use \'!command list\' to get aquainted with Peb 3000");
msg.channel.send(embed).then(message => {
const filter = (reaction, user) => reaction.emoji.name === '✅' && user.id === msg.author.id && reaction.message.id = message.id
const collector = message.createReactionCollector(filter, {});
collector.on('collect', r => {
if(r.emoji.name === '✅') {
let member = message.guild.members.get(reaction.users.last().id)
member.addRole(roleToAdd)
.then( member => {
message.channel.send(`You have been added to the **${roleToAdd.name}** role!`).then(m => m.delete(3000));
})
.catch(console.error)
}
})
collector.on('end', collected => console.log(`Collected ${collected.size} items`));
})
The way number 2 its listen reactionadd event. After restart its will work.
bot.on('messageReactionAdd', (reaction, user) => {
if(reaction.message.id === 'YOUR MESSAGE ID' && reaction.emoji.name === 'youreactionname') {
let meber = reaction.message.guild.members.get(user.id)
member.addRole('yourRole')
}
.catch(console.error)
});
I'm making a verification system that if you send a message. In a different channel, an embed shows up with 2 emoji's: 1 to accept, and 1 to deny. The .awaitReaction has to be triggered by a different user then the author. But when I change the filter. It triggers the message if the bot reacts to it. How can I fix this?
Here is my code:
let register = args.join(" ").slice(7)
const filter = (reaction, user) => ["✅", "❌"].includes(reaction.emoji.name) && !bot.user;
let test = new Discord.RichEmbed()
.addField("User:", message.author.username && message.author.tag, true)
.addField("Requested nickname:", register)
.setColor("#ed0c75")
.setImage(message.author.displayAvatarURL)
let acceptordeny = message.guild.channels.find(`name`, "accept-or-deny");
if(!acceptordeny) return message.channel.send("Can't find accept or deny channel.");
acceptordeny.send(test).then(async message => {
await message.react("✅")
await message.react("❌")
message.awaitReactions(filter, {
max: 1,
time: 60000,
errors: ["time"]
}).then(collected => {
const reaction = collected.first();
switch (reaction.emoji.name) {
case "✅":
console.log("Accepted")
break;
case '❌':
console.log("Denied")
break;
}
}).catch(collected => {
return acceptordeny.send("Failed")
})
})
Hope someone can help me with this.
Update the filter with this one:
const filter = (reaction, user) => ["✅", "❌"].includes(reaction.emoji.name) && user.id !== message.client.user.id;
It will check if the id of the user who reacted to the message is the same as the id and if, cancel.