Some fixes, some debugging

#93
This commit is contained in:
Norbi Peti 2019-05-11 00:15:50 +02:00
parent beab400873
commit b396ec47b4
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
4 changed files with 87 additions and 100 deletions

View file

@ -68,6 +68,7 @@
<exclude>net.ess3:Essentials</exclude> <exclude>net.ess3:Essentials</exclude>
</excludes> <!-- http://stackoverflow.com/questions/28458058/maven-shade-plugin-exclude-a-dependency-and-all-its-transitive-dependencies --> </excludes> <!-- http://stackoverflow.com/questions/28458058/maven-shade-plugin-exclude-a-dependency-and-all-its-transitive-dependencies -->
</artifactSet> </artifactSet>
<minimizeJar>true</minimizeJar>
</configuration> </configuration>
</execution> </execution>
</executions> </executions>

View file

@ -9,6 +9,9 @@ import discord4j.core.object.entity.MessageChannel;
import discord4j.core.object.entity.PrivateChannel; import discord4j.core.object.entity.PrivateChannel;
import discord4j.core.object.entity.Role; import discord4j.core.object.entity.Role;
import lombok.val; import lombok.val;
import reactor.core.publisher.Mono;
import java.util.concurrent.atomic.AtomicBoolean;
public class CommandListener { public class CommandListener {
/** /**
@ -16,54 +19,44 @@ public class CommandListener {
* *
* @param message The Discord message * @param message The Discord message
* @param mentionedonly Only run the command if ChromaBot is mentioned at the start of the message * @param mentionedonly Only run the command if ChromaBot is mentioned at the start of the message
* @return Whether it ran the command * @return Whether it <b>did not run</b> the command
*/ */
public static boolean runCommand(Message message, boolean mentionedonly) { public static Mono<Boolean> runCommand(Message message, MessageChannel channel, boolean mentionedonly) {
Mono<Boolean> ret = Mono.just(true);
if (!message.getContent().isPresent()) if (!message.getContent().isPresent())
return false; //Pin messages and such, let the mcchat listener deal with it return ret; //Pin messages and such, let the mcchat listener deal with it
System.out.println("1");
final MessageChannel channel = message.getChannel().block();
System.out.println("2");
val content = message.getContent().get(); val content = message.getContent().get();
if (channel == null) return false; Mono<?> tmp = ret;
if (!mentionedonly) { //mentionedonly conditions are in CommonListeners if (!mentionedonly) { //mentionedonly conditions are in CommonListeners
if (!(channel instanceof PrivateChannel) if (!(channel instanceof PrivateChannel)
&& !(content.charAt(0) == DiscordPlugin.getPrefix() && !(content.charAt(0) == DiscordPlugin.getPrefix()
&& channel.getId().asString().equals(DiscordPlugin.plugin.CommandChannel().get().asString()))) // && channel.getId().asString().equals(DiscordPlugin.plugin.CommandChannel().get().asString()))) //
return false; return ret;
channel.type().subscribe(); // Fun tmp = ret.then(channel.type()); // Fun
} }
System.out.println("3");
final StringBuilder cmdwithargs = new StringBuilder(content); final StringBuilder cmdwithargs = new StringBuilder(content);
val self = DiscordPlugin.dc.getSelf().block(); val gotmention = new AtomicBoolean();
System.out.println("4"); return tmp.flatMapMany(x ->
if (self == null) return false; DiscordPlugin.dc.getSelf().flatMap(self -> self.asMember(DiscordPlugin.mainServer.getId()))
val member = self.asMember(DiscordPlugin.mainServer.getId()).block(); .flatMapMany(self -> {
System.out.println("5"); gotmention.set(checkanddeletemention(cmdwithargs, self.getMention(), message));
if (member == null) return false; gotmention.set(checkanddeletemention(cmdwithargs, self.getNicknameMention(), message) || gotmention.get());
final String mention = self.getMention();
final String mentionNick = member.getNicknameMention();
System.out.println("6");
boolean gotmention = checkanddeletemention(cmdwithargs, mention, message);
gotmention = checkanddeletemention(cmdwithargs, mentionNick, message) || gotmention;
System.out.println("7");
val mentions = message.getRoleMentions(); val mentions = message.getRoleMentions();
for (String mentionRole : member.getRoles().filter(r -> mentions.any(rr -> rr.getName().equals(r.getName())).blockOptional().orElse(false)).map(Role::getMention).toIterable()) return self.getRoles().filterWhen(r -> mentions.any(rr -> rr.getName().equals(r.getName())))
gotmention = checkanddeletemention(cmdwithargs, mentionRole, message) || gotmention; // Delete all mentions .map(Role::getMention);
if (mentionedonly && !gotmention) }).map(mentionRole -> {
return false; gotmention.set(checkanddeletemention(cmdwithargs, mentionRole, message) || gotmention.get()); // Delete all mentions
System.out.println("8"); return !mentionedonly || gotmention.get(); //Stops here if false
channel.type().subscribe(); })).filter(b -> b).last(false).flatMap(b -> channel.type()).flatMap(v -> {
String cmdwithargsString = cmdwithargs.toString(); String cmdwithargsString = cmdwithargs.toString();
System.out.println("9");
try { try {
if (!DiscordPlugin.plugin.getManager().handleCommand(new Command2DCSender(message), cmdwithargsString)) if (!DiscordPlugin.plugin.getManager().handleCommand(new Command2DCSender(message), cmdwithargsString))
DPUtils.reply(message, channel, "Unknown command. Do " + DiscordPlugin.getPrefix() + "help for help.\n" + cmdwithargsString).subscribe(); return DPUtils.reply(message, channel, "Unknown command. Do " + DiscordPlugin.getPrefix() + "help for help.\n" + cmdwithargsString);
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("Failed to process Discord command: " + cmdwithargsString, e); TBMCCoreAPI.SendException("Failed to process Discord command: " + cmdwithargsString, e);
} }
System.out.println("10"); return Mono.empty();
return true; }).map(m -> false).defaultIfEmpty(true);
} }
private static boolean checkanddeletemention(StringBuilder cmdwithargs, String mention, Message message) { private static boolean checkanddeletemention(StringBuilder cmdwithargs, String mention, Message message) {

View file

@ -14,6 +14,7 @@ import discord4j.core.event.domain.role.RoleCreateEvent;
import discord4j.core.event.domain.role.RoleDeleteEvent; import discord4j.core.event.domain.role.RoleDeleteEvent;
import discord4j.core.event.domain.role.RoleUpdateEvent; import discord4j.core.event.domain.role.RoleUpdateEvent;
import lombok.val; import lombok.val;
import reactor.core.publisher.Mono;
public class CommonListeners { public class CommonListeners {
@ -27,39 +28,31 @@ public class CommonListeners {
- CommandListener (with the correct prefix in #bot, or in private) - CommandListener (with the correct prefix in #bot, or in private)
*/ */
public static void register(EventDispatcher dispatcher) { public static void register(EventDispatcher dispatcher) {
dispatcher.on(MessageCreateEvent.class).subscribe(event -> { dispatcher.on(MessageCreateEvent.class).flatMap(event -> {
val def = Mono.empty();
if (DiscordPlugin.SafeMode) if (DiscordPlugin.SafeMode)
return; return def;
val author = event.getMessage().getAuthor(); val author = event.getMessage().getAuthor();
if (!author.isPresent() || author.get().isBot()) if (!author.isPresent() || author.get().isBot())
return; return def;
//System.out.println("Author: "+author.get()); //System.out.println("Author: "+author.get());
//System.out.println("Bot: "+author.get().isBot()); //System.out.println("Bot: "+author.get().isBot());
System.out.println("A");
if (FunModule.executeMemes(event.getMessage())) if (FunModule.executeMemes(event.getMessage()))
return; return def;
System.out.println("B");
try {
boolean handled = false;
val commandChannel = DiscordPlugin.plugin.CommandChannel().get(); val commandChannel = DiscordPlugin.plugin.CommandChannel().get();
System.out.println("C"); val commandCh = DPUtils.getMessageChannel(DiscordPlugin.plugin.CommandChannel());
if ((commandChannel != null && event.getMessage().getChannelId().asLong() == commandChannel.asLong()) //If mentioned, that's higher than chat return commandCh.filter(ch -> (commandChannel != null && event.getMessage().getChannelId().asLong() == commandChannel.asLong()) //If mentioned, that's higher than chat
|| event.getMessage().getContent().orElse("").contains("channelcon")) //Only 'channelcon' is allowed in other channels || event.getMessage().getContent().orElse("").contains("channelcon")) //Only 'channelcon' is allowed in other channels
handled = CommandListener.runCommand(event.getMessage(), true); //#bot is handled here .filterWhen(ch -> { //Only continue if this doesn't handle the event
System.out.println("D"); return CommandListener.runCommand(event.getMessage(), ch, true); //#bot is handled here
if (handled) return; }).filterWhen(ch -> {
//System.out.println("Message handling");
val mcchat = Component.getComponents().get(MinecraftChatModule.class); val mcchat = Component.getComponents().get(MinecraftChatModule.class);
if (mcchat != null && mcchat.isEnabled()) //ComponentManager.isEnabled() searches the component again if (mcchat != null && mcchat.isEnabled()) //ComponentManager.isEnabled() searches the component again
handled = ((MinecraftChatModule) mcchat).getListener().handleDiscord(event); //Also runs Discord commands in chat channels return ((MinecraftChatModule) mcchat).getListener().handleDiscord(event); //Also runs Discord commands in chat channels
System.out.println("E"); return Mono.empty(); //Wasn't handled, continue
if (!handled) }).filterWhen(ch -> CommandListener.runCommand(event.getMessage(), ch, false));
handled = CommandListener.runCommand(event.getMessage(), false); }).onErrorContinue((err, obj) -> TBMCCoreAPI.SendException("An error occured while handling a message!", err))
System.out.println("F"); .subscribe();
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while handling a message!", e);
}
});
/*dispatcher.on(MessageCreateEvent.class).doOnNext(x -> System.out.println("Got message")) /*dispatcher.on(MessageCreateEvent.class).doOnNext(x -> System.out.println("Got message"))
.flatMap(MessageCreateEvent::getGuild) .flatMap(MessageCreateEvent::getGuild)
.flatMap(guild -> DiscordPlugin.dc.getSelf()) .flatMap(guild -> DiscordPlugin.dc.getSelf())
@ -73,6 +66,7 @@ public class CommonListeners {
dispatcher.on(RoleCreateEvent.class).subscribe(GameRoleModule::handleRoleEvent); dispatcher.on(RoleCreateEvent.class).subscribe(GameRoleModule::handleRoleEvent);
dispatcher.on(RoleDeleteEvent.class).subscribe(GameRoleModule::handleRoleEvent); dispatcher.on(RoleDeleteEvent.class).subscribe(GameRoleModule::handleRoleEvent);
dispatcher.on(RoleUpdateEvent.class).subscribe(GameRoleModule::handleRoleEvent); dispatcher.on(RoleUpdateEvent.class).subscribe(GameRoleModule::handleRoleEvent);
} }
private static boolean debug = false; private static boolean debug = false;

View file

@ -25,6 +25,7 @@ import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import reactor.core.publisher.Mono;
import java.awt.*; import java.awt.*;
import java.time.Instant; import java.time.Instant;
@ -217,34 +218,31 @@ public class MCChatListener implements Listener {
private static Thread recthread; private static Thread recthread;
// Discord // Discord
public boolean handleDiscord(MessageCreateEvent ev) { public Mono<Boolean> handleDiscord(MessageCreateEvent ev) {
val ret = Mono.just(true);
if (!ComponentManager.isEnabled(MinecraftChatModule.class)) if (!ComponentManager.isEnabled(MinecraftChatModule.class))
return false; return ret;
System.out.println("Chat event"); System.out.println("Chat event");
val author = ev.getMessage().getAuthor(); val author = ev.getMessage().getAuthor();
final boolean hasCustomChat = MCChatCustom.hasCustomChat(ev.getMessage().getChannelId()); final boolean hasCustomChat = MCChatCustom.hasCustomChat(ev.getMessage().getChannelId());
System.out.println("C1"); return ev.getMessage().getChannel().filter(channel -> {
val channel = ev.getMessage().getChannel().block(); return !(ev.getMessage().getChannelId().asLong() != module.chatChannel().get().asLong()
System.out.println("C2");
if (ev.getMessage().getChannelId().asLong() != module.chatChannel().get().asLong()
&& !(channel instanceof PrivateChannel && !(channel instanceof PrivateChannel
&& author.map(u -> MCChatPrivate.isMinecraftChatEnabled(u.getId().asString())).orElse(false) && author.map(u -> MCChatPrivate.isMinecraftChatEnabled(u.getId().asString())).orElse(false)
&& !hasCustomChat)) && !hasCustomChat)); //Chat isn't enabled on this channel
return false; //Chat isn't enabled on this channel }).filter(channel -> {
System.out.println("C3"); return !(channel instanceof PrivateChannel //Only in private chat
if (channel instanceof PrivateChannel //Only in private chat
&& ev.getMessage().getContent().isPresent() && ev.getMessage().getContent().isPresent()
&& ev.getMessage().getContent().get().length() < "/mcchat<>".length() && ev.getMessage().getContent().get().length() < "/mcchat<>".length()
&& ev.getMessage().getContent().get().replace("/", "") && ev.getMessage().getContent().get().replace("/", "")
.equalsIgnoreCase("mcchat")) //Either mcchat or /mcchat .equalsIgnoreCase("mcchat")); //Either mcchat or /mcchat
return false; //Allow disabling the chat if needed //Allow disabling the chat if needed
System.out.println("C4"); }).filterWhen(channel -> CommandListener.runCommand(ev.getMessage(), channel, true))
if (CommandListener.runCommand(ev.getMessage(), true)) //Allow running commands in chat channels
return true; //Allow running commands in chat channels .filter(channel -> {
System.out.println("C5");
MCChatUtils.resetLastMessage(channel); MCChatUtils.resetLastMessage(channel);
//System.out.println("Message: "+ev.getMessage().getAuthor().toString());
recevents.add(ev); recevents.add(ev);
System.out.println("Message event added");
if (rectask != null) if (rectask != null)
return true; return true;
recrun = () -> { //Don't return in a while loop next time recrun = () -> { //Don't return in a while loop next time
@ -255,6 +253,7 @@ public class MCChatListener implements Listener {
}; };
rectask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, recrun); //Start message processing rectask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, recrun); //Start message processing
return true; return true;
}).map(b -> false).defaultIfEmpty(true);
} }
private void processDiscordToMC() { private void processDiscordToMC() {