More reacts

This commit is contained in:
Norbi Peti 2019-05-06 15:47:01 +02:00
parent b68456e6f4
commit f266924a9a
6 changed files with 37 additions and 30 deletions

View file

@ -38,7 +38,7 @@ public class ChromaBot {
* @param message
* The message to send, duh (use {@link MessageChannel#createMessage(String)})
*/
public void sendMessage(Function<MessageChannel, Mono<Message>> message) {
public void sendMessage(Function<Mono<MessageChannel>, Mono<Message>> message) {
MCChatUtils.forAllMCChat(ch -> message.apply(ch).subscribe());
}
@ -48,7 +48,7 @@ public class ChromaBot {
* @param message The message to send, duh
* @param toggle The toggle type for channelcon
*/
public void sendMessageCustomAsWell(Function<MessageChannel, Mono<Message>> message, @Nullable ChannelconBroadcast toggle) {
public void sendMessageCustomAsWell(Function<Mono<MessageChannel>, Mono<Message>> message, @Nullable ChannelconBroadcast toggle) {
MCChatUtils.forCustomAndAllMCChat(ch -> message.apply(ch).subscribe(), toggle, false);
}

View file

@ -145,4 +145,8 @@ public final class DPUtils {
}).filter(ch -> ch instanceof MessageChannel).cast(MessageChannel.class);
}
public static Mono<MessageChannel> getMessageChannel(ConfigData<Snowflake> config) {
return getMessageChannel(config.getPath(), config.get());
}
}

View file

@ -6,6 +6,7 @@ import buttondevteam.lib.TBMCDebugMessageEvent;
import discord4j.core.object.entity.MessageChannel;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import reactor.core.publisher.Mono;
public class DebugMessageListener implements Listener {
@EventHandler
@ -18,7 +19,7 @@ public class DebugMessageListener implements Listener {
if (DiscordPlugin.SafeMode || !ComponentManager.isEnabled(ExceptionListenerModule.class))
return;
try {
MessageChannel mc = ExceptionListenerModule.getChannel();
Mono<MessageChannel> mc = ExceptionListenerModule.getChannel();
if (mc == null) return;
StringBuilder sb = new StringBuilder();
sb.append("```").append("\n");
@ -26,7 +27,7 @@ public class DebugMessageListener implements Listener {
message = message.substring(0, 2000);
sb.append(message).append("\n");
sb.append("```");
mc.createMessage(sb.toString()).subscribe();
mc.flatMap(ch -> ch.createMessage(sb.toString())).subscribe();
} catch (Exception ex) {
ex.printStackTrace();
}

View file

@ -18,6 +18,7 @@ import org.bukkit.event.HandlerList;
import org.bukkit.plugin.AuthorNagException;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredListener;
import reactor.core.publisher.Mono;
import javax.annotation.Nullable;
import java.util.Arrays;
@ -110,11 +111,11 @@ public class MCChatUtils {
return null;
}
public static void forAllMCChat(Consumer<MessageChannel> action) {
public static void forAllMCChat(Consumer<Mono<MessageChannel>> action) {
if (notEnabled()) return;
action.accept(module.chatChannel().get());
action.accept(module.chatChannelMono());
for (LastMsgData data : MCChatPrivate.lastmsgPerUser)
action.accept(data.channel);
action.accept(Mono.just(data.channel));
// lastmsgCustom.forEach(cc -> action.accept(cc.channel)); - Only send relevant messages to custom chat
}
@ -125,11 +126,11 @@ public class MCChatUtils {
* @param toggle The toggle to check
* @param hookmsg Whether the message is also sent from the hook
*/
public static void forCustomAndAllMCChat(Consumer<MessageChannel> action, @Nullable ChannelconBroadcast toggle, boolean hookmsg) {
public static void forCustomAndAllMCChat(Consumer<Mono<MessageChannel>> action, @Nullable ChannelconBroadcast toggle, boolean hookmsg) {
if (notEnabled()) return;
if (!GeneralEventBroadcasterModule.isHooked() || !hookmsg)
forAllMCChat(action);
final Consumer<MCChatCustom.CustomLMD> customLMDConsumer = cc -> action.accept(cc.channel);
final Consumer<MCChatCustom.CustomLMD> customLMDConsumer = cc -> action.accept(Mono.just(cc.channel));
if (toggle == null)
MCChatCustom.lastmsgCustom.forEach(customLMDConsumer);
else
@ -143,7 +144,7 @@ public class MCChatUtils {
* @param sender The sender to check perms of or null to send to all that has it toggled
* @param toggle The toggle to check or null to send to all allowed
*/
public static void forAllowedCustomMCChat(Consumer<MessageChannel> action, @Nullable CommandSender sender, @Nullable ChannelconBroadcast toggle) {
public static void forAllowedCustomMCChat(Consumer<Mono<MessageChannel>> action, @Nullable CommandSender sender, @Nullable ChannelconBroadcast toggle) {
if (notEnabled()) return;
MCChatCustom.lastmsgCustom.stream().filter(clmd -> {
//new TBMCChannelConnectFakeEvent(sender, clmd.mcchannel).shouldSendTo(clmd.dcp) - Thought it was this simple hehe - Wait, it *should* be this simple
@ -152,7 +153,7 @@ public class MCChatUtils {
if (sender == null)
return true;
return clmd.groupID.equals(clmd.mcchannel.getGroupID(sender));
}).forEach(cc -> action.accept(cc.channel)); //TODO: Send error messages on channel connect
}).forEach(cc -> action.accept(Mono.just(cc.channel))); //TODO: Send error messages on channel connect
}
/**
@ -163,29 +164,29 @@ public class MCChatUtils {
* @param toggle The toggle to check or null to send to all allowed
* @param hookmsg Whether the message is also sent from the hook
*/
public static void forAllowedCustomAndAllMCChat(Consumer<MessageChannel> action, @Nullable CommandSender sender, @Nullable ChannelconBroadcast toggle, boolean hookmsg) {
public static void forAllowedCustomAndAllMCChat(Consumer<Mono<MessageChannel>> action, @Nullable CommandSender sender, @Nullable ChannelconBroadcast toggle, boolean hookmsg) {
if (notEnabled()) return;
if (!GeneralEventBroadcasterModule.isHooked() || !hookmsg)
forAllMCChat(action);
forAllowedCustomMCChat(action, sender, toggle);
}
public static Consumer<MessageChannel> send(String message) {
return ch -> ch.createMessage(DPUtils.sanitizeString(message)).subscribe();
public static Consumer<Mono<MessageChannel>> send(String message) {
return ch -> ch.flatMap(mc -> mc.createMessage(DPUtils.sanitizeString(message))).subscribe();
}
public static void forAllowedMCChat(Consumer<MessageChannel> action, TBMCSystemChatEvent event) {
public static void forAllowedMCChat(Consumer<Mono<MessageChannel>> action, TBMCSystemChatEvent event) {
if (notEnabled()) return;
if (event.getChannel().isGlobal())
action.accept(module.chatChannel().get());
action.accept(module.chatChannelMono());
for (LastMsgData data : MCChatPrivate.lastmsgPerUser)
if (event.shouldSendTo(getSender(data.channel.getId(), data.user)))
action.accept(data.channel);
action.accept(Mono.just(data.channel)); //TODO: Only store ID?
MCChatCustom.lastmsgCustom.stream().filter(clmd -> {
if (!clmd.brtoggles.contains(event.getTarget()))
return false;
return event.shouldSendTo(clmd.dcp);
}).map(clmd -> clmd.channel).forEach(action);
}).map(clmd -> Mono.just(clmd.channel)).forEach(action);
}
/**
@ -209,8 +210,8 @@ public class MCChatUtils {
*/
public static void resetLastMessage(Channel channel) {
if (notEnabled()) return;
if (channel.getId().asLong() == module.chatChannel().get().getId().asLong()) {
(lastmsgdata == null ? lastmsgdata = new LastMsgData(module.chatChannel().get(), null)
if (channel.getId().asLong() == module.chatChannel().get().asLong()) {
(lastmsgdata == null ? lastmsgdata = new LastMsgData(module.chatChannelMono().block(), null)
: lastmsgdata).message = null;
return;
} // Don't set the whole object to null, the player and channel information should be preserved

View file

@ -51,9 +51,9 @@ class MCListener implements Listener {
if (dp != null) {
val user = DiscordPlugin.dc.getUserById(Snowflake.of(dp.getDiscordID())).block();
MCChatUtils.addSender(MCChatUtils.OnlineSenders, dp.getDiscordID(),
new DiscordPlayerSender(user, Objects.requireNonNull(user).getPrivateChannel().block(), p));
new DiscordPlayerSender(user, Objects.requireNonNull(user).getPrivateChannel().block(), p)); //TODO: Don't block
MCChatUtils.addSender(MCChatUtils.OnlineSenders, dp.getDiscordID(),
new DiscordPlayerSender(user, module.chatChannel().get(), p)); //Stored per-channel
new DiscordPlayerSender(user, module.chatChannelMono().block(), p)); //Stored per-channel
}
final String message = e.GetPlayer().PlayerName().get() + " joined the game";
MCChatUtils.forAllowedCustomAndAllMCChat(MCChatUtils.send(message), e.getPlayer(), ChannelconBroadcast.JOINLEAVE, true);
@ -124,7 +124,7 @@ class MCListener implements Listener {
String msg = (e.getValue() ? "M" : "Unm") + "uted user: " + user.getUsername() + "#" + user.getDiscriminator();
DPUtils.getLogger().info(msg);
if (modlog != null)
return modlog.createMessage(msg);
return modlog.flatMap(ch -> ch.createMessage(msg));
return Mono.empty();
})).subscribe();
}

View file

@ -4,7 +4,7 @@ import buttondevteam.core.ComponentManager;
import buttondevteam.discordplugin.DPUtils;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.architecture.Component;
import buttondevteam.lib.architecture.ConfigData;
import buttondevteam.lib.architecture.ReadOnlyConfigData;
import discord4j.core.event.domain.role.RoleCreateEvent;
import discord4j.core.event.domain.role.RoleDeleteEvent;
import discord4j.core.event.domain.role.RoleEvent;
@ -13,6 +13,7 @@ import discord4j.core.object.entity.MessageChannel;
import discord4j.core.object.entity.Role;
import lombok.val;
import org.bukkit.Bukkit;
import reactor.core.publisher.Mono;
import java.awt.*;
import java.util.Collections;
@ -33,7 +34,7 @@ public class GameRoleModule extends Component<DiscordPlugin> {
}
private ConfigData<MessageChannel> logChannel() {
private ReadOnlyConfigData<Mono<MessageChannel>> logChannel() {
return DPUtils.channelData(getConfig(), "logChannel", 239519012529111040L);
}
@ -49,13 +50,13 @@ public class GameRoleModule extends Component<DiscordPlugin> {
return; //Deleted or not a game role
GameRoles.add(role.getName());
if (logChannel != null)
logChannel.createMessage("Added " + role.getName() + " as game role. If you don't want this, change the role's color from the default.").subscribe();
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();
}, 100);
} else if (roleEvent instanceof RoleDeleteEvent) {
Role role=((RoleDeleteEvent) roleEvent).getRole().orElse(null);
if(role==null) return;
if (GameRoles.remove(role.getName()) && logChannel != null)
logChannel.createMessage("Removed " + role.getName() + " as a game role.").subscribe();
logChannel.flatMap(ch -> ch.createMessage("Removed " + role.getName() + " as a game role.")).subscribe();
} else if (roleEvent instanceof RoleUpdateEvent) {
val event = (RoleUpdateEvent) roleEvent;
if(!event.getOld().isPresent()) {
@ -65,7 +66,7 @@ public class GameRoleModule extends Component<DiscordPlugin> {
Role or=event.getOld().get();
if (!grm.isGameRole(event.getCurrent())) {
if (GameRoles.remove(or.getName()) && logChannel != null)
logChannel.createMessage("Removed " + or.getName() + " as a game role because it's color changed.").subscribe();
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;
@ -73,9 +74,9 @@ public class GameRoleModule extends Component<DiscordPlugin> {
GameRoles.add(event.getCurrent().getName()); //Add it because it has no color
if (logChannel != null) {
if (removed)
logChannel.createMessage("Changed game role from " + or.getName() + " to " + event.getCurrent().getName() + ".").subscribe();
logChannel.flatMap(ch -> ch.createMessage("Changed game role from " + or.getName() + " to " + event.getCurrent().getName() + ".")).subscribe();
else
logChannel.createMessage("Added " + event.getCurrent().getName() + " as game role because it has the default color.").subscribe();
logChannel.flatMap(ch -> ch.createMessage("Added " + event.getCurrent().getName() + " as game role because it has the default color.")).subscribe();
}
}
}