1
votes

So I have been utterly frustrated these past few days because I have not been able to find a single resource online which properly documents how to find emojis when writing a discord bot in javascript. I have been referring to this guide whose documentation about emojis seems to be either wrong, or outdated:

https://anidiots.guide/coding-guides/using-emojis

What I need is simple; to just be able to reference an emoji using the .find() function and store it in a variable. Here is my current code:

const Discord = require("discord.js");
const config = require("./config.json");
const fs = require("fs");
const client = new Discord.Client();
const guild = new Discord.Guild();
const bean = client.emojis.find("name", "bean");

client.on("message", (message) => {
    if (bean) {
      if (!message.content.startsWith("@")){
        if (message.channel.name == "bean" || message.channel.id == "478206289961418756") {
            if (message.content.startsWith("<:bean:" + bean.id + ">")) {
                message.react(bean.id);
            }
        }
      }
    }
    else {
      console.error("Error: Unable to find bean emoji");
    }
});

p.s. the whole bean thing is just a test

But every time I run this code it just returns this error and dies:

(node:3084) DeprecationWarning: Collection#find: pass a function instead

Is there anything I missed? I am so stumped...

4
from the warning I'd say you need to do something like client.emojis.find(emoji => emoji.name === "bean"). I never used discord.js so I may be completely wrongjonatjano
@jonatjano So I tried what you wrote there and the error seems to be gone but it's still dying : /Zyad-O
you may need to have it set in your event, I don't think these collection are dynamics, meaning the bean var doesn't get the new values as they come but keep the initials values onlyjonatjano

4 Answers

4
votes

I never used discord.js so I may be completely wrong

from the warning I'd say you need to do something like

client.emojis.find(emoji => emoji.name === "bean") 

Plus after looking at the Discord.js Doc it seems to be the way to go. BUT the docs never say anything about client.emojis.find("name", "bean") being wrong

2
votes

I've made changes to your code.

I hope it'll help you!

const Discord = require("discord.js");
const client = new Discord.Client();


client.on('ready', () => {
	console.log('ready');
});


client.on('message', message => {
	var bean = message.guild.emojis.find(emoji => emoji.name == 'bean');
	// By guild id 
	if(message.guild.id == 'your guild id') {
	if(bean) {
    		if(message.content.startsWith("<:bean:" + bean.id + ">")) {
    			message.react(bean.id);
    		}
    	}
}
});
1
votes

Please check out the switching to v12 discord.js guide

v12 introduces the concept of managers, you will no longer be able to directly use collection methods such as Collection#get on data structures like Client#users. You will now have to directly ask for cache on a manager before trying to use collection methods. Any method that is called directly on a manager will call the API, such as GuildMemberManager#fetch and MessageManager#delete.

In this specific situation, you need to add the cache object to your expression:

var bean = message.guild.emojis.cache?.find(emoji => emoji.name == 'bean');
0
votes

In case anyone like me finds this while looking for an answer, in v12 you will have to add cache in, making it look like this:

var bean = message.guild.emojis.cache.find(emoji => emoji.name == 'bean');

rather than:

var bean = message.guild.emojis.find(emoji => emoji.name == 'bean');