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(); val mentions = message.getRoleMentions();
final String mentionNick = member.getNicknameMention(); return self.getRoles().filterWhen(r -> mentions.any(rr -> rr.getName().equals(r.getName())))
System.out.println("6"); .map(Role::getMention);
boolean gotmention = checkanddeletemention(cmdwithargs, mention, message); }).map(mentionRole -> {
gotmention = checkanddeletemention(cmdwithargs, mentionNick, message) || gotmention; gotmention.set(checkanddeletemention(cmdwithargs, mentionRole, message) || gotmention.get()); // Delete all mentions
System.out.println("7"); return !mentionedonly || gotmention.get(); //Stops here if false
val mentions = message.getRoleMentions(); })).filter(b -> b).last(false).flatMap(b -> channel.type()).flatMap(v -> {
for (String mentionRole : member.getRoles().filter(r -> mentions.any(rr -> rr.getName().equals(r.getName())).blockOptional().orElse(false)).map(Role::getMention).toIterable()) String cmdwithargsString = cmdwithargs.toString();
gotmention = checkanddeletemention(cmdwithargs, mentionRole, message) || gotmention; // Delete all mentions try {
if (mentionedonly && !gotmention) if (!DiscordPlugin.plugin.getManager().handleCommand(new Command2DCSender(message), cmdwithargsString))
return false; return DPUtils.reply(message, channel, "Unknown command. Do " + DiscordPlugin.getPrefix() + "help for help.\n" + cmdwithargsString);
System.out.println("8"); } catch (Exception e) {
channel.type().subscribe(); TBMCCoreAPI.SendException("Failed to process Discord command: " + cmdwithargsString, e);
String cmdwithargsString = cmdwithargs.toString(); }
System.out.println("9"); return Mono.empty();
try { }).map(m -> false).defaultIfEmpty(true);
if (!DiscordPlugin.plugin.getManager().handleCommand(new Command2DCSender(message), cmdwithargsString))
DPUtils.reply(message, channel, "Unknown command. Do " + DiscordPlugin.getPrefix() + "help for help.\n" + cmdwithargsString).subscribe();
} catch (Exception e) {
TBMCCoreAPI.SendException("Failed to process Discord command: " + cmdwithargsString, e);
}
System.out.println("10");
return 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"); val commandChannel = DiscordPlugin.plugin.CommandChannel().get();
try { val commandCh = DPUtils.getMessageChannel(DiscordPlugin.plugin.CommandChannel());
boolean handled = false; return commandCh.filter(ch -> (commandChannel != null && event.getMessage().getChannelId().asLong() == commandChannel.asLong()) //If mentioned, that's higher than chat
val commandChannel = DiscordPlugin.plugin.CommandChannel().get(); || event.getMessage().getContent().orElse("").contains("channelcon")) //Only 'channelcon' is allowed in other channels
System.out.println("C"); .filterWhen(ch -> { //Only continue if this doesn't handle the event
if ((commandChannel != null && event.getMessage().getChannelId().asLong() == commandChannel.asLong()) //If mentioned, that's higher than chat return CommandListener.runCommand(event.getMessage(), ch, true); //#bot is handled here
|| event.getMessage().getContent().orElse("").contains("channelcon")) //Only 'channelcon' is allowed in other channels }).filterWhen(ch -> {
handled = CommandListener.runCommand(event.getMessage(), true); //#bot is handled here val mcchat = Component.getComponents().get(MinecraftChatModule.class);
System.out.println("D"); if (mcchat != null && mcchat.isEnabled()) //ComponentManager.isEnabled() searches the component again
if (handled) return; return ((MinecraftChatModule) mcchat).getListener().handleDiscord(event); //Also runs Discord commands in chat channels
//System.out.println("Message handling"); return Mono.empty(); //Wasn't handled, continue
val mcchat = Component.getComponents().get(MinecraftChatModule.class); }).filterWhen(ch -> CommandListener.runCommand(event.getMessage(), ch, false));
if (mcchat != null && mcchat.isEnabled()) //ComponentManager.isEnabled() searches the component again }).onErrorContinue((err, obj) -> TBMCCoreAPI.SendException("An error occured while handling a message!", err))
handled = ((MinecraftChatModule) mcchat).getListener().handleDiscord(event); //Also runs Discord commands in chat channels .subscribe();
System.out.println("E");
if (!handled)
handled = CommandListener.runCommand(event.getMessage(), false);
System.out.println("F");
} 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,44 +218,42 @@ 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"); && !(channel instanceof PrivateChannel
if (ev.getMessage().getChannelId().asLong() != module.chatChannel().get().asLong() && author.map(u -> MCChatPrivate.isMinecraftChatEnabled(u.getId().asString())).orElse(false)
&& !(channel instanceof PrivateChannel && !hasCustomChat)); //Chat isn't enabled on this channel
&& author.map(u -> MCChatPrivate.isMinecraftChatEnabled(u.getId().asString())).orElse(false) }).filter(channel -> {
&& !hasCustomChat)) return !(channel instanceof PrivateChannel //Only in private chat
return false; //Chat isn't enabled on this channel && ev.getMessage().getContent().isPresent()
System.out.println("C3"); && ev.getMessage().getContent().get().length() < "/mcchat<>".length()
if (channel instanceof PrivateChannel //Only in private chat && ev.getMessage().getContent().get().replace("/", "")
&& ev.getMessage().getContent().isPresent() .equalsIgnoreCase("mcchat")); //Either mcchat or /mcchat
&& ev.getMessage().getContent().get().length() < "/mcchat<>".length() //Allow disabling the chat if needed
&& ev.getMessage().getContent().get().replace("/", "") }).filterWhen(channel -> CommandListener.runCommand(ev.getMessage(), channel, true))
.equalsIgnoreCase("mcchat")) //Either mcchat or /mcchat //Allow running commands in chat channels
return false; //Allow disabling the chat if needed .filter(channel -> {
System.out.println("C4"); MCChatUtils.resetLastMessage(channel);
if (CommandListener.runCommand(ev.getMessage(), true)) recevents.add(ev);
return true; //Allow running commands in chat channels System.out.println("Message event added");
System.out.println("C5"); if (rectask != null)
MCChatUtils.resetLastMessage(channel); return true;
//System.out.println("Message: "+ev.getMessage().getAuthor().toString()); recrun = () -> { //Don't return in a while loop next time
recevents.add(ev); recthread = Thread.currentThread();
if (rectask != null) processDiscordToMC();
return true; if (DiscordPlugin.plugin.isEnabled()) //Don't run again if shutting down
recrun = () -> { //Don't return in a while loop next time rectask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, recrun); //Continue message processing
recthread = Thread.currentThread(); };
processDiscordToMC(); rectask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, recrun); //Start message processing
if (DiscordPlugin.plugin.isEnabled()) //Don't run again if shutting down return true;
rectask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, recrun); //Continue message processing }).map(b -> false).defaultIfEmpty(true);
};
rectask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, recrun); //Start message processing
return true;
} }
private void processDiscordToMC() { private void processDiscordToMC() {