Skip to content

Instantly share code, notes, and snippets.

@tuvior
Created March 16, 2018 16:39
Show Gist options
  • Save tuvior/b549e723599ce59cb35c015b6770f541 to your computer and use it in GitHub Desktop.
Save tuvior/b549e723599ce59cb35c015b6770f541 to your computer and use it in GitHub Desktop.
example for EasternGamer#7339
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
import sx.blah.discord.handle.obj.IGuild;
import sx.blah.discord.handle.obj.IRole;
import sx.blah.discord.handle.obj.IUser;
import java.util.LinkedHashMap;
import java.util.List;
public class RolePrefixes {
// LinkedHashMap maintains the order in which you add elements
// you will initialise this at startup of your bot possibly
public static LinkedHashMap<IRole, String> rolesPrefixes;
public void updateEveryone(MessageReceivedEvent event) {
IGuild guild = event.getGuild();
List<IUser> users = guild.getUsers();
// this operation will be applied to all users of the guild
users.forEach(user -> {
// here we go through all roles with a prefix in order, and get the first one that our user has
// in case the user has none of these roles, we return null
IRole userRoleWithPrefix = rolesPrefixes.keySet().stream().filter(role -> user.hasRole(role)).findFirst().orElse(null);
if (userRoleWithPrefix != null) {
// we get the prefix from our map
String prefix = rolesPrefixes.get(userRoleWithPrefix);
// assuming there's a space between prefix and name
guild.setUserNickname(user, prefix + " " + user.getName());
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment