How could I mention a user in an embed? - discord

I'm currently having an issue where I'm unable to ping people in embeds. I've searched up solutions online, but none of them are working. Here's my code, thanks.
else if (command === 'post') {
if (!message.member) return;
const messageContent = args.slice(1).join(' ');
if (!messageContent) return await message.channel.send("Invalid syntax, please include full detail, including what you need done, payment, time etc.");
await message.channel.send("Post sent for approval.");
const hiringEmbed = new Discord.MessageEmbed()
.setColor(embedColor)
.setTitle('Hiring post.')
.setAuthor({name : message.member.user.username, iconURL : message.member.user.displayAvatarURL()})
.setDescription(messageContent)
.setFooter({text : `Post by <#${message.member.user.id}>`}) // This is the line that wont work
.setTimestamp()
await client.channels.cache.find(channel => channel.id === '937027971137548378').send({embeds : [hiringEmbed]});
}

To ping someone you need to add the user to the content of the message you send channel.send({content:'<#${id}>', embeds: [hiringEmbed]

You cannot ping a member in an embed but rather only mention them. So, you need to ping them in the regular content of the message instead of the embed.
await client.channels.cache.find(channel => channel.id === '937027971137548378').send({content: `${message.member.user.id}`, embeds : [hiringEmbed]});
In the footer you can use their username instead if you want to mention who used the command.

.setFooter({text : `Post by ${message.author.tag}`})
or
.setFooter(`Post by ${message.author.tag}`)

Related

event channelCreate .setauthor

is there any way to put the user who created the channel in .setAuthor? I searched in https://discord.js.org/#/docs/main/stable/class/GuildChannel but found nothing
client.on("channelCreate", function(channel) {
const logchannel = channel.guild.channels.cache.find(ch => ch.name === "logchannel")
if (!logchannel) return
const embed = new Discord.MessageEmbed()
.setTitle("Channel created)
.setDescription(`Channel <#${channel.id}> has created.`)
.setTimestamp()
.setAuthor(//user who created the channel)
logchannel.send(embed)
})
I think it's best to use the AuditLogs, you may want to take a look at it.

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) =>//

Discord.js How can I edit previous bot's message?

so I am trying to make a ROSTER command. The command is $roster add/remove #user RANK. This command basically should edit a previous bot's message (roster) and add a user to the roster to the RANK in the command... This is my code so far, but I haven't managed to make the roster message and the editing part of it and the RANK system. If someone could help that would be very amazing!
//ROOSTER COMMAND
client.on('message', async message => {
if (message.content.startsWith(prefix + "roster")) {
if (!message.member.hasPermission('ADMINISTRATOR')) return message.channel.send('You do not have that permission! :x:').then(message.react(':x:'))
const args = message.content.slice(prefix.length + 7).split(/ +/)
let uReply = args[0];
const user = message.mentions.members.first()
if(!uReply) message.channel.send("Please use `add` or `remove`.")
if(uReply === 'add') {
if(!user) return message.channel.send("Please make sure to provide which user you would like to add...")
message.channel.send(`you are adding **${user.displayName}** from the roster.`)
} else if(uReply === 'remove') {
if(!user) return message.channel.send("Please make sure to provide which user you would like to add...")
message.channel.send(`you are removing **${user.displayName}** from the roster.`)
}
}})
Sounds like the .edit() method is what you want.
Example from the docs:
// Update the content of a message
message.edit('This is my new content!')
.then(msg => console.log(`Updated the content of a message to ${msg.content}`))
.catch(console.error);
To edit your bots previous message, you need to have a reference of the message you want to edit. The returned reference is a promise so do not forget to use await keyword. you can then use .edit() function on the reference to update you msg.
const msgRef = await msg.channel.send("Hello");
msgRef.edit("Bye");

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