簡體   English   中英

如何在discord.js中制作反應角色?

[英]How to make reaction roles in 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();

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

希望可以有人幫幫我!

您可以使用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');

這是實時預覽:

圖片

你也可以使用這個包來做一些其他很酷的事情! 像嵌入分頁器,問題,是/否問題......你可以在這里找到它們!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM