How can I write a DM to every user in a role? I'm going to make it so when you get a VoiceChannel joint that all are written in a certain role by DM. What's the best way to do that?
Heres my Code:
const guild = bot.guilds.cache.get('601109434197868574');
const voiceChannel = guild.channels.cache.get('706243822564409444');
voiceChannel.members.forEach(member => {
let sup = guild.roles.cache.find(role => role.name === '▬▬ Anastic | Supporter ▬▬⠀');
sup.send('Hey!')
})
}, 10000)```
sup is a role and you are trying to send a message to the role itself, which is not possible. (Role.send('Hey!').
You need to loop through the role members.
const Guild = client.guilds.cache.get("GuildID");
if (!Guild) return false;
const Role = Guild.roles.cache.find(role => role.name == "▬▬ Anastic | Supporter ▬▬");
if (!Role) return false;
Role.members.forEach(member => {
member.send("Hello!").catch(e => console.error(`Couldn't send the message to ${member.user.tag}!`));
});
Related
I am trying to make a code that searches a custom status for the phrase ".gg/RoundTable" and will then give the person a certain role I have in my server.
Here is my code so far , the code runs with no errors but it will not assign the role.
const Discord = require("discord.js")
const client = new Discord.Client()
const mySecret = process.env['TOKEN']
client.login(mySecret)
const roleID = 865801753462702090
client.on('presenceUpdate', async (oldPresence, newPresence) => {
const role = newPresence.guild.roles.cache.find(role => role.name === 'Pic Perms (.gg/RoundTable)');
const status = ".gg/RoundTable"
const member = newPresence.member
console.log(member.user.presence.activities[0].state)
if(member.presence.activities[0].state.includes(status)){
return newPresence.member.roles.add(roleID)
} else {
if(member.roles.cache.has(roleID)) {
newPresence.member.roles.remove(roleID)
}
}
})
Try this:
const Discord = require("discord.js");
const client = new Discord.Client();
const roleID = "851563088314105867";
client.on("presenceUpdate", async (_, newPresence) => {
const role = newPresence.guild.roles.cache.get(roleID);
const status = ".gg/RoundTable";
const member = newPresence.member;
if (member.presence.activities[0].state?.includes(status)) {
return newPresence.member.roles.add(role);
} else {
if (member.roles.cache.has(role)) {
newPresence.member.roles.remove(role);
}
}
});
client.login("your-token");
I'd recommend finding your role in the RoleManager.cache using get() as you already have the roleID and then actually assign that role instead of the roleID. Note I added an optional chaining operator since if a user does not have a custom status .state will be null.
I'm doing the most basic mute command ever, and I need to find if the user has a role called 'Muted'
I've added the code below, but I keep getting an error.
if (command === "mute") {
const { member, mentions } = message
const mutee = message.mentions.users.first();
const muter = message.author
console.log(mutee)
if(member.hasPermission('ADMINISTRATOR')) {
let muteRole = message.guild.roles.cache.find(role => role.name === "Muted");
if (message.guild.members.get(mutee.id).roles.cache.has(muteRole.id)) {
message.channel.send(new MessageEmbed() .setTitle(`Uh Oh!`) .setDescription(`${mutee.user} already has been Muted!`))
}
else {
}
}
}
First, I recommend you to get the mentioned member directly instead of the user with const mutee = message.mentions.members.first();. (If you are using the command under a Guild Channel)
Then, you can simply check if he has the role with:
if (mutee.roles.cache.has(muteRole.id))
How to update the overwrite of a specific role? I want #Player to talk in a specific channel when triggering the command, I don't know how overwritePermissions() works I'm using Discord.js V12
Firstly overwritePermissions, like it says overwrites all the permissions, you might want updateOverwrites instead
Both require a role resolvable or user resolvable, so the first step is to get that role:
const guild = <Guild>;
const role = guild.roles.cache.get(role_id);
const role2 = guild.roles.cache.find(role => role.name === "Player");
After you get that role you need a channel to change the permissions in:
const guild = <Guild>;
const message = <Message>;
const channel = message.chanenl;
const channel2 = guild.channels.cache.get(channel_id);
const channel3 = guild.channels.cache.find(channel => channel.name === "name-here");
After that you can just use the method:
channel.updateOverwrite(role, { VIEW_MESSAGES: true, SEND_MESSAGES: true });
or
channel.overwritePermissions([
{
id: role.id,
allow: ["VIEW_MESSAGES", "SEND_MESSAGES"]
}
]);
I was trying to make a ban command where you can ban a user with a reason.
Turns out user.ban is not a function in Discord.js V12 even though it should be.
Here is my code.
const { MessageEmbed } = require('discord.js');
module.exports = {
name: 'ban',
description: 'Bans a user.',
category: 'Moderation',
usage: '^ban <user> <reason>',
run: async (bot, message, args) => {
if (!message.member.hasPermission('BAN_MEMBERS')) {
return message.channel.send('You do not have permission to do this! ❌');
}
if (!message.guild.me.hasPermission('BAN_MEMBERS')) {
return message.channel.send('I do not have permission to do this! ❌');
}
const user = message.mentions.users.first();
if (!user) {
return message.channel.send('User was not specified. ❌');
}
if (user.id === message.author.id) {
return message.channel.send('You cannot ban yourself! ❌');
}
let reason = message.content
.split(' ')
.slice(2)
.join(' ');
if (!reason) {
reason = 'No reason provided.';
}
let Embed = new MessageEmbed()
.setTitle(`Justice! | Ban Action`)
.setDescription(`Banned \`${user}\` - Tag: \`${user.discriminator}\``)
.setColor('ORANGE')
.setThumbnail(user.avatarURL)
.addField('Banned by', `\`${message.author.username}\``)
.addField(`Reason?`, `\`${reason}\``)
.setTimestamp();
message.channel.send(Embed);
user.ban(reason);
},
};
Is there a way to fix this?
You're getting a User instead of a GuildMember. A User represents a person on discord, while a GuildMember represents a member of a server. You can get a GuildMember instead of a User by using mentions.members instead of mentions.users ex:
const user = message.mentions.members.first()
I'd like to notify our main chat channel when a role has changed for someone, a specific role though -- how can I do this?
I hope I understood your question well. You have to use the guildMemberUpdate event to check if the roles are still the same if the event gets triggered. Then, you have to run a simple for loop and check which roles have been removed or assigned from the guildMember.
Here is the code:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('guildMemberupdate', (oldMember, newMember) => {
const messagechannel = oldMember.guild.channels.find(r => r.name === 'Name of the channel where the announcement should be sent');
if (!messagechannel) return 'Channel does not exist!';
if (oldMember.roles.size < newMember.roles.size) {
const embed = new Discord.RichEmbed()
.setColor('ORANGE')
.setTimestamp()
.setAuthor('Role assigned')
.addField(`📎 Member:`, `${oldMember.user.tag} (${oldMember.id})`);
for (const role of newMember.roles.map(x => x.id)) {
if (!oldMember.roles.has(role)) {
embed.addField(`📥 Role(s):`, `${oldMember.guild.roles.get(role).name}`);
}
}
messagechannel.send({
embed
});
}
if (oldMember.roles.size > newMember.roles.size) {
const embed = new Discord.RichEmbed()
.setColor('ORANGE')
.setTimestamp()
.setAuthor('Role removed')
.addField(`📎 Member`, `${oldMember.user.tag} (${oldMember.id})`);
for (const role of oldMember.roles.map(x => x.id)) {
if (!newMember.roles.has(role)) {
embed.addField(`📥 Role(s):`, `${oldMember.guild.roles.get(role).name}`);
}
}
messagechannel.send({
embed
});
}
});