Sending a newly created emoji after guild.emojis.create() - node.js

I have an addemoji command and I want it to send the new emoji created after the bot creates it. It sends like :emojiname: instead of the actual emoji added. How can I define the new emoji added? Somebody suggested that I use console.log but I have no idea how to use that information that it puts inside the log.
module.exports = {
name: 'addemoji',
description: 'ping pong',
execute(message, args) {
const Discord = require('discord.js');
const PREFIX = 'ly?';
const load = '<a:loading:824515478939762699>';
if (message.content.startsWith(PREFIX + 'addemoji')) {
if (message.guild.me.permissions.has('USE_EXTERNAL_EMOJIS')) {
if (message.guild.me.permissions.has('MANAGE_EMOJIS')) {
if (message.member.hasPermission('MANAGE_EMOJIS')) {
const match = /<(a?):(.+):(\d+)>/u.exec(message.content);
if (!match)
return message.reply(
'Please include a custom emoji in your message!',
);
// animated will be 'a' if it is animated or '' if it isn't
const [, animated, name, id] = match;
const url = `https://cdn.discordapp.com/emojis/${id}.${
animated ? 'gif' : 'png'
}`;
const user = message.mentions.users.first() || message.author;
const nameid = `<:${name}:${id}>`;
message.guild.emojis.create(url, name);
let newname = console.log(name);
let newid = console.log(id);
const embed = new Discord.MessageEmbed()
.setTitle(`Emoji added! <:${newname}:${newid}>`)
.setColor(0x7732a8);
message.channel.send(embed);
}
}
}
}
}
};

GuildEmojiManager#create() returns a Promise with the newly created emoji. You can access and display the new emoji by handling this promise, then using the <:name:id> wrapper for emoijs
message.guild.emojis.create(url, name).then(newEmoji => {
const embed = new Discord.MessageEmbed()
.setTitle(`Emoji added! <:${newEmoji.name}:${newEmoji.id}>`)
.setColor(0x7732a8)
message.channel.send(embed);
});

Related

Is there any way to ignore backslashes and forward slashes in link route?

I am making a user Authentication system and it sends users a confirmation email when user registers, the problem I'm facing is that sometimes link contains / and \ .due to this any text after the / was considered as new route. Is there any way to ignore these slashes. btw I'm using hashing to generate the link.
The link is generated by this piece of code.
const hashedEmail = await bcrypt.hash(email,10);
console.log(await bcrypt.compare(email,hashedEmail))
const mailOptions = {
from: 'someone#gmail.com',
to: email,
subject: 'Confirm your email',
text : `Thanks for registering! Please click on the link to confirm your email:
http://localhost:3500/confirm/${hashedEmail}`
}
confirmation Route
const bcrypt = require('bcrypt');
const fsPromises = require('fs').promises;
const path = require('path');
// user data:
const userData = {
users: require('../model/users.json'),
setUsersData : function(data) {this.users = data}
}
async function confirmationRoute(fastify,options,done){
fastify.get('/confirm/:id',async(req,res) => {
const HashedId = req.params.id;
let foundUser;
for (let i = 0; i < userData.users.length; i++) {
if (await bcrypt.compare(userData.users[i].email,HashedId)){
foundUser = userData.users[i];
}
}
if(foundUser){
// change isverified to true
foundUser.isVerified = true;
// write the new user data to the file
userData.setUsersData([...userData.users.filter(user => user.email !== foundUser.email),foundUser]);
await fsPromises.writeFile(path.join(__dirname,'..','model','users.json'),
JSON.stringify(userData.users)
);
res.send({"message":"Account Verified"})
}
else{
res.send({"message":"Invalid Link"})
}
})
done()
}
module.exports = confirmationRoute;
Example Link is : http://localhost:3500/confirm/$2b$10$bWUrZ6d0dQuZ8.e/Hqd..OEIaFN2By3nVE6x6H2sHCyLlA/nTriKO
You need to encode the parameter and decode it before processing it:
const param = encodeURIComponent('hello/world?')
console.log(param)
const data = decodeURIComponent(param)
console.log(data)

why my bot is not dending the image in embed

const { MessageEmbed, WebhookClient, GuildMember} = require('discord.js');
module.exports = {
name : 'guildMemberAdd',
/**
*
* #param {GuildMember} member
*/
async execute(member) {
const {guild, user} = member;
const welcomer = new WebhookClient({
id : '958046742446759936',
token: '3X9d1gJT8rLCyV-WFkorfkpU5Tl9iF8IXWMz3BOfuR7GQ1Sf5V5OD1HwWxRHRLf97tK0'
});
const welcome = new MessageEmbed()
.setColor('AQUA')
.setAuthor({name: user.tag,iconURL: user.avatarURL({dynamic:true})})
.setThumbnail(user.avatarURL)
.setDescription(`Welcome ${member} to the **${guild.name}**!\nAccount Created: <t:${parseInt(user.createdTimestamp/1000)}:R>\nLatest Member Count: **${guild.memberCount}**`)
.setFooter({
text: user.tag,
iconURL: user.avatarURL()
})
welcomer.send({embeds:[welcome]})
let welcomeRole = guild.roles.cache.find(role => role.id === '954841166933811210');
console.log(`${user.tag} has joined the server`)
member.roles.add(welcomeRole);
}
}
I am trying to do create an embed but it is not loading the thumbnail please help me to do it as I am very upset
it did not even give my photo in the embed what is the problem behind it is this is in code or from discord
use user.displayAvatarURL() instead of user.avatarURL()

Trying to catch response but why wont it send?

I am currently working on a command right now that I would like the server owner to use. When done it sends the embed and will wait for the server owner to paste we will say the channel id. I want to label this to call in another command but at the moment it sends the embed but when I send a channel ID nothing happens. No message is sent but it also does not break the bot. I am still able to send the command again and again.
Please do not mind the slop, I am new to all this and trying to learn as I go any tips would be appreciated!
EDIT: I am using version 12 (12.5.3)
const { prefix } = require('../config.json');
const Discord = require('discord.js');
const client = new Discord.Client();
module.exports = {
name: 'setchannel',
description: 'command to assign bot to channel',
execute(message, args) {
if(message.channel.type == "dm") {
const keepinservercommandembed = new Discord.MessageEmbed()
.setColor('#FF0000')
.setTitle('**Im Sorry!**')
.addFields(
{ name: `**Can` + `'t accept: ${prefix}setchannel**`, value: 'Please keep this command to the server!' },
)
message.channel.send(keepinservercommandembed);
}
else {
const messagesender = message.member.id;
const serverowner = message.guild.owner.id;
const setchannelembed = new Discord.MessageEmbed()
.setColor('#FF0000')
.addFields(
{ name: `**Let's get searching!**`, value: `Hello ${message.guild.owner}, what channel would you like me to post group finding results?` },
)
const notownerembed = new Discord.MessageEmbed()
.setColor('#FF0000')
.addFields(
{ name: `**Whoops!**`, value: `Sorry only ${message.guild.owner} can use this command.` },
)
if(messagesender == serverowner) {
message.delete ();
const filter = m => m.content.message;
const collector = message.channel.createMessageCollector(filter, { time: 15000 });
message.channel.send(setchannelembed).then(() => {
collector.on('collect', m => {
message.channel.send(`${m.content}`)
});
});
}
else {
message.delete ();
message.channel.send(notownerembed);
}
}
}
}
After comparing your code to my similiar discord.js v13 code and checking out the v12 docs, I think I found the problem.
I don't think m.content.message is a function. If your aim is for someone to paste the channel ID, the filter code would be:
const filter = m => m.author.id === message.guild.owner.id;
To filter out other people who may be entering an ID (assuming the input is valid).
Use the filter as follows:
message.channel.createMessageCollector({filter: filter, time: 15000 });
To filter out invalid inputs:
collector.on('collect', m => {
if (isNaN(m)) return; //all channel id's in discord are integers (numbers)
else message.channel.send(`${m.content}`);
});

So, I got a question. Is it possible to have a embed welcome message in a Discord bot that gets deleted after a few seconds?

