首页 > 解决方案 > 如何在 discord.js 中制作反应角色?

问题描述

我想知道如何在我的 discord.js 机器人中集成反应角色。我已经尝试过传统的方法,messageReactionAdd但似乎很难让它可扩展和可编辑,而且在我的机器人在许多行会中之后它就变得不可用了......我一直在尝试寻找能够实现这一点的节点模块,但是我唯一发现的是这个,当试图将它集成到我的机器人中时,它只是让我的命令和东西不再起作用,我试图阅读如何使用该包制作反应角色,但无济于事,没有任何效果。 ..我确实尝试过:

const ReactionRole = require("reaction-role");
const system = new ReactionRole("my-token");

let option1 = system.createOption("x:697809640147105878", "697809380137107478"); 
let option2 = system.createOption("emoji-1:720843460158698152", "708355720436777033");
let option3 = system.createOption("pepe:720623437466435626", "703908514887761930");

system.createMessage("725572782157898450", "702115562158948432", 2, null, option1, option2, option3);

system.init();

但正如我所说,它使我所有的命令都无法使用......

希望可以有人帮帮我!

标签: javascriptnode.jsdiscorddiscord.js

解决方案


您可以使用discord.js-collector 包轻松实现这一点,它支持MongoDB数据库,因此您不再需要手动编辑机器人!

这是如何做到的:

const { ReactionRoleManager } = require('discord.js-collector'); //We import the discord.js-collector package that'll make reaction roles possible
const { Client } = require('discord.js'); // We import the client constructor to initialize a new client
const client = new Client(); //We create a new client

const reactionRoleManager = new ReactionRoleManager(client, {
 //We create a reaction role manager that'll handle everything related to reaction roles
 storage: true, // Enable reaction role store in a Json file
 path: __dirname + '/roles.json', // Where will save the roles if store is enabled
 mongoDbLink: 'url mongoose link', // See here to see how setup mongoose: https://github.com/IDjinn/Discord.js-Collector/tree/dev/examples/reaction-role-manager/Note.md & https://medium.com/@LondonAppBrewery/how-to-download-install-mongodb-on-windows-4ee4b3493514
});

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

client.on('message', async (message) => {
 const client = message.client;
 const args = message.content.split(' ').slice(1);
 // Example
 // >createReactionRole @role :emoji: MessageId
 if (message.content.startsWith('>createReactionRole')) {
  const role = message.mentions.roles.first();
  if (!role)
   return message
    .reply('You need mention a role')
    .then((m) => m.delete({ timeout: 1_000 }));

  const emoji = args[1];
  if (!emoji)
   return message
    .reply('You need use a valid emoji.')
    .then((m) => m.delete({ timeout: 1_000 }));

  const msg = await message.channel.messages.fetch(args[2] || message.id);
  if (!role)
   return message
    .reply('Message not found!')
    .then((m) => m.delete({ timeout: 1_000 }));

  reactionRoleManager.addRole({
   message: msg,
   role,
   emoji,
  });
  message.reply('Done').then((m) => m.delete({ timeout: 500 }));
 }
});

client.login('Token');

这是一个实时预览:

图片

你也可以使用这个包来做一些其他非常酷的事情!像嵌入分页器,问题,是/否问题......你可以在这里找到它们!


推荐阅读