Search code examples
javascriptif-statementdiscord.js

Discord.JS (14.0) Random chance to React with Emoji?


I'm very new to JS, and DiscordJS in general.

I am trying to get my bot to have a CHANCE to react to any message from a specific user, with a specific emoji ID.

The documentation I found says to use "client.emojis.find", but I get a TypeError that tells me this is not a function.

Any help is appreciated!

client.on("messageCreate", (message) => {
  if (message.author.id === "217900978756780032") {
    const otterEmoji = client.emojis.find(
      (emoji) => emoji.id === "1028007226859855914"
    );
    if (Math.random() < 0.5) {
      message.react("otterEmoji");
    }
  }
});

EDIT: This seems to work, but if anyone has other ideas for a cleaner randomizer, let me know!

client.on("messageCreate", (message) => {
  const a = Math.floor(Math.random() * 11);
  if (a >= 8) {
    if (message.author.id === "217900978756780032") {
      message.react("🦦");
    }
  }
});

Solution

  • I think instead of client.emojis, you need to use client.emojis.cache:

    const otterEmoji = client.emojis.cache.find((emoji) => emoji.id = '1028007226859855914'
    

    (based on https://github.com/AnIdiotsGuide/discordjs-bot-guide/blob/master/coding-guides/using-emojis.md#how-does-discordjs-store-emojis)