Markoudstaal / node-red-contrib-discord-advanced

Recieve, send, edit and delete Discord messages in node-red.
MIT License
47 stars 16 forks source link

addGuildMemberRole in discordPermission #62

Closed javis86 closed 1 year ago

javis86 commented 1 year ago

is addGuildMemberRole in any way possible?

From discord user Ikuria: https://pastebin.com/90KNVSCg

  module.exports = function (RED) {
  var discordBotManager = require('./lib/discordBotManager.js');

  function discordPermissions(config) {
    RED.nodes.createNode(this, config);
    var node = this;
    var configNode = RED.nodes.getNode(config.token);
    discordBotManager.getBot(configNode).then(function (bot) {
      node.on('input', function (msg, send, done) {
        const action = msg.action || 'get';
        const user = msg.user || null;
        const guild = msg.guild || null;
        const role = msg.role || null;

        const setError = (error) => {
          node.status({
            fill: "red",
            shape: "dot",
            text: error
          })
          done(error);
        }

        const setSuccess = (succesMessage) => {
          node.status({
            fill: "green",
            shape: "dot",
            text: succesMessage
          });
          done();
        }

        const checkIdOrObject = (check) => {
          try {
            if (typeof check !== 'string') {
              if (check.hasOwnProperty('id')) {
                return check.id;
              } else {
                return false;
              }
            } else {
              return check;
            }
          } catch (error) {
            return false;
          }
        }

        const sendRoles = () => {
          const userID = checkIdOrObject(user);
          const guildID = checkIdOrObject(guild);

          if (!userID) {
            setError(`msg.user wasn't set correctly`);
          } else if (!guildID) {
            setError(`msg.guild wasn't set correctly`);
          } else {
            bot.guilds.fetch(guildID).then(guild => {
              return guild.members.fetch(userID);
            }).then(user => {
              try {
                let roles = [];
                user.roles.cache.each(role => {
                  roles.push(role);
                });
                msg.payload = roles;
              } catch (error) {
                setError(error);
              }
              msg.user = user;
              send(msg);
              setSuccess(`roles sent`);
            }).catch(error => {
              setError(error);
            });
          }
        }

        const setRole = () => {
          const userID = checkIdOrObject(user);
          const guildID = checkIdOrObject(guild);
          const roleID = checkIdOrObject(role);

          if (!userID) {
            setError(`msg.user wasn't set correctly`);
          } else if (!guildID) {
            setError(`msg.guild wasn't set correctly`);
          } else if (!roleID) {
            setError(`msg.role wasn't set correctly`);
          } else {
            bot.guilds.fetch(guildID).then(guild => {
              guild.members.fetch(userID).then(user => {
                  try {
            guild.members.cache.get(userID).roles.add(role);
                    msg.payload = "role set";
                    send(msg);
                    setSuccess(`role set`);
                  } catch (error) {
                    setError(error);
                  }
              });
            }).catch(error => {
              setError(error);
            });
          }
        }

        switch (action.toLowerCase()) {
          case 'get':
            sendRoles();
            break;
          case 'set':
            setRole();
            break;
          default:
            setError(`msg.action has an incorrect value`)
        }
      });

      node.on('close', function () {
        discordBotManager.closeBot(bot);
      });
    }).catch(err => {
      console.log(err);
      node.status({
        fill: "red",
        shape: "dot",
        text: err
      });
    });
  }
  RED.nodes.registerType("discordPermissions", discordPermissions);
}
javis86 commented 1 year ago

Removing roles added too

javis86 commented 1 year ago

Some examples for testing:

[
    {
        "id": "efb879b82de43ec5",
        "type": "discordPermissions",
        "z": "ca68a7529a9fc04d",
        "name": "Get Roles",
        "token": "",
        "x": 320,
        "y": 40,
        "wires": [
            [
                "560c10d50b51f1f9"
            ]
        ]
    },
    {
        "id": "560c10d50b51f1f9",
        "type": "debug",
        "z": "ca68a7529a9fc04d",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 530,
        "y": 40,
        "wires": []
    },
    {
        "id": "71e71bceb6718bc6",
        "type": "inject",
        "z": "ca68a7529a9fc04d",
        "name": "",
        "props": [
            {
                "p": "user",
                "v": "395396700082274324",
                "vt": "str"
            },
            {
                "p": "guild",
                "v": "830549562909130803",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 170,
        "y": 40,
        "wires": [
            [
                "efb879b82de43ec5"
            ]
        ]
    },
    {
        "id": "e7fad3baf462b856",
        "type": "discordPermissions",
        "z": "ca68a7529a9fc04d",
        "name": "Set Role",
        "token": "",
        "x": 320,
        "y": 100,
        "wires": [
            [
                "13b5ca756e31e52b"
            ]
        ]
    },
    {
        "id": "13b5ca756e31e52b",
        "type": "debug",
        "z": "ca68a7529a9fc04d",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 530,
        "y": 100,
        "wires": []
    },
    {
        "id": "d2a8abba2b86310a",
        "type": "inject",
        "z": "ca68a7529a9fc04d",
        "name": "",
        "props": [
            {
                "p": "user",
                "v": "759860816857530369",
                "vt": "str"
            },
            {
                "p": "guild",
                "v": "830549562909130803",
                "vt": "str"
            },
            {
                "p": "action",
                "v": "set",
                "vt": "str"
            },
            {
                "p": "role",
                "v": "905806871225565204",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 170,
        "y": 100,
        "wires": [
            [
                "e7fad3baf462b856"
            ]
        ]
    },
    {
        "id": "d3cabe323c55cd7c",
        "type": "discordPermissions",
        "z": "ca68a7529a9fc04d",
        "name": "Remove Role",
        "token": "",
        "x": 340,
        "y": 160,
        "wires": [
            [
                "b204b3ba5ebd0639"
            ]
        ]
    },
    {
        "id": "b204b3ba5ebd0639",
        "type": "debug",
        "z": "ca68a7529a9fc04d",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 530,
        "y": 160,
        "wires": []
    },
    {
        "id": "8d7fb6cfbdfbe7af",
        "type": "inject",
        "z": "ca68a7529a9fc04d",
        "name": "",
        "props": [
            {
                "p": "user",
                "v": "759860816857530369",
                "vt": "str"
            },
            {
                "p": "guild",
                "v": "830549562909130803",
                "vt": "str"
            },
            {
                "p": "action",
                "v": "remove",
                "vt": "str"
            },
            {
                "p": "role",
                "v": "905806871225565204",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 170,
        "y": 160,
        "wires": [
            [
                "d3cabe323c55cd7c"
            ]
        ]
    }
]