Remove a Discord Role (bumped)

javascript
commands
roles
discord

#1

So I recently set up the setRole function to work properly on discord by using the user’s Discord mention ID (<@!discordID>) like so:

var role = 'Cool: Beans',
discordUser = event.getDiscordUser();
$.discord.say(channel, '*Gave role* ***' + role + '*** to *' + discordUser + '.*');
$.discord.setRole(role, discordUser);

And it works well! The story is a bit different from removeRole though.
After some tweaks in (discord/core) misc.js (and the removeRole function clearly being in the discordAPI list https://github.com/PhantomBot/PhantomBot/blob/master/source/tv/phantombot/discord/util/DiscordUtil.java) I tried doing:

var role = 'Cool: Beans',
discordUser = event.getDiscordUser();
$.discord.say(channel, '*Removed role* ***' + role + '*** from *' + discordUser + '.*');
$.discord.removeRole(role, discordUser);

And it did not work. I don’t have any errors in consoles indicating the command didn’t work.

The tweaks in misc.js are:

function setRole(role, username) {
	return $.discordAPI.addRole(role, username);
}

function removeRole(role, username) {
	return $.discordAPI.removeRole(role, username);
}

and:

$.discord = {
	getUserMention: getUserMention,
	userMention: getUserMention,
	removeGame: removeGame,
	userPrefix: userPrefix,
	setStream: setStream,
	setGame: setGame,
	setRole: setRole,
	removeRole: removeRole,
	say: say,
	resolve: { 
		global: getUserMentionOrChannel,
		getUserMentionOrChannel: getUserMentionOrChannel
	},
	username: {
		resolve: getUserMention,
		random: getRandomUser,
		getUserMention: getUserMention,
		getRandomUser: getRandomUser
	}
};

Edit: Link to old thread on Adding a Discord Role Assigning Discord Role on Command


#2

Am I allowed to bump this :face_with_raised_eyebrow:


#3

This is normal, not all core functions are set in the misc.js file, just use the $.discordAPI object to get it.


#4

Still doesn’t work even if I replace

var role = 'Cool: Beans',
discordUser = event.getDiscordUser();
$.discord.say(channel, '*Removed role* ***' + role + '*** from *' + discordUser + '.*');
$.discord.removeRole(role, discordUser);

with

var role = 'Cool: Beans',
discordUser = event.getDiscordUser();
$.discord.say(channel, '*Removed role* ***' + role + '*** from *' + discordUser + '.*');
$.discordAPI.removeRole(role, discordUser);

#5

You’ll have to either use the role name and username or the role object and username object. There’s no method to support both format.


#6

Try this $.discordAPI.removeRole($.discordAPI.getRole('role name'), event.getDiscordUser())

You can check how the methods work here: https://github.com/PhantomBot/PhantomBot/blob/master/source/tv/phantombot/discord/util/DiscordUtil.java


#7

That worked TY, but I don’t understand why.
Isn’t there already a method that has the getRole inside removeRole?

public void removeRole(String roleName, String userName) {
    removeRole(getRole(roleName), getUser(userName));
}

or does it need to be

public void removeRole(String roleName, String userName) {
    removeRole(getRole(roleName), userName);

}

because I already have the mention as String userName?


#8

Yes, but you’re passing the username object (event.getDiscordUser()), which isn’t a string, it’s a class, so you’re confusing Rhino and it’s probably not doing anything at all in the end.


#9

Ohhh, that makes more sense. I thought it returned a string in the format “<@!34234234234324>”.
Now it makes sense. Thanks again!


#10

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.