如何在 Discord.js 中按名称查找表情符号
How Find Emojis By Name In Discord.js
因此,过去几天我一直非常沮丧,因为我无法在网上找到一个资源来正确记录在 javascript 中编写不和谐机器人时如何查找表情符号。我一直在参考本指南,其关于表情符号的文档似乎有误或已过时:
https://anidiots.guide/coding-guides/using-emojis
我需要的很简单;只是能够使用 .find()
函数引用表情符号并将其存储在变量中。这是我当前的代码:
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。整个 bean 只是一个测试
但每次我 运行 这段代码它只是 returns 这个错误然后死掉:
(node:3084) DeprecationWarning: Collection#find: pass a function instead
有什么我遗漏的吗?我好难过...
我从来没有用过discord.js
所以我可能完全错了
从警告中我会说你需要做类似
的事情
client.emojis.find(emoji => emoji.name === "bean")
再加上看了 Discord.js Doc
之后,这似乎是要走的路。但是文档从来没有说过 client.emojis.find("name", "bean")
是错误的
我已经修改了你的代码。
希望对您有所帮助!
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);
}
}
}
});
万一像我这样的人在寻找答案时发现这个问题,在 v12 中你将不得不添加缓存,使其看起来像这样:
var bean = message.guild.emojis.cache.find(emoji => emoji.name == 'bean');
而不是:
var bean = message.guild.emojis.find(emoji => emoji.name == 'bean');
请查看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
.
在这种特定情况下,您需要将缓存对象添加到表达式中:
var bean = message.guild.emojis.cache?.find(emoji => emoji.name == 'bean');
因此,过去几天我一直非常沮丧,因为我无法在网上找到一个资源来正确记录在 javascript 中编写不和谐机器人时如何查找表情符号。我一直在参考本指南,其关于表情符号的文档似乎有误或已过时:
https://anidiots.guide/coding-guides/using-emojis
我需要的很简单;只是能够使用 .find()
函数引用表情符号并将其存储在变量中。这是我当前的代码:
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。整个 bean 只是一个测试
但每次我 运行 这段代码它只是 returns 这个错误然后死掉:
(node:3084) DeprecationWarning: Collection#find: pass a function instead
有什么我遗漏的吗?我好难过...
我从来没有用过discord.js
所以我可能完全错了
从警告中我会说你需要做类似
的事情client.emojis.find(emoji => emoji.name === "bean")
再加上看了 Discord.js Doc
之后,这似乎是要走的路。但是文档从来没有说过 client.emojis.find("name", "bean")
是错误的
我已经修改了你的代码。
希望对您有所帮助!
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);
}
}
}
});
万一像我这样的人在寻找答案时发现这个问题,在 v12 中你将不得不添加缓存,使其看起来像这样:
var bean = message.guild.emojis.cache.find(emoji => emoji.name == 'bean');
而不是:
var bean = message.guild.emojis.find(emoji => emoji.name == 'bean');
请查看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 likeClient#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 asGuildMemberManager#fetch
andMessageManager#delete
.
在这种特定情况下,您需要将缓存对象添加到表达式中:
var bean = message.guild.emojis.cache?.find(emoji => emoji.name == 'bean');