Git Product home page Git Product logo

discordjs-reaction-role's Introduction

discordjs-reaction-role

This is a pluggable package for Discord.js applications and bots. It will add or remove a member from a specific role whenever the user reacts or unreacts to a specific message using a specific reaction.

Example use cases:

  • If a member reacts with πŸ”” to a message, the member opts in to the Announcements role to be notified of updates.
  • If a member reacts with βœ… to a message, the member gets verified as human.
  • Use different roles to have different clans or groups in your server.
  • Have different roles such as "Likes Cats", "Likes Dogs", "Likes Ducks" and let a member add to as many roles as desired to indicate their preferences.

Requirements

A JavaScript project already depending on Discord.js 13 or Discord.js 14.

Still using Discord.js 12 and having a hard time upgrading? πŸ‘‰ Stay using discordjs-reaction-role 1.0.2, the last version supporting Discord.js 12. You can pin it in your package.json like this:

"discordjs-reaction-role": "<2.0.0"

Sorry for stating this, but consider upgrading your bot.

How to use

NOTE: If you want a more detailed example, check the examples/ directory. This is useful to see the partials and intents required to run the bot too, which are:

  • Partials: MESSAGE and REACTION.
  • Intents: GUILD_MESSAGE_REACTIONS, GUILD_MESSAGES and GUILDS.

Import the ReactionRole manager to use it.

If you are using Node.js and CommonJS: (You probably are if you don't know):

const { ReactionRole } = require("discordjs-reaction-role");

If you are using ES Modules or TypeScript, you can make use of import:

import { ReactionRole } from "discordjs-reaction-role";

ReactionRole manager

This library exposes a class called ReactionRole that depends on Discord.js' Client class. It tries to be agnostic from any other framework built on top of Discord.js. This way, it should be easy to integrate into your existing bot system.

The constructor expects to be given the Discord.js client and the configuration object. (More on that later.) For instance,

const client = new Client(...);
const rr = new ReactionRole(client, [
  { messageId: "12341234", reaction: "πŸ””", roleId: "5959859595" }, // Basic usage
  { messageId: "12341234", reaction: "βœ…", roleId: "5959859598" }, // Multiple reactions per message!
  { messageId: "12341234", reaction: "784536908345", roleId: "5959859598" }, // Custom emoji by ID
  { messageId: "12341234", reaction: "worry", roleId: "5959859598" }, // Custom emoji by emoji name
]);

As soon as you instantiate the ReactionRole class, messageReactionAdd and messageReactionRemove events will be caught and:

  • When a member reacts using the given emoji to the given message, the member will be added to the role.
  • When a member unreacts using the given emoji to the given message, the member will be removed from the role.

Configuration array

The configuration object is an array of entries. Each entry has three keys:

  • messageId: the ID of the message that the user has to react to.
  • reaction: the emoji that the user has to use, either as the custom emoji ID, the custom emoji name or the Unicode codepoint.
  • roleId: the ID of the role that will be given to the user when reacted.

For instance, if you want that whenever a user reacts with a :+1: to the message with ID 100001, they get the role with ID 300003, you should use the following configuration object:

const config = [
  {
    messageId: "100001",
    roleId: "300003",
    reaction: "πŸ‘",
  },
];

When the reaction is removed, the role is removed.

It is up to you how to set up the configuration object. For instance, in a small server or in a bot designed to be used with a single server, you might just hardcode the settings into the source code of your bot, although this is not recommended. You could use a SettingProvider or read it from database, download the JSON from the internet, or whatever.

Teardown

If you need to remove the events (for instance, to restart the settings), you can call the teardown method:

function stop(role) {
  role.teardown();
}

Known issues

It is difficult to sync the roles and reactions in the following cases:

  • If the bot is offline when someone reacts, the reaction will not be picked. It has to be done manually, or the user should have to un-react and react again.
  • If someone leaves the server after reacting, the reaction will remain, although for obvious reasons the user will not be part of that role anymore.

Copyright and license

ISC License. Happy to hear about cool projects using this library.

Copyright 2021-2022 Dani RodrΓ­guez

Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

discordjs-reaction-role's People

Contributors

danirod avatar dependabot-preview[bot] avatar dependabot[bot] avatar leonissenbaum avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

discordjs-reaction-role's Issues

`TypeError: Cannot read property 'then' of undefined`

I keep getting this, and it seems to be caused by the code assuming that extractRole will always return a role, but not all reactions users make are related to role pickup, so this keeps spamming the console. The promise should probably return Role?, and the return value of the functions should be stored into a variable and checked before executing .then on it. For optimization it could be moved before the member fetch, to return without any API requests in case the reaction is not configured for any roles in the config at all

[Feature Request] Add roles based on existing reaction

It would be very cool if we could execute a function that would go through the existing reactions on the configured messages, and assign any roles that are missing based on those. Use case is that our bot may have been offline for some time, or we're migrating from another bot that missed many reactions. This could ensure there's nothing missing, I think it would make sense to have such an option

TypeError: ReactionRole is not a constructor WITH ES MODULES

MY NODE VERSION:node v16.14.2,
OS:WINDOWS:10,
discordjs-reaction-role: "^2.1.1",
discord.js: "^13.6.0",

import { Client, Intents } from "discord.js";
import ReactionRole from 'discordjs-reaction-role';

import config from './config/config.json' assert { type: 'json' };;

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.DIRECT_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS] });

new ReactionRole(client, [{ messageId: "965629960666644520", reaction: "πŸ””", roleId: "965590388788301944" }])
const token = config.token;

client.on("ready", () => {
    // client.channels.cache.get("965574795817254912").send('Hello here!');

    console.log("Ready BOT!");
})

client.login(token)

I cant call the constructor i tried console.log(ReactionRole) after import i found out that the class is in an object with a key called default {default:CLASS} so i call new ReactionRole.default(client, [{ messageId: "965629960666644520", reaction: "πŸ””", roleId: "965590388788301944" }]) The reactions are not registered!Doesnt detect any reactions and roles not assigned!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.