Muffinz
Muffinz

Reputation: 35

Trying to show off emoji when it's added using Discord.js

Currently I'm trying to add something to my bot to display text and a preview when an emoji is added. I was pretty close, but it appears the emoji doesn't exist on the server during the emojiCreate event. my code (which is a mess) looks like this currently:

var latestEmojiName = "";
let announcementChannel = "";
client.on("emojiCreate", emoji => {
  announcementChannel = emoji.guild.channels.find(x => x.id === "625678148440162314");
  announcementChannel.send("new emoji has been added:");
  latestEmojiName = emoji.name;
  newEmojiFollowup1();
});

function newEmojiFollowup1() {
  setTimeout(newEmojiFollowup2, 2000);
}

function newEmojiFollowup2() {
  announcementChannel.send(client.guilds.find(x => x.id === "607642928872947724").emojis.find(x => x.name === latestEmojiName));
}

Upvotes: 1

Views: 590

Answers (1)

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48630

Ok, I added the following listener to one of my bots and it worked. Also, there is no need to look-up the guild because you do not need a timeout. The emoji object has all you need already.

You need to send: <:${emoji.name}:${emoji.id}>

Also, use let instead of var to resolve scoping issues and there is no need for all the "follow-on functions.

// Verified with [email protected]
const channelId = "625678148440162314" // e.g. Channel: #general
client.on("emojiCreate", emoji => {
  const channel = emoji.guild.channels.find(x => x.id === channelId)
  channel.send("A new emoji has been added:")
  channel.send(`<:${emoji.name}:${emoji.id}>`)
})

You can also send a rich-embed message:

// Verified with [email protected]
const channelId = "625678148440162314" // e.g. Channel: #general
client.on("emojiCreate", emoji => {
  const channel = emoji.guild.channels.find(x => x.id === channelId)
  const embed = new RichEmbed()
    .setTitle("A new emoji has been added!")
    .setColor(0x222222)
    .setDescription("Preview:")
  embed.addField(`:${emoji.name}:`, `<:${emoji.name}:${emoji.id}>`, true)
  channel.send(embed)
})

Upvotes: 1

Related Questions