Updated to Discord4J v3, permission injection, improvements #99

Merged
NorbiPeti merged 25 commits from dev into master 2019-06-06 20:45:22 +00:00
3 changed files with 22 additions and 18 deletions
Showing only changes of commit 34e3bb0ead - Show all commits

View file

@ -68,7 +68,7 @@ public class DiscordPlugin extends ButtonPlugin {
private ConfigData<Optional<Guild>> mainServer() {
return getIConfig().getDataPrimDef("mainServer", 0L,
id -> {
System.out.println("WTF ID: " + id); //TODO: It attempts to get the default as well
//It attempts to get the default as well
if ((long) id == 0L)
return Optional.empty(); //Hack?
return dc.getGuildById(Snowflake.of((long) id))
@ -81,6 +81,9 @@ public class DiscordPlugin extends ButtonPlugin {
return DPUtils.snowflakeData(getIConfig(), "commandChannel", 239519012529111040L);
}
/**
* If the role doesn't exist, then it will only allow for the owner.
*/
public ConfigData<Mono<Role>> modRole() {
return DPUtils.roleData(getIConfig(), "modRole", "Moderator");
}
@ -138,10 +141,7 @@ public class DiscordPlugin extends ButtonPlugin {
private void handleReady(List<GuildCreateEvent> event) {
try {
System.out.println("w t f: " + mainServer);
mainServer = mainServer().get().orElse(null); //Shouldn't change afterwards
System.out.println("Main server: " + mainServer);
System.out.println("wtf: " + mainServer().get());
if (mainServer == null) {
if (event.size() == 0) {
getLogger().severe("Main server not found! Invite the bot and do /discord reset");
@ -153,7 +153,8 @@ public class DiscordPlugin extends ButtonPlugin {
mainServer().set(Optional.of(mainServer)); //Save in config
}
SafeMode = false;
DPUtils.disableIfConfigError(null, commandChannel(), modRole()); //Won't disable, just prints the warning here
DPUtils.disableIfConfigErrorRes(null, commandChannel(), DPUtils.getMessageChannel(commandChannel()));
DPUtils.disableIfConfigError(null, modRole()); //Won't disable, just prints the warning here
Component.registerComponent(this, new GeneralEventBroadcasterModule());
Component.registerComponent(this, new MinecraftChatModule());

View file

@ -17,7 +17,8 @@ public class DebugCommand extends ICommand2DC {
.map(u -> u.asMember(DiscordPlugin.mainServer.getId()))
.orElse(Mono.empty()))
.flatMap(m -> DiscordPlugin.plugin.modRole().get()
.map(mr -> m.getRoleIds().stream().anyMatch(r -> r.equals(mr.getId()))))
.map(mr -> m.getRoleIds().stream().anyMatch(r -> r.equals(mr.getId())))
.switchIfEmpty(Mono.fromSupplier(() -> DiscordPlugin.mainServer.getOwnerId().asLong() == m.getId().asLong()))) //Role not found
.subscribe(success -> {
if (success)
sender.sendMessage("debug " + (CommonListeners.debug() ? "enabled" : "disabled"));

View file

@ -47,20 +47,22 @@ public class CommonListeners {
return commandCh.filterWhen(ch -> event.getMessage().getChannel().map(mch ->
(commandChannel != null && mch.getId().asLong() == commandChannel.asLong()) //If mentioned, that's higher than chat
|| mch instanceof PrivateChannel
|| event.getMessage().getContent().orElse("").contains("channelcon"))) //Only 'channelcon' is allowed in other channels
.filterWhen(ch -> { //Only continue if this doesn't handle the event
|| event.getMessage().getContent().orElse("").contains("channelcon")) //Only 'channelcon' is allowed in other channels
.flatMap(shouldRun -> { //Only continue if this doesn't handle the event
if (!shouldRun)
return Mono.just(true); //The condition is only for the first command execution, not mcchat
timings.printElapsed("Run command 1");
return CommandListener.runCommand(event.getMessage(), ch, true); //#bot is handled here
}).filterWhen(ch -> {
timings.printElapsed("mcchat");
val mcchat = Component.getComponents().get(MinecraftChatModule.class);
if (mcchat != null && mcchat.isEnabled()) //ComponentManager.isEnabled() searches the component again
return ((MinecraftChatModule) mcchat).getListener().handleDiscord(event); //Also runs Discord commands in chat channels
return Mono.empty(); //Wasn't handled, continue
}).filterWhen(ch -> {
timings.printElapsed("Run command 2");
return CommandListener.runCommand(event.getMessage(), ch, false);
});
})).filterWhen(ch -> {
timings.printElapsed("mcchat");
val mcchat = Component.getComponents().get(MinecraftChatModule.class);
if (mcchat != null && mcchat.isEnabled()) //ComponentManager.isEnabled() searches the component again
return ((MinecraftChatModule) mcchat).getListener().handleDiscord(event); //Also runs Discord commands in chat channels
return Mono.empty(); //Wasn't handled, continue
}).filterWhen(ch -> {
timings.printElapsed("Run command 2");
return CommandListener.runCommand(event.getMessage(), ch, false);
});
}).onErrorContinue((err, obj) -> TBMCCoreAPI.SendException("An error occured while handling a message!", err))
.subscribe();
dispatcher.on(PresenceUpdateEvent.class).subscribe(event -> {