How can I fix this code? What I want to happen is that if the member or the requester is going to pause the music that is playing but it is not in the same voice channel as the bot, it will not be paused.
const guildQueue = client.queue.get(message.guild.id);
const member = message.author || message.member.voice || message.member.voice.channel;
const musicbot = guildQueue || message.guild.me.voice.channel;
if (!guildQueue || !message.guild.me.voice.channel) {
return;
}
if (member == musicbot) {
const embed = new MessageEmbed()
.setColor('#5865F2')
.setDescription(`The track is paused.`)
guildQueue.audioPlayer.pause();
return message.channel.send({embeds: [embed]});
} else if (!member == musicbot) {
return message.channel.send('You need to be in the same channel as the bot!');
} else {
return;
}
You're misunderstanding the variable you need to get. In order to compare the voice channel, you must get first the GuildMember of the user and the bot itself.
Then, you can easily compare their VoiceState and see if they are in the same channel.
const guildQueue = client.queue.get(message.guild.id);
const member = message.member;
const musicbotMember = message.guild.me;
if (!guildQueue || !musicbotMember) return;
if (member.voice.channel === musicbot.voice.channel) {
const embed = new MessageEmbed()
.setColor('#5865F2')
.setDescription(`The track is paused.`)
guildQueue.audioPlayer.pause();
return message.channel.send({ embeds: [embed] });
} else {
return message.channel.send('You need to be in the same channel as the bot!');
}
try to type this first:
if (!member == musicbot) {
return message.channel.send('You need to be in the same channel as the bot!');
}
then join the else statement
if (!member == musicbot) {
return message.channel.send('You need to be in the same channel as the bot!');
} else {
const embed = new MessageEmbed()
.setColor('#5865F2')
.setDescription(`The track is paused.`)
guildQueue.audioPlayer.pause();
return message.channel.send({ embeds: [embed] });
}
So the bot should recognize your first statement as if and return if the member and the bot not in the same voice channel, or if this thing didn't work, just add else if:
if (!member == musicbot) {
return message.channel.send('You need to be in the same channel as the bot!');
} else if(member === musicbot){
const embed = new MessageEmbed()
.setColor('#5865F2')
.setDescription(`The track is paused.`)
guildQueue.audioPlayer.pause();
return message.channel.send({ embeds: [embed] });
}
Related
when i try to unmute someone is allready muted i still get that user is muted even though i have this code.if anyone could help it would very helpfull thank you>
if(user.roles.cache.has(muterole)) {
return message.channel.send("Given User do not have mute role so what i am suppose to take")
}
module.exports = {
name: 'unmute',
description: "This unmutes a member",
execute(message, args, Discord){
if (!message.member.hasPermission("MANAGE_ROLES")) {
return message.channel.send(
"Sorry but you do not have permission to unmute anyone"
);
}
if (!message.guild.me.hasPermission("MANAGE_ROLES")) {
return message.channel.send("I do not have permission to manage roles.");
}
const user = message.mentions.members.first();
if (!user) {
return message.channel.send(
"Please mention the member to who you want to unmute"
);
}
let muterole = message.guild.roles.cache.find(x => x.name === "Muted")
let mainrole = message.guild.roles.cache.find(x => x.name === "Verified")
if(user.roles.cache.has(muterole)) {
return message.channel.send("Given User do not have mute role so what i am suppose to take")
}
user.roles.remove(muterole)
user.roles.add(mainrole)
message.channel.send(`**${message.mentions.users.first().username}** is unmuted`)
user.send(`You are now unmuted from **${message.guild.name}**`)
const MuteEmbed = new Discord.MessageEmbed()
.setColor('#1ABC9C')
.setDescription(`${message.mentions.users.first().username} has now been unmuted `)
.addField('Moderator',`<#${message.author.id}>`)
.addField('member',`${message.mentions.users.first().username}`)
.setFooter(message.member.displayName, message.author.displayAvatarURL({ dynamic: true }))
.setTimestamp()
const channel = message.guild.channels.cache.find(ch => ch.name === 'logs' );channel.send(MuteEmbed)
}
}
I have a suggest command on my bot that im working on. However it only works when the user suggesting is in my server since it is codded to send the suggestion to a specific channel id. Is there a way to code it where the suggestion comes to my dms or specified channel even if the user suggesting isn't in the server? here is my code:
const { MessageEmbed } = require("discord.js")
module.exports.run = async (client, message, args) => {
if (!args.length) {
return message.channel.send("Please Give the Suggestion")
}
let channel = message.guild.channels.cache.find((x) => (x.name === "sauce-supplier-suggestions" || x.name === "sauce-supplier-suggestions"))
if (!channel) {
return message.channel.send("there is no channel with name - sauce-supplier-suggestions")
}
let embed = new MessageEmbed()
.setAuthor("SUGGESTION: " + message.author.tag, message.author.avatarURL())
.setThumbnail(message.author.avatarURL())
.setColor("#ff2050")
.setDescription(args.join(" "))
.setTimestamp()
channel.send(embed).then(m => {
m.react("✅")
m.react("❌")
})
message.channel.send("sucsessfully sent your suggestion to bot team thank you for your suggestion!!")
}
I made some small changes in your code. Now if a user uses the command correctly, the bot will send you a DM with the users suggestion.
const { MessageEmbed } = require("discord.js")
module.exports.run = async (client, message, args) => {
let suggestion = args.join(" ");
let owner = client.users.cache.get("YOUR ID");
if (!suggestion) {
return message.channel.send("Please Give the Suggestion")
}
let channel = message.guild.channels.cache.find((x) => (x.name === "sauce-supplier-suggestions" || x.name === "sauce-supplier-suggestions"))
if (!channel) {
return message.channel.send("there is no channel with name - sauce-supplier-suggestions")
}
let embed = new MessageEmbed()
.setAuthor("SUGGESTION: " + message.author.tag, message.author.displayAvatarURL({ dynamic: true }))
.setThumbnail(message.author.displayAvatarURL({ dynamic: true }))
.setColor("#ff2050")
.setDescription(suggestion)
.setTimestamp()
owner.send(embed).then(m => {
m.react("✅")
m.react("❌")
})
message.channel.send("sucsessfully sent your suggestion to bot team thank you for your suggestion!!")
}
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 have a command which worked, but at some point stopped, returning to the chat message that the role does not exist. An error "(node:12228) DeprecationWarning: Collection#find: pass a function instead" is sent to the console every time I use a command, but I always had it
const Discord = require("discord.js");
module.exports.run = async (bot, message, args) => {
if(!message.member.hasPermission("MANAGE_MEMBERS")) return message.reply();
let rMember = message.guild.member(message.mentions.users.first()) || message.guild.members.get(args[0]);
if(!rMember) return message.reply("nope.");
let role = args.join(" ").slice(22);
if(!role) return message.reply("nope!");
let gRole = message.guild.roles.find(`name`, role);
if(!gRole) return message.reply("role does not exist.");
const allowed = ['some id'];
if (!allowed.includes(gRole.id)) return;
if(rMember.roles.has(gRole.id)) return message.reply("nope.");
await(rMember.removeRoles(['some id']));
await(rMember.addRole(gRole.id));
if(gRole.id == 'id') rMember.addRole('id') && rMember.removeRoles(['some id']);;
try{
await rMember.send(`you got ${gRole.name}!`)
}catch(e){
}
}
module.exports.help = {
name: "role"
}
So I need the command to work.
In order of appearance, I see these mistakes in your code...
You're not catching any rejected promises.
MANAGE_MEMBERS is not a valid permission flag.
You should pass a function into Collection.find().
No idea what you're trying to do with the declaration of role.
Your use of the && logical operator in your if statement is incorrect. Use a block statement instead.
Your catch block in the try...catch statement has been left empty.
Combining the answers provided to your other questions about this code, this is a correct, much cleaner rewrite...
const getMention = require('discord-mentions'); // Don't forget to install.
module.exports.run = async (bot, message, args) => {
if (!message.guild) return;
try {
if (!message.member.hasPermission('MANAGE_ROLES')) return;
if (!args[1]) return await message.reply('Please provide a user.');
if (!args[2]) return await message.reply('Please provide a role.');
if (args[3]) return await message.reply('Too many arguments provided.');
let member;
if (getMention(args[1])) member = getMention(args[1], message.guild).member;
else member = message.guild.members.find(m => m.user.tag === args[1] || m.id === args[1]);
if (!member) return await message.reply('Invalid user provided.');
let role;
if (getMention(args[2])) role = getMention(args[2], message.guild).role;
else role = message.guild.roles.find(r => r.name === args[2] || r.id === args[2]);
if (!role) return await message.reply('Invalid role provided.');
const allowed = ['role1ID', 'role2ID', 'role3ID'];
if (!allowed.includes(role.id)) return await message.reply('That role is not allowed.');
if (member.roles.has(role.id)) return await message.reply('That user already has that role.');
await member.removeRoles(['someID', 'someOtherID']);
await member.addRole(role.id);
if (role.id === 'anAlternateRoleID') {
await member.removeRoles(['someID', 'someOtherID']);
await member.addRole('otherRoleID');
}
await member.send(`You got the ${role.name} role.`)
.catch(() => console.log(`Couldn't send message to ${member.user.tag}.`));
} catch(err) {
console.error(err);
}
};
module.exports.help = {
name: 'role'
};
Keep in mind, if you're removing just one role from a member, you should use the singular version of the method, GuildMember.removeRole().
I was using something like this but i want it to look for reactions in the embed sent! not in the message
const collector = message.createReactionCollector((reaction, user) =>
user.id === message.author.id &&
reaction.emoji.name === "◀" ||
reaction.emoji.name === "▶" ||
reaction.emoji.name === "❌"
).once("collect", reaction => {
const chosen = reaction.emoji.name;
if(chosen === "◀"){
// Prev page
}else if(chosen === "▶"){
// Next page
}else{
// Stop navigating pages
}
collector.stop();
});
A RichEmbed is sent as part of a message. Therefore, when you add a reaction, it's on the message, not the embed.
See the example below which gives the appearance of reactions on an embed.
const { RichEmbed } = require('discord.js');
var embed = new RichEmbed()
.setTitle('**Test**')
.setDescription('React with the emojis.');
message.channel.send(embed)
.then(async msg => {
// Establish reaction collector
for (emoji of ['◀', '▶', '❌']) await msg.react(emoji);
})
.catch(console.error);