How can send DM for unban command used user? - discord.js

I'm developing an unban command for a ban command.
When the ban is lifted, I want the embed message unbanEmbeduserside to be sent to the banned user via private message. This embed message will let the banned user know about it.
The codes for my unban command are as follows. Thanks in advance to my friends who will help me with how to write a code so that I can perform the operation I mentioned in the above article.
async run (bot, message, args) {
message.delete(message.author);
if (!message.member.hasPermission("BAN_MEMBERS"))
return message.author.send("Buna yetkin yok.")
let unbanEmbednotfoundmessage = new discord.MessageEmbed()
.setColor(0xdb2727)
.setDescription(`Kullanıcı bulunamadı veya yasaklı değil.`);
let userID = args[0]
message.guild.fetchBans().then(bans => {
if(bans.size == 0) return message.channel.send(unbanEmbednotfoundmessage);
let bUser = bans.find(b => b.user.id == userID)
if(!bUser) return message.channel.send(unbanEmbednotfoundmessage)
let unbanEmbeduserside = new discord.MessageEmbed()
.setAuthor('YASAKLAMA KALDIRILDI', 'https://i.hizliresim.com/midfo22.jpg')
.setDescription(`
*Yasaklamanız kaldırıldı, sunucuya tekrar katılabilirsiniz!*
Yasaklamayı Kaldıran Yetkili: **${message.author.username}**
`)
.setColor(0xc8b280)
.setTimestamp()
.setFooter('gtaplus Multiplayer Community');
let unbanEmbedserverside = new discord.MessageEmbed()
.setAuthor('YASAKLAMA KALDIRMA', 'https://i.hizliresim.com/midfo22.jpg')
.setDescription(`
Yasağı Kaldırılan Kullanıcı: **${userID}**
Yasaklamayı Kaldıran Yetkili: **${message.author.username}**
`)
.setColor(0xc8b280)
.setTimestamp()
.setFooter('gtaplus Multiplayer Community');
message.guild.members.unban(bUser.user).then(() => message.channel.send(`**${userID}** yasaklaması kaldırıldı.`));
if (unbanEmbedserverside){
const log = message.guild.channels.cache.find(channel => channel.name === 'server-log')
log.send(unbanEmbedserverside);
}
})
}}

In Discord, any user, including a bot, will need to be in a server with a user to send them direct messages. This, unfortunately, means that unless the bot shares a server with the banned user, you will not be able to send the message. If the bot does share another server, the answer that Skularun Mrusal provided in the comments of your post should work.

Related

Discord.js Forward DM message to specific channel