var TOKEN = ""
const client = ("TOKEN");
const { Client, MessageEmbed } = require('discord.js');
const channel = ("CHANNELID");
const embed = new MessageEmbed()
('guildMemberAdd', member => {
const embed = new Discord.RichEmbed()
.setColor('#0099ff')
.setDescription(`Welcome ${member}! You are the ${membercount}th member!`)
.setImage(member.user.avatarURL)
})
That is my current code for this but, I'm unsure because there are some errors to it.
Yes it is possible.
Please don't use this code, it will not work based on how you have it coded now
var TOKEN = ""
const client = ("TOKEN");
const { Client, MessageEmbed } = require('discord.js');
const channel = ("CHANNELID");
const embed = new MessageEmbed()
('guildMemberAdd', member => {
const embed = new Discord.RichEmbed()
.setColor('#0099ff')
.setDescription(`Welcome ${member}! You are the ${membercount}th member!`)
.setImage(member.user.avatarURL)
})
Please use the code below:
// var TOKEN = "" //token is never a variable
// const client = ("TOKEN"); //please find a different way to code this as this will cause confusion later on
const TOKEN = ""
const { Client, MessageEmbed } = require('discord.js');
const client = new Client({
intents: [], // pick the intents you need here https://discord.com/developers/docs/topics/gateway#list-of-intents
})
client.login(TOKEN)
client.on('guildMemberAdd', member => {
const welcomeChannel = client.channels.cache.get("channel_id")
const membercount = member.guild.members.cache.filter(member => !member.user.bot).size
const timeout = 30 * 1000 //30 being how many seconds till the message is deleted.
const embed = new MessageEmbed()
.setColor('#0099ff')
.setTitle(`${member.displayName} has joined us!`)
.setDescription(`Welcome ${member}!\nYou are member number ${membercount}!`)
.setThumbnail(member.displayAvatarURL())
welcomeChannel.send({
embeds: [embed]
}).then((message) => {
setTimeout(() => {
message.delete()
}, timeout);
})
})
Yes, you can!
just use
.then(msg => {
setTimeout(() => {
msg.delete().catch(() => null)
}, 5000)
})

Getting an image URL from a message

I want to get the image URL or the image itself from a message, and put it in my embed. I have no idea to do it myself, hope you can help me :)
client.on('messageReactionAdd', (reaction, user, message) => {
const eggsa = client.emojis.find(emoji => emoji.name === "eggsa");
if (reaction.emoji.name === 'eggsa') {
const msg = reaction.message;
const guild = msg.guild;
const guildMembers = guild.members;
const guildMember = guildMembers.get(user.id);
if (!guildMember.roles.some(r=>["Leder"].includes(r.name)) ) return guildMember.send(" ',:^\\ ");
const message = reaction.message;
const kanal = reaction.message.guild.channels.find('name', 'sitater');
var embed = new Discord.RichEmbed()
.setDescription(message.content)
.setTimestamp()
.setFooter(message.author.username, reaction.message.author.avatarURL)
.setImage()
kanal.send({embed});
}
});
You can simply use the .attachments property from the message object. This returns you all attachments of this message included all pictures. From there on, you can use the property .url and this gives you the image url that you can add in your embed.
In the following code I added a check if there are even attachments. If yes, add the first image url as image of the embed.
Here is the code:
client.on('messageReactionAdd', (reaction, user, message) => {
const eggsa = client.emojis.find(emoji => emoji.name === 'eggsa');
if (reaction.emoji.name === 'eggsa') {
const msg = reaction.message;
const guild = msg.guild;
const guildMembers = guild.members;
const guildMember = guildMembers.get(user.id);
if (!guildMember.roles.some(r => ['Leder'].includes(r.name))) return guildMember.send(" ',:^\\ ");
const message = reaction.message;
const kanal = reaction.message.guild.channels.find('name', 'sitater');
const embed = new Discord.RichEmbed()
.setDescription(message.content)
.setTimestamp()
.setFooter(message.author.username, reaction.message.author.avatarURL);
if (message.attachments.size !== 0) {
embed.setImage(message.attachments.first().url);
}
kanal.send({ embed });
}
});

Resources