Update, bot cmd fix, debugging

This commit is contained in:
Norbi Peti 2019-05-20 21:51:28 +02:00
parent 9e8f988ea6
commit ac61225730
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
6 changed files with 90 additions and 60 deletions

View file

@ -179,7 +179,7 @@
<dependency> <dependency>
<groupId>com.discord4j</groupId> <groupId>com.discord4j</groupId>
<artifactId>discord4j-core</artifactId> <artifactId>discord4j-core</artifactId>
<version>3.0.3</version> <version>3.0.5</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-jdk14 --> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-jdk14 -->
<dependency> <dependency>

View file

@ -21,42 +21,53 @@ public class CommandListener {
* @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 <b>did not run</b> the command * @return Whether it <b>did not run</b> the command
*/ */
public static Mono<Boolean> runCommand(Message message, MessageChannel channel, boolean mentionedonly) { public static Mono<Boolean> runCommand(Message message, MessageChannel commandChannel, boolean mentionedonly) {
Mono<Boolean> ret = Mono.just(true); Mono<Boolean> ret = Mono.just(true);
if (!message.getContent().isPresent()) if (!message.getContent().isPresent())
return ret; //Pin messages and such, let the mcchat listener deal with it return ret; //Pin messages and such, let the mcchat listener deal with it
val content = message.getContent().get(); val content = message.getContent().get();
Mono<?> tmp = ret; System.out.println("A");
if (!mentionedonly) { //mentionedonly conditions are in CommonListeners return message.getChannel().flatMap(channel -> {
if (!(channel instanceof PrivateChannel) Mono<?> tmp = ret;
&& !(content.charAt(0) == DiscordPlugin.getPrefix() if (!mentionedonly) { //mentionedonly conditions are in CommonListeners
&& channel.getId().asString().equals(DiscordPlugin.plugin.commandChannel().get().asString()))) // System.out.println("B");
return ret; //System.out.println("Channel type: " + commandChannel.getClass().getSimpleName());
tmp = ret.then(channel.type()); // Fun //System.out.println("Guild: " + ((TextChannel) commandChannel).getGuildId());
} if (!(channel instanceof PrivateChannel)
final StringBuilder cmdwithargs = new StringBuilder(content); && !(content.charAt(0) == DiscordPlugin.getPrefix()
val gotmention = new AtomicBoolean(); && channel.getId().asLong() == commandChannel.getId().asLong())) //
return tmp.flatMapMany(x -> return ret;
DiscordPlugin.dc.getSelf().flatMap(self -> self.asMember(DiscordPlugin.mainServer.getId())) System.out.println("C");
.flatMapMany(self -> { tmp = ret.then(channel.type()).thenReturn(true); // Fun (this true is ignored - x)
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(); final StringBuilder cmdwithargs = new StringBuilder(content);
}).map(m -> false).defaultIfEmpty(true); val gotmention = new AtomicBoolean();
return tmp.flatMapMany(x ->
DiscordPlugin.dc.getSelf().flatMap(self -> self.asMember(DiscordPlugin.mainServer.getId()))
.flatMapMany(self -> {
System.out.println("D");
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 -> {
System.out.println("E");
gotmention.set(checkanddeletemention(cmdwithargs, mentionRole, message) || gotmention.get()); // Delete all mentions
return !mentionedonly || gotmention.get(); //Stops here if false
}).switchIfEmpty(Mono.fromSupplier(() -> !mentionedonly || gotmention.get())))
.filter(b -> b).last(false).filter(b -> b).doOnNext(b -> channel.type().subscribe()).flatMap(b -> {
String cmdwithargsString = cmdwithargs.toString();
try {
System.out.println("F");
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) { private static boolean checkanddeletemention(StringBuilder cmdwithargs, String mention, Message message) {

View file

@ -13,6 +13,7 @@ import discord4j.core.event.domain.message.MessageCreateEvent;
import discord4j.core.event.domain.role.RoleCreateEvent; 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 discord4j.core.object.entity.PrivateChannel;
import lombok.val; import lombok.val;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
@ -41,16 +42,23 @@ public class CommonListeners {
return def; return def;
val commandChannel = DiscordPlugin.plugin.commandChannel().get(); val commandChannel = DiscordPlugin.plugin.commandChannel().get();
val commandCh = DPUtils.getMessageChannel(DiscordPlugin.plugin.commandChannel()); 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 return commandCh.filterWhen(ch -> event.getMessage().getChannel().map(mch ->
|| event.getMessage().getContent().orElse("").contains("channelcon")) //Only 'channelcon' is allowed in other channels commandChannel != null && event.getMessage().getChannelId().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 .filterWhen(ch -> { //Only continue if this doesn't handle the event
System.out.println("Run command 1");
return CommandListener.runCommand(event.getMessage(), ch, true); //#bot is handled here return CommandListener.runCommand(event.getMessage(), ch, true); //#bot is handled here
}).filterWhen(ch -> { }).filterWhen(ch -> {
System.out.println("mcchat");
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
return ((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
return Mono.empty(); //Wasn't handled, continue return Mono.empty(); //Wasn't handled, continue
}).filterWhen(ch -> CommandListener.runCommand(event.getMessage(), ch, false)); }).filterWhen(ch -> {
System.out.println("Run command 2");
return CommandListener.runCommand(event.getMessage(), ch, false);
});
}).onErrorContinue((err, obj) -> TBMCCoreAPI.SendException("An error occured while handling a message!", err)) }).onErrorContinue((err, obj) -> TBMCCoreAPI.SendException("An error occured while handling a message!", err))
.subscribe(); .subscribe();
/*dispatcher.on(MessageCreateEvent.class).doOnNext(x -> System.out.println("Got message")) /*dispatcher.on(MessageCreateEvent.class).doOnNext(x -> System.out.println("Got message"))

View file

@ -226,11 +226,13 @@ public class MCChatListener implements Listener {
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());
return ev.getMessage().getChannel().filter(channel -> { return ev.getMessage().getChannel().filter(channel -> {
System.out.println("Filter 1");
return !(ev.getMessage().getChannelId().asLong() != module.chatChannel().get().asLong() return !(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)); //Chat isn't enabled on this channel && !hasCustomChat)); //Chat isn't enabled on this channel
}).filter(channel -> { }).filter(channel -> {
System.out.println("Filter 2");
return !(channel instanceof PrivateChannel //Only in private chat return !(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()

View file

@ -210,14 +210,17 @@ public class MCChatUtils {
*/ */
public static void resetLastMessage(Channel channel) { public static void resetLastMessage(Channel channel) {
if (notEnabled()) return; if (notEnabled()) return;
System.out.println("Reset last message");
if (channel.getId().asLong() == module.chatChannel().get().asLong()) { if (channel.getId().asLong() == module.chatChannel().get().asLong()) {
(lastmsgdata == null ? lastmsgdata = new LastMsgData(module.chatChannelMono().block(), null) (lastmsgdata == null ? lastmsgdata = new LastMsgData(module.chatChannelMono().block(), null)
: lastmsgdata).message = null; : lastmsgdata).message = null;
System.out.println("Reset done: public chat");
return; return;
} // Don't set the whole object to null, the player and channel information should be preserved } // Don't set the whole object to null, the player and channel information should be preserved
for (LastMsgData data : channel instanceof PrivateChannel ? MCChatPrivate.lastmsgPerUser : MCChatCustom.lastmsgCustom) { for (LastMsgData data : channel instanceof PrivateChannel ? MCChatPrivate.lastmsgPerUser : MCChatCustom.lastmsgCustom) {
if (data.channel.getId().asLong() == channel.getId().asLong()) { if (data.channel.getId().asLong() == channel.getId().asLong()) {
data.message = null; data.message = null;
System.out.println("Reset done: private/custom chat");
return; return;
} }
} }

View file

@ -26,7 +26,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().filter(this::isGameRole).map(Role::getName).collect(Collectors.toList()).block(); GameRoles = DiscordPlugin.mainServer.getRoles().filterWhen(this::isGameRole).map(Role::getName).collect(Collectors.toList()).block();
} }
@Override @Override
@ -46,11 +46,14 @@ public class GameRoleModule extends Component<DiscordPlugin> {
if (roleEvent instanceof RoleCreateEvent) { if (roleEvent instanceof RoleCreateEvent) {
Bukkit.getScheduler().runTaskLaterAsynchronously(DiscordPlugin.plugin, () -> { Bukkit.getScheduler().runTaskLaterAsynchronously(DiscordPlugin.plugin, () -> {
Role role=((RoleCreateEvent) roleEvent).getRole(); Role role=((RoleCreateEvent) roleEvent).getRole();
if (!grm.isGameRole(role)) grm.isGameRole(role).flatMap(b -> {
return; //Deleted or not a game role if (!b)
GameRoles.add(role.getName()); return Mono.empty(); //Deleted or not a game role
if (logChannel != null) GameRoles.add(role.getName());
logChannel.flatMap(ch -> ch.createMessage("Added " + role.getName() + " as game role. If you don't want this, change the role's color from the default.")).subscribe(); if (logChannel != null)
return logChannel.flatMap(ch -> ch.createMessage("Added " + role.getName() + " as game role. If you don't want this, change the role's color from the default."));
return Mono.empty();
}).subscribe();
}, 100); }, 100);
} else if (roleEvent instanceof RoleDeleteEvent) { } else if (roleEvent instanceof RoleDeleteEvent) {
Role role=((RoleDeleteEvent) roleEvent).getRole().orElse(null); Role role=((RoleDeleteEvent) roleEvent).getRole().orElse(null);
@ -64,30 +67,33 @@ public class GameRoleModule extends Component<DiscordPlugin> {
return; return;
} }
Role or=event.getOld().get(); Role or=event.getOld().get();
if (!grm.isGameRole(event.getCurrent())) { grm.isGameRole(event.getCurrent()).flatMap(b -> {
if (GameRoles.remove(or.getName()) && logChannel != null) if (!b) {
logChannel.flatMap(ch -> ch.createMessage("Removed " + or.getName() + " as a game role because it's color changed.")).subscribe(); if (GameRoles.remove(or.getName()) && logChannel != null)
} else { return logChannel.flatMap(ch -> ch.createMessage("Removed " + or.getName() + " as a game role because it's color changed."));
if (GameRoles.contains(or.getName()) && or.getName().equals(event.getCurrent().getName())) } else {
return; if (GameRoles.contains(or.getName()) && or.getName().equals(event.getCurrent().getName()))
boolean removed = GameRoles.remove(or.getName()); //Regardless of whether it was a game role return Mono.empty();
GameRoles.add(event.getCurrent().getName()); //Add it because it has no color boolean removed = GameRoles.remove(or.getName()); //Regardless of whether it was a game role
if (logChannel != null) { GameRoles.add(event.getCurrent().getName()); //Add it because it has no color
if (removed) if (logChannel != null) {
logChannel.flatMap(ch -> ch.createMessage("Changed game role from " + or.getName() + " to " + event.getCurrent().getName() + ".")).subscribe(); if (removed)
else return logChannel.flatMap(ch -> ch.createMessage("Changed game role from " + or.getName() + " to " + event.getCurrent().getName() + "."));
logChannel.flatMap(ch -> ch.createMessage("Added " + event.getCurrent().getName() + " as game role because it has the default color.")).subscribe(); else
return logChannel.flatMap(ch -> ch.createMessage("Added " + event.getCurrent().getName() + " as game role because it has the default color."));
}
} }
} return Mono.empty();
}).subscribe();
} }
} }
@SuppressWarnings("ConstantConditions") private Mono<Boolean> isGameRole(Role r) {
private boolean isGameRole(Role r) {
if (r.getGuildId().asLong() != DiscordPlugin.mainServer.getId().asLong()) if (r.getGuildId().asLong() != DiscordPlugin.mainServer.getId().asLong())
return false; //Only allow on the main server return Mono.just(false); //Only allow on the main server
val rc = new Color(149, 165, 166, 0); val rc = new Color(149, 165, 166, 0);
return r.getColor().equals(rc) return Mono.just(r.getColor().equals(rc)).filter(b -> b).flatMap(b ->
&& DiscordPlugin.dc.getSelf().block().asMember(DiscordPlugin.mainServer.getId()).block().hasHigherRoles(Collections.singleton(r)).block(); //Below one of our roles DiscordPlugin.dc.getSelf().flatMap(u -> u.asMember(DiscordPlugin.mainServer.getId())).flatMap(m -> m.hasHigherRoles(Collections.singleton(r)))) //Below one of our roles
.defaultIfEmpty(false);
} }
} }