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>
</excludes> <!-- http://stackoverflow.com/questions/28458058/maven-shade-plugin-exclude-a-dependency-and-all-its-transitive-dependencies -->
</artifactSet>
<minimizeJar>true</minimizeJar>
</configuration>
</execution>
</executions>

View file

@ -9,6 +9,9 @@ import discord4j.core.object.entity.MessageChannel;
import discord4j.core.object.entity.PrivateChannel;
import discord4j.core.object.entity.Role;
import lombok.val;
import reactor.core.publisher.Mono;
import java.util.concurrent.atomic.AtomicBoolean;
public class CommandListener {
/**
@ -16,54 +19,44 @@ public class CommandListener {
*
* @param message The Discord 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())
return false; //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");
return ret; //Pin messages and such, let the mcchat listener deal with it
val content = message.getContent().get();
if (channel == null) return false;
Mono<?> tmp = ret;
if (!mentionedonly) { //mentionedonly conditions are in CommonListeners
if (!(channel instanceof PrivateChannel)
&& !(content.charAt(0) == DiscordPlugin.getPrefix()
&& channel.getId().asString().equals(DiscordPlugin.plugin.CommandChannel().get().asString()))) //
return false;
channel.type().subscribe(); // Fun
return ret;
tmp = ret.then(channel.type()); // Fun
}
System.out.println("3");
final StringBuilder cmdwithargs = new StringBuilder(content);
val self = DiscordPlugin.dc.getSelf().block();
System.out.println("4");
if (self == null) return false;
val member = self.asMember(DiscordPlugin.mainServer.getId()).block();
System.out.println("5");
if (member == null) return false;
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();
for (String mentionRole : member.getRoles().filter(r -> mentions.any(rr -> rr.getName().equals(r.getName())).blockOptional().orElse(false)).map(Role::getMention).toIterable())
gotmention = checkanddeletemention(cmdwithargs, mentionRole, message) || gotmention; // Delete all mentions
if (mentionedonly && !gotmention)
return false;
System.out.println("8");
channel.type().subscribe();
String cmdwithargsString = cmdwithargs.toString();
System.out.println("9");
try {
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;
val gotmention = new AtomicBoolean();
return tmp.flatMapMany(x ->
DiscordPlugin.dc.getSelf().flatMap(self -> self.asMember(DiscordPlugin.mainServer.getId()))
.flatMapMany(self -> {
gotmention.set(checkanddeletemention(cmdwithargs, self.getMention(), message));
gotmention.set(checkanddeletemention(cmdwithargs, self.getNicknameMention(), message) || gotmention.get());
val mentions = message.getRoleMentions();
return self.getRoles().filterWhen(r -> mentions.any(rr -> rr.getName().equals(r.getName())))
.map(Role::getMention);
}).map(mentionRole -> {
gotmention.set(checkanddeletemention(cmdwithargs, mentionRole, message) || gotmention.get()); // Delete all mentions
return !mentionedonly || gotmention.get(); //Stops here if false
})).filter(b -> b).last(false).flatMap(b -> channel.type()).flatMap(v -> {
String cmdwithargsString = cmdwithargs.toString();
try {
if (!DiscordPlugin.plugin.getManager().handleCommand(new Command2DCSender(message), cmdwithargsString))
return DPUtils.reply(message, channel, "Unknown command. Do " + DiscordPlugin.getPrefix() + "help for help.\n" + cmdwithargsString);
} catch (Exception e) {
TBMCCoreAPI.SendException("Failed to process Discord command: " + cmdwithargsString, e);
}
return Mono.empty();
}).map(m -> false).defaultIfEmpty(true);
}
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.RoleUpdateEvent;
import lombok.val;
import reactor.core.publisher.Mono;
public class CommonListeners {
@ -27,39 +28,31 @@ public class CommonListeners {
- CommandListener (with the correct prefix in #bot, or in private)
*/
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)
return;
return def;
val author = event.getMessage().getAuthor();
if (!author.isPresent() || author.get().isBot())
return;
return def;
//System.out.println("Author: "+author.get());
//System.out.println("Bot: "+author.get().isBot());
System.out.println("A");
if (FunModule.executeMemes(event.getMessage()))
return;
System.out.println("B");
try {
boolean handled = false;
val commandChannel = DiscordPlugin.plugin.CommandChannel().get();
System.out.println("C");
if ((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
handled = CommandListener.runCommand(event.getMessage(), true); //#bot is handled here
System.out.println("D");
if (handled) return;
//System.out.println("Message handling");
val mcchat = Component.getComponents().get(MinecraftChatModule.class);
if (mcchat != null && mcchat.isEnabled()) //ComponentManager.isEnabled() searches the component again
handled = ((MinecraftChatModule) mcchat).getListener().handleDiscord(event); //Also runs Discord commands in chat channels
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);
}
});
return def;
val commandChannel = DiscordPlugin.plugin.CommandChannel().get();
val commandCh = DPUtils.getMessageChannel(DiscordPlugin.plugin.CommandChannel());
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
.filterWhen(ch -> { //Only continue if this doesn't handle the event
return CommandListener.runCommand(event.getMessage(), ch, true); //#bot is handled here
}).filterWhen(ch -> {
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 -> CommandListener.runCommand(event.getMessage(), ch, false));
}).onErrorContinue((err, obj) -> TBMCCoreAPI.SendException("An error occured while handling a message!", err))
.subscribe();
/*dispatcher.on(MessageCreateEvent.class).doOnNext(x -> System.out.println("Got message"))
.flatMap(MessageCreateEvent::getGuild)
.flatMap(guild -> DiscordPlugin.dc.getSelf())
@ -73,6 +66,7 @@ public class CommonListeners {
dispatcher.on(RoleCreateEvent.class).subscribe(GameRoleModule::handleRoleEvent);
dispatcher.on(RoleDeleteEvent.class).subscribe(GameRoleModule::handleRoleEvent);
dispatcher.on(RoleUpdateEvent.class).subscribe(GameRoleModule::handleRoleEvent);
}
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.Listener;
import org.bukkit.scheduler.BukkitTask;
import reactor.core.publisher.Mono;
import java.awt.*;
import java.time.Instant;
@ -217,44 +218,42 @@ public class MCChatListener implements Listener {
private static Thread recthread;
// Discord
public boolean handleDiscord(MessageCreateEvent ev) {
public Mono<Boolean> handleDiscord(MessageCreateEvent ev) {
val ret = Mono.just(true);
if (!ComponentManager.isEnabled(MinecraftChatModule.class))
return false;
return ret;
System.out.println("Chat event");
val author = ev.getMessage().getAuthor();
final boolean hasCustomChat = MCChatCustom.hasCustomChat(ev.getMessage().getChannelId());
System.out.println("C1");
val channel = ev.getMessage().getChannel().block();
System.out.println("C2");
if (ev.getMessage().getChannelId().asLong() != module.chatChannel().get().asLong()
&& !(channel instanceof PrivateChannel
&& author.map(u -> MCChatPrivate.isMinecraftChatEnabled(u.getId().asString())).orElse(false)
&& !hasCustomChat))
return false; //Chat isn't enabled on this channel
System.out.println("C3");
if (channel instanceof PrivateChannel //Only in private chat
&& ev.getMessage().getContent().isPresent()
&& ev.getMessage().getContent().get().length() < "/mcchat<>".length()
&& ev.getMessage().getContent().get().replace("/", "")
.equalsIgnoreCase("mcchat")) //Either mcchat or /mcchat
return false; //Allow disabling the chat if needed
System.out.println("C4");
if (CommandListener.runCommand(ev.getMessage(), true))
return true; //Allow running commands in chat channels
System.out.println("C5");
MCChatUtils.resetLastMessage(channel);
//System.out.println("Message: "+ev.getMessage().getAuthor().toString());
recevents.add(ev);
if (rectask != null)
return true;
recrun = () -> { //Don't return in a while loop next time
recthread = Thread.currentThread();
processDiscordToMC();
if (DiscordPlugin.plugin.isEnabled()) //Don't run again if shutting down
rectask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, recrun); //Continue message processing
};
rectask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, recrun); //Start message processing
return true;
return ev.getMessage().getChannel().filter(channel -> {
return !(ev.getMessage().getChannelId().asLong() != module.chatChannel().get().asLong()
&& !(channel instanceof PrivateChannel
&& author.map(u -> MCChatPrivate.isMinecraftChatEnabled(u.getId().asString())).orElse(false)
&& !hasCustomChat)); //Chat isn't enabled on this channel
}).filter(channel -> {
return !(channel instanceof PrivateChannel //Only in private chat
&& ev.getMessage().getContent().isPresent()
&& ev.getMessage().getContent().get().length() < "/mcchat<>".length()
&& ev.getMessage().getContent().get().replace("/", "")
.equalsIgnoreCase("mcchat")); //Either mcchat or /mcchat
//Allow disabling the chat if needed
}).filterWhen(channel -> CommandListener.runCommand(ev.getMessage(), channel, true))
//Allow running commands in chat channels
.filter(channel -> {
MCChatUtils.resetLastMessage(channel);
recevents.add(ev);
System.out.println("Message event added");
if (rectask != null)
return true;
recrun = () -> { //Don't return in a while loop next time
recthread = Thread.currentThread();
processDiscordToMC();
if (DiscordPlugin.plugin.isEnabled()) //Don't run again if shutting down
rectask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, recrun); //Continue message processing
};
rectask = Bukkit.getScheduler().runTaskAsynchronously(DiscordPlugin.plugin, recrun); //Start message processing
return true;
}).map(b -> false).defaultIfEmpty(true);
}
private void processDiscordToMC() {