Role command DiscordJS V13 - discord.js

If the user does not have the role, it will add the role, but if the user already has the role, it will remove the role. How can I do that?
if (message.member.roles.has(role.id)){
const embed = new MessageEmbed()
.setColor('#5865F2')
.setDescription(`Role added ${role} to ${member}.`)
.setFooter(`Requested by ${message.author.username}`)
member.roles.remove(role.id)
message.channel.send({ embeds: [embed] });
} else {
const embed = new MessageEmbed()
.setColor('#5865F2')
.setDescription(`Role removed ${role} to ${member}.`)
.setFooter(`Requested by ${message.author.username}`)
member.roles.add(role.id)
message.channel.send({ embeds: [embed] });
}

The reason you are getting this error is because the .has() function does not exist in the roles property. Instead, you need to fetch them using .fetch() or use the cached roles by using roles.cache.has(). So your code might look something like this:
if (message.member.roles.cache.has(role.id)){
const embed = new MessageEmbed()
.setColor('#5865F2')
.setDescription(`Role added ${role} to ${member}.`)
.setFooter(`Requested by ${message.author.username}`)
member.roles.remove(role.id)
message.channel.send({ embeds: [embed] });
} else {
const embed = new MessageEmbed()
.setColor('#5865F2')
.setDescription(`Role removed ${role} to ${member}.`)
.setFooter(`Requested by ${message.author.username}`)
member.roles.add(role.id)
message.channel.send({ embeds: [embed] });
}

Related

Discord.js how to send embed to a specific channel

How do i send embed to a specific channel.
dont mind the code below thats just my example on how i do it
id = client.channels.cache.get("id")
client.once('ready', () => {
embed = new Discord.MessageEmbed();
embed.setTitle('example')
embed.setAuthor(client.user.tag)
embed.setDescription('example')
id.send({ embeds: [embed] })
})
just make it embed
client.on('ready', () => {
client.channels.fetch('<channel id>')
.then(channel => {
channel.send("Hello here!");
})
});
You will need to fetch the channel.
client.once('ready', () => {
const channel = client.channels.cache.get('ID_HERE')
let embed = new Discord.MessageEmbed()
.setTitle('example')
.setAuthor(client.user.tag)
.setDescription('example')
channel.send({ embeds: [ embed ] });
})
I've added channel into the ready event which should fix the problem.

how do i embed my bots reply with say command discord.js

on my say command it's not embedded so it lets members #everyone I want to embed the bot's reply to prevent that.
i tried other embeds but they did not work out due to them being
outdated i tried my own but it did not work
command I use:
client.on('message',
function(msg){
if(msg.content === "v!say"){
I don't lnow what to put after
There are several things you could do on this
FYI message is deprecrated and you should use the event listener below:
client.on(`messageCreate`, async (msg) => {
// code here
};
Prevent #everyone and #here
if(msg.content === `v!say` && !msg.content.includes(`#everyone`) && !msg.content.includes(`#here`){
let message2send = //however you already do this
await msg.channel.send({
content: message2send,
});
}
Embed Message
const { MessageEmbed } = require(`discord.js`);
//put at top of file
if(msg.content === `v!say`){
let message2send = //however you already do this
let embed = new MessageEmbed()
.setDescription(message2send)
await msg.channel.send({
embeds: [embed],
});
}
Do Both
const { MessageEmbed } = require(`discord.js`);
//put at top of file
if(msg.content === `v!say` && !msg.content.includes(`#everyone`) && !msg.content.includes(`#here`){
let message2send = //however you already do this
let embed = new MessageEmbed()
.setDescription(message2send)
await msg.channel.send({
embeds: [embed],
});
}
UPDATE: this answer is not for Discord.js v14+ as MessageEmbed is now EmbedBuilder

Music Bot DiscordJSV13 Disconnect Command

A member is going to disconnect a bot in a voice channel, but he can disconnect the call without being inside the voice channel. How can I fix this?
const voiceChannel = message.member.voice.channel;
const { channel } = message.member.voice || message.member.voice.channel;
const guildQueue = client.queue.get(message.guild.id);
const member = member.voice.channel;
// Check if the user is in the same channel. If not then it will not be able to disconnect the bot.
if (message.guild.me.voice.channel || guildQueue){
const embed = new MessageEmbed()
.setColor('#5865F2')
.setDescription(`You're not on the call.`)
return message.channel.send({ embeds: [embed] });
}
// Disconnects the bot from the call.
const embed = new MessageEmbed()
.setColor('#5865F2')
.setDescription(`I'm no longer connected.`)
guildQueue.connection.destroy();
client.queue.delete(message.guild.id);
return message.channel.send({ embeds: [embed] });
}

Error Message: (node:12160) and problem with an await function

My Problem is that I'm getting the following error Message:
(node:12160) DeprecationWarning: The message event is deprecated. Use messageCreate instead(Use `node --trace-deprecation ...` to show where the warning was created)
I know that there's a solution for this on this Site but my problem is that my await function is not working when I try to change the .send({ embeds: [embed] }) to message.channel.send({ embeds: [embed] }); and i don't know how to change the embed so my await function is working and the error resolves.
if (message.content.startsWith("!Suggestion")) {
var str = message.content.slice(" !Suggestion".length);
const embed = new Discord.MessageEmbed()
.setThumbnail('Png')
.setTitle("Suggestion")
.setColor('#151515')
.addFields(
{ name: "Person, with a suggestion:", value: `<#!${message.author.id}>` },
{ name: 'Suggestion:', value: `${str}` },
{ name: 'Channel:', value: `${message.channel}` },
)
.setTimestamp()
.setFooter("You can vote for the suggestion with the emojis on the bottom")
await message.guild.channels.cache.find(c => c.name === 'Suggestion')
.send({ embeds: [embed] })
.then(embedMessage => {
embedMessage.react("✅")
embedMessage.react("❌")
});
message.delete();
}
});
It looks like you updated to Discord.js V13 and there are a lot of changes.
The warning you get is about the on message event.
On v12 it was:
client.on("message", ...);
and now on v13 it is:
client.on("messageCreate", ...);
Check https://discordjs.guide/additional-info/changes-in-v13.html for all the changes in the newest version.
You will have to update your code according to the changes.

My reaction roles command won't work in Discord.js v12

Ok so... I've been attempting to make a reaction roles command but it won't work. The role isn't given to the user. It gives me this error "ReferenceError: reaction is not defined"
Code:
const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL","REACTION"]});
client.on('message', async message => {
if(message.content === '>reactions'){
const embed = new Discord.MessageEmbed()
.setTitle('Reaction Roles')
.setDescription('React to obtain your roles!')
.setColor('#242323')
let MessageEmbed = await message.channel.send(embed)
MessageEmbed.react('👨‍👦')
}
if(reaction.message.partial) await reaction.message.fetch();
if(reaction.partial) await reaction.fetch();
if(user.client) return;
if(!reaction.message.guild) return;
if(reaction.message.channel.id === "759064796611215442") {
if(reaction.emoji.name === '👨‍👦'){
await reaction.message.guild.member.cache.get(user.id).roles.add('760838222057046046')
}
}
});

Resources