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>
<groupId>com.discord4j</groupId>
<artifactId>discord4j-core</artifactId>
<version>3.0.3</version>
<version>3.0.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-jdk14 -->
<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
* @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);
if (!message.getContent().isPresent())
return ret; //Pin messages and such, let the mcchat listener deal with it
val content = message.getContent().get();
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 ret;
tmp = ret.then(channel.type()); // Fun
}
final StringBuilder cmdwithargs = new StringBuilder(content);
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);
System.out.println("A");
return message.getChannel().flatMap(channel -> {
Mono<?> tmp = ret;
if (!mentionedonly) { //mentionedonly conditions are in CommonListeners
System.out.println("B");
//System.out.println("Channel type: " + commandChannel.getClass().getSimpleName());
//System.out.println("Guild: " + ((TextChannel) commandChannel).getGuildId());
if (!(channel instanceof PrivateChannel)
&& !(content.charAt(0) == DiscordPlugin.getPrefix()
&& channel.getId().asLong() == commandChannel.getId().asLong())) //
return ret;
System.out.println("C");
tmp = ret.then(channel.type()).thenReturn(true); // Fun (this true is ignored - x)
}
return Mono.empty();
}).map(m -> false).defaultIfEmpty(true);
final StringBuilder cmdwithargs = new StringBuilder(content);
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) {

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.RoleDeleteEvent;
import discord4j.core.event.domain.role.RoleUpdateEvent;
import discord4j.core.object.entity.PrivateChannel;
import lombok.val;
import reactor.core.publisher.Mono;
@ -41,16 +42,23 @@ public class CommonListeners {
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
return commandCh.filterWhen(ch -> event.getMessage().getChannel().map(mch ->
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
System.out.println("Run command 1");
return CommandListener.runCommand(event.getMessage(), ch, true); //#bot is handled here
}).filterWhen(ch -> {
System.out.println("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 -> 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))
.subscribe();
/*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();
final boolean hasCustomChat = MCChatCustom.hasCustomChat(ev.getMessage().getChannelId());
return ev.getMessage().getChannel().filter(channel -> {
System.out.println("Filter 1");
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 -> {
System.out.println("Filter 2");
return !(channel instanceof PrivateChannel //Only in private chat
&& ev.getMessage().getContent().isPresent()
&& ev.getMessage().getContent().get().length() < "/mcchat<>".length()

View file

@ -210,14 +210,17 @@ public class MCChatUtils {
*/
public static void resetLastMessage(Channel channel) {
if (notEnabled()) return;
System.out.println("Reset last message");
if (channel.getId().asLong() == module.chatChannel().get().asLong()) {
(lastmsgdata == null ? lastmsgdata = new LastMsgData(module.chatChannelMono().block(), null)
: lastmsgdata).message = null;
System.out.println("Reset done: public chat");
return;
} // 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) {
if (data.channel.getId().asLong() == channel.getId().asLong()) {
data.message = null;
System.out.println("Reset done: private/custom chat");
return;
}
}

View file

@ -26,7 +26,7 @@ public class GameRoleModule extends Component<DiscordPlugin> {
@Override
protected void enable() {
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
@ -46,11 +46,14 @@ public class GameRoleModule extends Component<DiscordPlugin> {
if (roleEvent instanceof RoleCreateEvent) {
Bukkit.getScheduler().runTaskLaterAsynchronously(DiscordPlugin.plugin, () -> {
Role role=((RoleCreateEvent) roleEvent).getRole();
if (!grm.isGameRole(role))
return; //Deleted or not a game role
GameRoles.add(role.getName());
if (logChannel != null)
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();
grm.isGameRole(role).flatMap(b -> {
if (!b)
return Mono.empty(); //Deleted or not a game role
GameRoles.add(role.getName());
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);
} else if (roleEvent instanceof RoleDeleteEvent) {
Role role=((RoleDeleteEvent) roleEvent).getRole().orElse(null);
@ -64,30 +67,33 @@ public class GameRoleModule extends Component<DiscordPlugin> {
return;
}
Role or=event.getOld().get();
if (!grm.isGameRole(event.getCurrent())) {
if (GameRoles.remove(or.getName()) && logChannel != null)
logChannel.flatMap(ch -> ch.createMessage("Removed " + or.getName() + " as a game role because it's color changed.")).subscribe();
} else {
if (GameRoles.contains(or.getName()) && or.getName().equals(event.getCurrent().getName()))
return;
boolean removed = GameRoles.remove(or.getName()); //Regardless of whether it was a game role
GameRoles.add(event.getCurrent().getName()); //Add it because it has no color
if (logChannel != null) {
if (removed)
logChannel.flatMap(ch -> ch.createMessage("Changed game role from " + or.getName() + " to " + event.getCurrent().getName() + ".")).subscribe();
else
logChannel.flatMap(ch -> ch.createMessage("Added " + event.getCurrent().getName() + " as game role because it has the default color.")).subscribe();
grm.isGameRole(event.getCurrent()).flatMap(b -> {
if (!b) {
if (GameRoles.remove(or.getName()) && logChannel != null)
return logChannel.flatMap(ch -> ch.createMessage("Removed " + or.getName() + " as a game role because it's color changed."));
} else {
if (GameRoles.contains(or.getName()) && or.getName().equals(event.getCurrent().getName()))
return Mono.empty();
boolean removed = GameRoles.remove(or.getName()); //Regardless of whether it was a game role
GameRoles.add(event.getCurrent().getName()); //Add it because it has no color
if (logChannel != null) {
if (removed)
return logChannel.flatMap(ch -> ch.createMessage("Changed game role from " + or.getName() + " to " + event.getCurrent().getName() + "."));
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 boolean isGameRole(Role r) {
private Mono<Boolean> isGameRole(Role r) {
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);
return r.getColor().equals(rc)
&& DiscordPlugin.dc.getSelf().block().asMember(DiscordPlugin.mainServer.getId()).block().hasHigherRoles(Collections.singleton(r)).block(); //Below one of our roles
return Mono.just(r.getColor().equals(rc)).filter(b -> b).flatMap(b ->
DiscordPlugin.dc.getSelf().flatMap(u -> u.asMember(DiscordPlugin.mainServer.getId())).flatMap(m -> m.hasHigherRoles(Collections.singleton(r)))) //Below one of our roles
.defaultIfEmpty(false);
}
}