I have a code. this code replaces the letter "x" in the members' name with "y". but it sends the message I set for each user. that is, if there are 15 users, it sends the message "I changed the name of the x person" 15 times. I want to make this a single message then I want the bot to edit the message according to the user.
like this;
"I changed the name of member person" then "I changed the name of member2 person. (edited)"?
exports.run =async (bot, message, args) => {
let tokaci = message.guild.members.filter(membersx => {
return membersx.roles.some(r=>["639572199409319994"].includes(r.id));
});
let tokacis = message.guild.members.filter(membersx => {
return membersx.roles.some(r=>["tokuchi"].includes(r.name));
}).size;
tokaci.forEach(member => {
if(!member.displayName.includes(`✯`)) return message.channel.send(`<:reds:669706016375701574> **Belirtilen role sahip kişilerin isminde değiştirilmesi gereken harfi bulamadım.**`)
if(member.manageable) {
let newNickName = member.displayName.replace(`✯`, '⛥');
member.setNickname(newNickName)
.catch(console.error)
message.channel.send(`***<:onays:669706016354729984> Belirtilen role sahip olan kişilerin ismindeki harfi değiştiriyorum. ${member}***`)
}
});
}
You can use another function to edit message or use message.edit. Method channel.send() return a promise with sended message.
exports.run =async (bot, message, args) => {
let tokaci = message.guild.members.filter(membersx => {
return membersx.roles.some(r=>["639572199409319994"].includes(r.id));
});
let tokacis = message.guild.members.filter(membersx => {
return membersx.roles.some(r=>["tokuchi"].includes(r.name));
}).size;
message.channel.send('Start edit nicknemes').then(msg => {
tokaci.forEach(member => {
if(!member.displayName.includes(`✯`)) return message.channel.send(`<:reds:669706016375701574> **Belirtilen role sahip kişilerin isminde değiştirilmesi gereken harfi bulamadım.**`)
if(member.manageable) {
let newNickName = member.displayName.replace(`✯`, '⛥');
member.setNickname(newNickName)
.catch(console.error)
let text = `***<:onays:669706016354729984> Belirtilen role sahip olan kişilerin ismindeki harfi değiştiriyorum. ${member}***`
editMsg(msg,text)
}
});
})
}
function editMsg(message, text) {
message.edit(text)
}
V2
exports.run =async (bot, message, args) => {
let tokaci = message.guild.members.filter(membersx => {
return membersx.roles.some(r=>["639572199409319994"].includes(r.id));
});
let tokacis = message.guild.members.filter(membersx => {
return membersx.roles.some(r=>["tokuchi"].includes(r.name));
}).size;
message.channel.send('Start edit nicknemes').then(msg => {
tokaci.forEach(member => {
if(!member.displayName.includes(`✯`)) {
editMsg(msg,`***<:onays:669706016354729984> Belirtilen role sahip olan kişilerin ismindeki harfi değiştiriyorum. ${member}***`)
}
if(member.manageable) {
let newNickName = member.displayName.replace(`✯`, '⛥');
member.setNickname(newNickName)
.catch(console.error)
let text = `***<:onays:669706016354729984> Belirtilen role sahip olan kişilerin ismindeki harfi değiştiriyorum. ${member}***`
editMsg(msg,text)
}
});
})
}
function editMsg(message, text) {
message.edit(text)
}
Related
I want to make a command that tags a user when there name got mentoined. But now the bot only tags the username and not a nickname the person has in the server
client.on("message", message => {
if(message.content === 'test'){
message.channel.send("works")
console.log("logged the message")
}
})
client.on("message", message => {
const list = message.guild;
list.members.cache.each(member => {
pinging = member.user.id
console.log(member.user.username)
if(message.content.includes(member.user.username)){
message.channel.send(`<#${pinging}>`)
}
})
})
can anyone help?
First thing, you only want to have one listener for the message event, so try this code:
client.on("message", message => {
if (message.content === 'test'){
message.channel.send("works")
console.log("logged the message")
}
// insert choices from below here
})
If you want it to listen for a member being tagged
const mentionedMember = message.mentions.members.first()
if (mentionedMember){
message.channel.send(`${mentionedMember}`)
}
Now if you want it to listen for username instead of #username
const args = message.content.slice().trim().split(' ')
const guild = message.guild
args.forEach(arg => {
const member = guild.members.cache.find(member => member.user.username.toLowerCase() === arg.toLowerCase())
if (member) {
message.channel.send(`${member}`)
} else {
return
}
})
To listen for displayName
const args = message.content.slice().trim().split(' ')
const guild = message.guild
args.forEach(arg => {
const member = guild.members.cache.find(member => member.displayName.toLowerCase() === arg.toLowerCase())
if (member) {
message.channel.send(`${member}`)
} else {
return
}
})
So I made this poll command that people without admin could make polls. To prevent spam, there is a verify step for everyone without admin. But when you react to the poll to verify it, it only works for the person making the poll. And not the admin that's supposed to check if it's not spam.
So when the admin reacts nothing happens but when the person that made the poll reacts to it, it verifies the poll and sends it to the main channel.
Code is down below is someone could help! 'Appreciate it!
const {Client, Collection, GuildMember, User, MessageEmbed, Message} = require("discord.js");
const ms = require("ms");
const delay = (msec) => new Promise((resolve) => setTimeout(resolve, msec));
module.exports.run = async(client, message, args, user, reaction) => {
var woord = '!poll'
var question = args.slice(0).join(' ')
var poll = new MessageEmbed()
.setTitle(`${message.author.username} wil een poll maken.`)
.setDescription(question)
.setColor('#eb8dd8')
.setFooter(`Poll gemaakt door: `+ message.author.username)
var success = new MessageEmbed()
.setDescription(question)
.setColor('#eb8dd8')
.setFooter("Poll started door: "+ message.author.username)
if(message.content.includes(woord)) {
message.delete({timeout:0})
}
if(!message.member.roles.cache.some(r => r.name === 'Barman')) {
if(message.channel.name.includes("🙇-poll")) {
if(args[0]) {
message.delete()
message.guild.channels.create(message.author.username, { permissionOverwrites:[
{
deny: 'VIEW_CHANNEL',
id: message.guild.id
},
{
allow: 'VIEW_CHANNEL',
id: message.author.id
},
],
}).then(channel => {
channel.send(poll).then(poll => {
poll.react('✅')
.then(() => poll.react('❌'));
})
})
} else {
message.delete()
}
}
} else {
var pollChannel = client.channels.cache.get('876531134702448701')
pollChannel.send(success).then(success => {
success.react('✅')
.then(() => success.react('❌'))
})
}
client.on('messageReactionAdd', (reaction, user) => {
const deleteChannel = message.guild.channels.cache.find(channel => channel.name.toLowerCase() === user.username);
var pollChannel = client.channels.cache.get('876531134702448701')
if(reaction.emoji.name === '✅') {
if(message.guild.channels.cache.find(channel => channel.name.toLowerCase() === user.username)) {
deleteChannel.delete()
.then(channel => {
pollChannel.send(success).then(success =>{
success.react('✅')
.then(() => success.react('❌'))
})
})
}
} if(reaction.emoji.name === '❌') {
if(message.guild.channels.cache.find(channel => channel.name.toLowerCase() === user.username)) {
deleteChannel.delete()
}
}
})
}
module.exports.help = {
name: "poll"
}
At the start of your function you can do this:
const questions = new Collection();
questions.set("What is the color of healthy grass?", "green");
questions.set("How many vowels are there in these letters: apple", "2");
const chosen = questions.randomKey()
await message.channel.send(`Please answer the question. This is to prevent spam. Make sure your spelling is correct. You have 30 seconds —> ${chosen}`)
try {
await message.channel.awaitMessages((m) => m.author.id === message.author.id && m.content.toLowerCase() === questions.get(chosen), {
time: 30000,
max: 1
})
} catch(err) {
return message.channel.send("You ran out of time to answer correctly!")
}
//code to make poll
Your awaitMessages syntax may be different on v13. You can also replace my questions, and add as many as you want!
i am having a few difficulties getting my kick command to only allow people with the permission node KICK_MEMBERS to kick people. currently i have the bot in a state where anyone and everyone is allowed to kick the following is my code.
const Discord = require('discord.js')
module.exports.run = async (bot, message, args) => {
const user = message.mentions.users.first();
if (user) {
const member = message.guild.member(user);
if (member) {
member
.kick('Optional reason that will display in the audit logs')
.then(() => {
message.reply(`Successfully kicked ${user.tag}`);
})
.catch(err => {
message.reply('I was unable to kick the member');
console.error(err);
});
} else {
message.reply("That user isn't in this guild!");
}
} else {
message.reply("You didn't mention the user to kick!");
}
};
module.exports.help = {
name: "kick"
}
GuildMember#hasPermission returns a boolean of whether a user has a specified permission or not. We can use it to declare if the user has the KICK_MEMBERS permission.
Final Code
const Discord = require('discord.js')
module.exports.run = async (bot, message, args) => {
if (!message.member.hasPermission('KICK_MEMBERS', { checkAdmin: true, checkOwner: true })) return message.reply('You cannot use this command!')
// I like to have it so people with the administrator permission can use it regardless.
const user = message.mentions.users.first();
if (user) {
const member = message.guild.member(user);
if (member) {
member
.kick('Optional reason that will display in the audit logs')
.then(() => {
message.reply(`Successfully kicked ${user.tag}`);
})
.catch(err => {
message.reply('I was unable to kick the member');
console.error(err);
});
} else {
message.reply("That user isn't in this guild!");
}
} else {
message.reply("You didn't mention the user to kick!");
}
};
module.exports.help = {
name: "kick"
}
My bot can issue roles by mention, but I want it to be given by name (so that there's no need to ping the entire role. This is the syntax of the command: !role <#user> <role name>
I'm using discord.js#v12
const Discord = require("discord.js");
module.exports.run = async(bot, message, args) => {
if (!message.member.roles.cache.has('706754890294099990')) return message.channel.send(`${message.author}, roles can only be assigned by admins`).then(msg => {
setTimeout(() => {
msg.delete()
}, 5000);
})
let rMember = message.mentions.members.first()
if (!rMember) return message.reply(`You didn't mention any user`).then(msg => {
setTimeout(() => {
msg.delete()
}, 5000);
})
let role = message.guild.roles.cache.find(role => role.name. == args[1]) || message.mentions.roles.first()
if (!role) return message.channel.send(`${message.author}, You haven't written any role name`).then(msg => {
setTimeout(() => {
msg.delete()
}, 5000);
})
if (rMember.roles.cache.has(role.id)) return message.reply(`This member already has this role!`).then(msg => {
setTimeout(() => {
msg.delete()
}, 5000);
})
if (rMember.roles.cache.has(role.id)) {
} else {
await (rMember.roles.add(role.id));
message.channel.send(`${message.author} Added __${role.name}__ to ${rMember}`);
}
}
module.exports.help = {
name: "role"
}
If your role has spaces in its name that means that your args will be something like this: ['#user', 'Name', 'in', 'parts']
To get the full name you need to join together all the arguments except for the first one, which is the user. To do that, you can use Array.slice() and Array.join(); here's an example:
let roleName = args
.slice(1) // Take every element from index 1 to the end
.join(' ') // Join them back with spaces
let role = message.guild.roles.cache.find(r => r.name == roleName) || message.mentions.roles.first()
I am currently working on discord bot and faced a problem where I need to limit reactions. I am trying to achieve the following goal - if the user selects reaction it should be replaced with the newest reaction. Can someone advise on how to do it?
Here is my screenshot and code of what I have at this moment.
My code:
client.on('raw', event => {
console.log(event);
const eventName = event.t;
if(eventName === 'MESSAGE_REACTION_ADD') {
if(event.d.message_id === '70цw3748527414706191') {
var reactionChannel = client.channels.get(event.d.channel_id);
if(reactionChannel.messages.has(event.d.message_id))
return;
else {
reactionChannel.fetchMessage(event.d.message_id)
.then(msg => {
var msgReaction = msg.reactions.get(event.d.emoji.name + ":" + event.d.emoji.id);
var user = client.users.get(event.d.user_id);
client.emit('messageReactionAdd', msgReation, user);
})
.catch(err => console.log(err));
}
}
}
else if(eventName === 'MESSAGE_REACTION_ADD') {
if(event.d.message_id === '703748527414706191') {
var reactionChannel = client.channels.get(event.d.channel_id);
if(reactionChannel.messages.has(event.d.message_id))
return;
else {
reactionChannel.fetchMessage(event.d.message_id)
.then(msg => {
var msgReaction = msg.reactions.get(event.d.emoji.name + ":" + event.d.emoji.id);
var user = client.users.get(event.d.user_id);
client.emit('messageReactionRemove', msgReation, user);
})
.catch(err => console.log(err));
}
}
}
});
client.on('messageReactionAdd', (messageReaction, user) => {
var roleName = messageReaction.emoji.name;
console.log(roleName);
var role = messageReaction.message.guild.roles.find(role => role.name.toLowerCase() ===
roleName.toLowerCase());
if(role) {
var member = messageReaction.message.guild.members.find(member => member.id === user.id);
if(member) {
member.addRole(role.id);
console.log("Success");
}
}
});
client.on('messageReactionRemove', (messageReaction, user) => {
var roleName = messageReaction.emoji.name;
var role = messageReaction.message.guild.roles.find(role => role.name.toLowerCase() ===
roleName.toLowerCase());
if(role) {
var member = messageReaction.message.guild.members.find(member => member.id === user.id);
if(member) {
member.removeRole(role.id);
console.log("Success");
}
}
});
Thank you for your guidance!
You can check on messageReactionAdd, if they already have another color-role, with this you would need either each color role to have a prefix or to have an array of what color-roles exist.
heres a start
client.on('messageReactionAdd', (reaction, user) => {
const guild = reaction.message.guild;
const roleName = reaction.emoji.name.toLowerCase();
console.log(roleName);
const role = guild.roles.cache.find(r => r.name.toLowerCase() === roleName);
if (role) {
const member = guild.members.cache.find(m => m.id === user.id);
//check if they already have a color role
if (alreadyColorRole) {
//either make it so he cant react until he unreacts to the role
//or automatically remove the other reaction.
reaction.remove();
} else {
member.addRole(role.id);
console.log("Success");
}
}
});