So, I need a way to forward a message sent in the bot's dm to a specific channel in my server.
this is the code that I got so far:
execute(message, args) {
if(message.channel.type == "dm"){
let cf = args.join(' ')
const cfAdm = message.guild.channels.cache.get('767082831205367809')
let embed = new discord.MessageEmbed()
.setTitle('**CONFISSÃO**')
.setDescription(cf)
.setColor('#000000')
const filter = (reaction, user) => ['👍', '👎'].includes(reaction.emoji.name);
const reactOptions = {maxEmojis: 1};
cfAdm.send(embed)
.then(function (message) {
message.react('👍')
.then(() => message.react('👎'))
/**** start collecting reacts ****/
.then(() => message.awaitReactions(filter, reactOptions))
/**** collection finished! ****/
.then(collected => {
if (collected.first().emoji.name === '👍') {
const cfGnr = message.guild.channels.cache.get('766763882097672263')
cfGnr.send(embed)
}
})
});
}
else {
message.delete()
message.channel.send('Send me this in the dm so you can stay anon')
.then (message =>{
message.delete({timeout: 5000})
})
}
}
But for some reason that I can't seem to understand, it gives me this error:
TypeError: Cannot read property 'channels' of null
If anyone can help me, that would be greatly apreciated.
Thanks in advance and sorry for the bad english
The error here is that you are trying to call const cfAdm = message.guild.channels.cache.get('ID') on a message object from a Direct Message channel ( if(message.channel.type == "dm"){)
DMs don't have guilds, therefore message.guild is null and message.guild.channels does not exist.
You will need to first access the channel some other way. Luckily, Discord.js has a way to access all channels the bot can work with* (you don't need to muck around with guilds because all channels have unique IDs):
client.channels.fetch(ID) or client.channels.cache.get(ID)
(I have not tested this but it seems fairly straight forward)
*See the link for caveats that apply to bots in a large amount of servers.

Always getting back an, "Internal Server Error," when trying to run ban command on my Discord bot

I have tried many different ways of formatting the code, however, whenever I add code so that I must provide a reasoning to ban someone, I am always given an Internal Server Error. Here is my code.
module.exports.run = async (client, message, args) => {
const member = message.mentions.members.first();
const reason = args.slice(1).join(" ")
if (!message.member.hasPermission("BAN_MEMBERS")) {
return message.reply("you lack sufficiant permissions to execute this command.");
} else if (member.hasPermission("ADMINISTRATOR")) {
message.reply("you cannot ban this member.")
}
member.ban(reason).then((member) => {
message.channel.send(`${member} has been banned.`);
});
I use a command handler, and all my other commands work fine.
first step: Define the user
let user = message.mentions.members.first() || message.guild.members.cache.get(args.join(' '));
Second step: Create embed message or normal message
const userbanned = new Discord.MessageEmbed()
.setColor('#FF0000')
.setAuthor('User Banned')
.setDescription(`**${user.user.username}#${user.user.discriminator}** is now banned from this server`)
.setFooter(`bot_name`);
Third step: Send message
user.send(`You were banned from **${message.guild.name}** by ${message.author.username}#${message.author.discriminator}`)
return user
.ban()
.then(() => message.channel.send(userbanned))
.catch(error => message.reply("ERROR"));
Try changing
member.ban().then((member) =>//
to
member.ban({reason : args.slice(1).join(' ')}).then((member) =>//

Bot comes online but the embed won't post on discord

i'm new to programming and started making a discord bot by watching a few tutorials. I want the bot to DM the discord embed to the user who types "-buy" in a text channel. When running the code, the bot comes online and runs the "your bot name is online!" but no DM is sent. I would greatly appreciate any help. Thanks
const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = '-';
client.once('ready', () => {
console.log('your bot name is online!');
});
client.on('message', message =>{
if(message.author.client) return;
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLocaleLowerCase();
if(command === 'buy'){
const testEmbed = new Discord.MessageEmbed()
.setColor(0x7f03fc)
.setTitle('test embeddy')
.setDescription('test description woo')
.setFooter('this is the footer')
try {
message.author.send(testEmbed);
} catch {
message.reply('Sorry I cannot message you! Check if your DMs are public!')
}
}
});
client.login('');
the token isn't the problem, i deleted it so i could upload here
The message.author.client returns the bot client, and it doesn't return a boolean. So your bot is blocked from there. Try removing that code and write message.author.bot that returns a boolean if the message author is a bot user. It will work.

How do i use the command that uses mention on discord.js bot

Ok so I have made a command, that sends an embed on users info
case 'playerinfo'
const embed = new discord.MessageEmbed()
.setTitle('User information')
.addField('Player Name', message.author.username)
.addField('Current server', message.guild.name)
.setColor(0x34eb8f)
.setThumbnail(message.author.displayAvatarURL())
message.channel.send(embed)
now what it does is if I do !playerinfo, it tells us our username, and our profile picture and the server we are using it on. But what I want it to do is that if I type !playerinfo #randomname, I want it to give info on that player. Please tell me how to do that?
You can use message.mentions.users to get the mentioned user, if that is not provided then it'd just use your own user
case 'playerinfo'
const user = message.mentions.users.first() || message.author
const embed = new discord.MessageEmbed()
.setTitle('User information')
.addField('Player Name', user.username)
.addField('Current server', message.guild.name)
.setColor(0x34eb8f)
.setThumbnail(user.displayAvatarURL())
message.channel.send(embed)

Discord.js delete messages from specific UserIDs

So I'm having some problems with the code, I am trying to have the message.author.id match a list of IDs in a const then do something.
const blacklisted = ["644298972420374528", "293534327805968394", "358478352467886083"];
if (message.author.id === blacklisted) {
message.delete()
const blacklistedMSG = new Discord.RichEmbed()
.setColor('#ff0000')
.setTitle('Blacklisted')
.setDescription(`You are currently Blacklisted. This means you cannot send messages in Any server with this bot.`)
.setTimestamp()
.setFooter(copyright);
message.author.send(blacklistedMSG).then(msg => {msg.delete(30000)})
}
When I have it like that, it doesn't do anything and there are no errors in the console about it. But when the code is this:
if (message.author.id === "644298972420374528") {
message.delete()
const blacklistedMSG = new Discord.RichEmbed()
.setColor('#ff0000')
.setTitle('Blacklisted')
.setDescription(`You are currently Blacklisted. This means you cannot send messages in Any server with this bot.`)
.setTimestamp()
.setFooter(copyright);
message.author.send(blacklistedMSG).then(msg => {msg.delete(30000)})
}
It works and sends the user an embed and deletes the message they sent. Not sure what's going on with it. Thanks in advance.
You are making a direct comparison to the array rather than checking to see if message.author.id is in the array.
Instead of
if (message.author.id === blacklisted)
Try
if (blacklisted.includes(message.author.id))

Resources