如何让机器人发送个性化的表情符号?

2022-01-10 00:00:00 discord node.js javascript discord.js

嗯,我目前正在使用表情符号 :x:,但在我的服务器上我有一个名为 :superbotxemoji 的表情符号:我只是不知道我是怎么做到的让我的机器人使用它

我的代码:

const Discord = require('discord.js');模块.exports = {名称:'说',描述:'说',执行(消息,参数){const { 前缀,令牌 } = require('../config.json');if (!message.member.hasPermission('ADMINISTRATOR'))返回 message.channel.send({嵌入:{颜色:16777201,描述:`:x:|${message.author}, 你不能使用这个命令.`,页脚:{文本:` |所需权限:管理员`,},},});如果(!args.length)返回 message.channel.send({嵌入:{颜色:16777201,描述:`:x:|${message.author}, 你需要发消息.`,页脚:{文本:` |示例:!打个招呼`,},},});const sayMessage = args.join(' ');message.delete({ timeout: 1 });message.channel.send(sayMessage);},};

解决方案

其实官方discord.js指南里面有非常详细的解释,你可以找到 集合和 .find() 方法.

client.emojis.cache.find(emoji => emoji.name === '<表情符号的名称>')

此方法还可以发送自定义表情符号,但是这次您可以通过名称找到它们.请注意,如果给定名称的表情符号不止一个,它将不起作用.

绕过这个问题的一种方法是查看 guild.emojis.cache 集合.这样可以减少可能重复的表情符号数量.

Well, I'm currently using the emoji :x:, but on my server I have an emoji called :superbotxemoji: I just don't know how I get my bot to use it

My code:

const Discord = require('discord.js');

module.exports = {
 name: 'say',
 description: 'say',
 execute(message, args) {
  const { prefix, token } = require('../config.json');

  if (!message.member.hasPermission('ADMINISTRATOR'))
   return message.channel.send({
    embed: {
     color: 16777201,
     description: `:x: | ${message.author}, You are not allowed to use this command.`,
     footer: {
      text: `   | Required permission: ADMINISTRATOR`,
     },
    },
   });

  if (!args.length)
   return message.channel.send({
    embed: {
     color: 16777201,
     description: `:x: | ${message.author}, You need to put a message.`,
     footer: {
      text: `   | Example: !say hello`,
     },
    },
   });

  const sayMessage = args.join(' ');
  message.delete({ timeout: 1 });
  message.channel.send(sayMessage);
 },
};

解决方案

There is actually a very detailed explanation from the official discord.js guide which you can find here, although I'll try to paraphrase it.

To send a custom emoji, you must get that emoji's unique ID. To find that, you must send the emote in discord with a backslash in front of it; essentially escaping the emoji.

This will result in the emojis unique ID in this format: <:emoji-name:emoji-id>

If you paste this special string into a message, the bot will send the emoji. However, the emoji must be from a guild the bot is part of.

On the other hand, there's another very easy way to get an emoji using the client.emojis.cache collection and the .find() method.

client.emojis.cache.find(emoji => emoji.name === '<name of emoji>')

This method will also make it possible to send custom emojis, however, this time you can find them by name. Be careful, if there are more than one emojis by the given name, it will not work.

A way to bypass this problem would be looking at a guild.emojis.cache collection. This way the amount of possible duplicate emojis would be narrowed down.

相关文章