Role color config, removed role debug, role fixes

This commit is contained in:
Norbi Peti 2020-08-30 02:23:21 +02:00
parent 6bf91afab9
commit 80a0312b1f
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
2 changed files with 22 additions and 24 deletions

View file

@ -23,7 +23,7 @@ import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
* Automatically collects roles with a certain color (the second to last in the upper row - #95a5a6). * Automatically collects roles with a certain color.
* Users can add these roles to themselves using the /role Discord command. * Users can add these roles to themselves using the /role Discord command.
*/ */
public class GameRoleModule extends Component<DiscordPlugin> { public class GameRoleModule extends Component<DiscordPlugin> {
@ -32,7 +32,7 @@ public class GameRoleModule extends Component<DiscordPlugin> {
@Override @Override
protected void enable() { protected void enable() {
getPlugin().getManager().registerCommand(new RoleCommand(this)); getPlugin().getManager().registerCommand(new RoleCommand(this));
GameRoles = DiscordPlugin.mainServer.getRoles().filterWhen(r -> isGameRole(r, false)).map(Role::getName).collect(Collectors.toList()).block(); GameRoles = DiscordPlugin.mainServer.getRoles().filterWhen(this::isGameRole).map(Role::getName).collect(Collectors.toList()).block();
} }
@Override @Override
@ -47,6 +47,15 @@ public class GameRoleModule extends Component<DiscordPlugin> {
return DPUtils.channelData(getConfig(), "logChannel"); return DPUtils.channelData(getConfig(), "logChannel");
} }
/**
* The role color that is used by game roles.
* Defaults to the second to last in the upper row - #95a5a6.
*/
private final ReadOnlyConfigData<Color> roleColor = getConfig().<Color>getConfig("roleColor")
.def(new Color(149, 165, 166, 0))
.getter(rgb -> new Color(Integer.parseInt(((String) rgb).substring(1))))
.setter(color -> "#" + Integer.toHexString(color.getRGB())).buildReadOnly();
public static void handleRoleEvent(RoleEvent roleEvent) { public static void handleRoleEvent(RoleEvent roleEvent) {
val grm = ComponentManager.getIfEnabled(GameRoleModule.class); val grm = ComponentManager.getIfEnabled(GameRoleModule.class);
if (grm == null) return; if (grm == null) return;
@ -58,7 +67,7 @@ public class GameRoleModule extends Component<DiscordPlugin> {
Role role = ((RoleCreateEvent) roleEvent).getRole(); Role role = ((RoleCreateEvent) roleEvent).getRole();
if (notMainServer.test(role)) if (notMainServer.test(role))
return; return;
grm.isGameRole(role, false).flatMap(b -> { grm.isGameRole(role).flatMap(b -> {
if (!b) if (!b)
return Mono.empty(); //Deleted or not a game role return Mono.empty(); //Deleted or not a game role
GameRoles.add(role.getName()); GameRoles.add(role.getName());
@ -83,7 +92,7 @@ public class GameRoleModule extends Component<DiscordPlugin> {
Role or = event.getOld().get(); Role or = event.getOld().get();
if (notMainServer.test(or)) if (notMainServer.test(or))
return; return;
grm.isGameRole(event.getCurrent(), true).flatMap(b -> { grm.isGameRole(event.getCurrent()).flatMap(b -> {
if (!b) { if (!b) {
if (GameRoles.remove(or.getName()) && logChannel != null) if (GameRoles.remove(or.getName()) && logChannel != null)
return logChannel.flatMap(ch -> ch.createMessage("Removed " + or.getName() + " as a game role because its color changed.")); return logChannel.flatMap(ch -> ch.createMessage("Removed " + or.getName() + " as a game role because its color changed."));
@ -104,25 +113,14 @@ public class GameRoleModule extends Component<DiscordPlugin> {
} }
} }
private Mono<Boolean> isGameRole(Role r, boolean debugMC) { private Mono<Boolean> isGameRole(Role r) {
boolean debug = debugMC && r.getName().equalsIgnoreCase("Minecraft"); if (r.getGuildId().asLong() != DiscordPlugin.mainServer.getId().asLong())
if (debug) TBMCCoreAPI.sendDebugMessage("Checking if Minecraft is a game role...");
if (r.getGuildId().asLong() != DiscordPlugin.mainServer.getId().asLong()) {
if (debug) TBMCCoreAPI.sendDebugMessage("Not in the main server: " + r.getGuildId().asString());
return Mono.just(false); //Only allow on the main server return Mono.just(false); //Only allow on the main server
} val rc = roleColor.get();
val rc = new Color(149, 165, 166, 0); System.out.println("Needed role color: " + rc);
if (debug) TBMCCoreAPI.sendDebugMessage("Game role color: " + rc + " - MC color: " + r.getColor()); return Mono.just(r.getColor().equals(rc)).filter(b -> b).flatMap(b ->
return Mono.just(r.getColor().equals(rc)) DiscordPlugin.dc.getSelf().flatMap(u -> u.asMember(DiscordPlugin.mainServer.getId()))
.doAfterSuccessOrError((b, e) -> { .flatMap(m -> m.hasHigherRoles(Collections.singleton(r)))) //Below one of our roles
if (debug) TBMCCoreAPI.sendDebugMessage("1. b: " + b + " - e: " + e); .defaultIfEmpty(false);
}).filter(b -> b).flatMap(b ->
DiscordPlugin.dc.getSelf().flatMap(u -> u.asMember(DiscordPlugin.mainServer.getId()))
.doAfterSuccessOrError((m, e) -> {
if (debug) TBMCCoreAPI.sendDebugMessage("2. m: " + m.getDisplayName() + " e: " + e);
}).flatMap(m -> m.hasHigherRoles(Collections.singleton(r)))) //Below one of our roles
.doAfterSuccessOrError((b, e) -> {
if (debug) TBMCCoreAPI.sendDebugMessage("3. b: " + b + " - e: " + e);
}).defaultIfEmpty(false);
} }
} }

View file

@ -72,7 +72,7 @@ public class RoleCommand extends ICommand2DC {
sb.append("\n"); sb.append("\n");
b = !b; b = !b;
} }
if (sb.charAt(sb.length() - 1) != '\n') if (sb.length() > 0 && sb.charAt(sb.length() - 1) != '\n')
sb.append('\n'); sb.append('\n');
sender.sendMessage("list of roles:\n```\n" + sb + "```"); sender.sendMessage("list of roles:\n```\n" + sb + "```");
} }