Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toggle role of user. #70

Open
TheRealKev1n opened this issue Jun 18, 2016 · 5 comments
Open

toggle role of user. #70

TheRealKev1n opened this issue Jun 18, 2016 · 5 comments

Comments

@TheRealKev1n
Copy link

Hello,

I got 2 groups member and vip
is it possible that the bot adds vip to a user with a command? and remove it with a command

Regards,
Kevin

@Einarin
Copy link
Collaborator

Einarin commented Sep 20, 2016

This is possible, but the bot doesn't do it yet.

@ghost
Copy link

ghost commented Nov 3, 2020

I created a command, but until now, it only adds the role to myself. Need to do some more stuff to make it work as its expected from @TheRealKev1n.

@ghost
Copy link

ghost commented Nov 3, 2020

its not even good code, but here it is:

exports.toggleRole = {
	usage: "<role (name or id)>",
	description: "toggles a role of a user.",
	process: function(bot,msg,suffix) {
		var role_by_name = msg.channel.guild.roles.cache.find(role => role.name === suffix);
		var role_by_id = msg.channel.guild.roles.cache.find(role => role.id === suffix);
		if(role_by_name){
				if(msg.member.roles.cache.find(role => role.name === suffix)){
					msg.member.roles.remove(role_by_name);
					msg.channel.send("Removed your role.");
				}
				else{
					msg.member.roles.add(role_by_name);
					msg.channel.send("Added the role to you.");
				}
		} else if(role_by_id){
			if(msg.member.roles.cache.find(role => role.id === suffix)){
				msg.member.roles.remove(role_by_id);
				msg.channel.send("Removed your role.");
			}
			else{
				msg.member.roles.add(role_by_id);
				msg.channel.send("Added the role to you.");
			}
		} else {
			msg.channel.send("role doesn't exist");
		}
	}
}

@F9Alejandro
Copy link
Collaborator

F9Alejandro commented Nov 3, 2020

I would suggest instead of looking directly at role id or name, look for a role mention in the command args and a user mention. from there it should check to see if the mentioned user has the mentioned role, if so then remove else add.

EDIT 2:

Code above should work, just make sure that you have the role name or id hardcoded somewhere or add it as a var in one of the config files so it can be accessed if you truly want to do a toggle based system. I would better suggest to use the one I am posting in a new comment below, it will work better and has the option of changing different users roles.

I can try to find a sample code from my old repo, it might not work though because of the discord.js change.

EDIT 1:
Code added is not for roles directly and needs some modifying, but has the same idea.

exports.perm = {
    usage: "set <can be @user or @role> or rm <can be @user or @role>",
    description: "Used to set(add) or remove permissions from a @user or @role",
    process: function(bot,msg,arg){
        var args = arg.split(" ");
        var permsFile = require("../../permissions.json");
        var user, role;
        if(args[0] == "set"){
        if(typeof args[1] != 'undefined' && typeof args[2] != 'undefined' && typeof args[3] != 'undefined'){
            var node1 = args[2];
            var perm1;
            if(args[3] == "true"){ perm1 = true; }else{ perm1 = false; }
            try{
                if(msg.mentions.users.first()){ console.log('User mention'); user = msg.mentions.users.first().id; }
                if(msg.mentions.roles.first()){ console.log('Role mention '+msg.mentions.roles.first().id); role = msg.mentions.roles.first().id; }
            }catch(err){ console.log('Error occured at: '+err+' Mentions.first() is undefined'); }
            if(typeof user != 'undefined'){
                if(typeof permsFile.users[user] == 'undefined'){ permsFile.users[user] = {[node1]:perm1}; }
                else{ permsFile.users[user][node1] = perm1; }
            }else if(typeof role != 'undefined'){
                if(typeof permsFile.roles[role] == 'undefined'){ permsFile.roles[role] = {[node1]:perm1}; }
                else{ permsFile.roles[role][node1] = perm1; }
            }
        }else{ msg.channel.send('__Parameters can\'t be left blank: '+typeof args[1]+', '+typeof args[2]+', '+typeof args[3]+'__'); }
        }else if(args[0] == "rm"){
            if(typeof args[1] != 'undefined' && typeof args[2] != 'undefined'){
                var node1 = args[2];
                try{
                    if(msg.mentions.users.first()){ user = msg.mentions.users.first().id; }
                    if(msg.mentions.roles.first()){ role = msg.mentions.roles.first().id; }
                }catch(err){ console.log('Error occured at: '+err+' Mentions.first() is undefined'); }
                if(typeof user != 'undefined'){ delete permsFile.users[user][node1]; }
                else if(typeof role != 'undefined'){ delete permsFile.roles[role][node1]; }
            }else{ msg.channel.send('__Parameters can\'t be left blank: '+typeof args[1]+', '+typeof args[2]+', '+typeof args[3]+'__'); }
        }else{ msg.channel.send("__Too few Arguments: either add or rm.__"); }
        try{
            if(fs.lstatSync("./permissions.json").isFile()){ console.log("WARNING: permissions.json found but we couldn't read it!\n" + e.stack); }
        } catch(e2){
            if (!fs.writeFile("./permissions.json",JSON.stringify(permsFile,null,2))){
                //console.log('Write success!');
            }}
    }

@F9Alejandro
Copy link
Collaborator

F9Alejandro commented Nov 3, 2020

Here is a perm based toggle of a mentioned user and role

exports.perm = {
    usage: "<@user> <@role>",
    description: "Used to toggle @role of specified @user",
    process: function(bot,msg,arg){
        var args = arg.split(" ");
        var user, role;
        if(typeof args[0] != 'undefined' && typeof args[1] != 'undefined'){
            try{
                if(msg.mentions.members.first()){ console.log('User mention '+msg.mentions.members.first().id); user = msg.mentions.members.first(); }
                if(msg.mentions.roles.first()){ console.log('Role mention '+msg.mentions.roles.first().id); roleid = msg.mentions.roles.first().id; }
            }catch(err){ console.log('Error occured at: '+err+' Mentions.first() is undefined'); }
				if(user.roles.cache.find(role => role.id === roleid)){
					user.roles.remove(roleid);
					msg.channel.send("Removed the role from <@!"+user+">.");
				}
				else{
					user.roles.add(roleid);
					msg.channel.send("Added the role to <@!"+user+">.");
				}
        } else{ msg.channel.send('__Parameters can\'t be left blank: '+typeof args[0]+', '+typeof args[1]+'__'); }else{ msg.channel.send("__Too few Arguments.__"); }
	}
}

F9Alejandro added a commit to F9Alejandro/DiscordBot that referenced this issue Nov 3, 2020
 On branch role-perm
 Changes to be committed:
	modified:   plugins/Admin/admin.js
@F9Alejandro F9Alejandro linked a pull request Nov 3, 2020 that will close this issue
@F9Alejandro F9Alejandro changed the title Add group to user. toggle role of user. Nov 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants