Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public class PrettifyMessageCreate extends MessageConverter {
public static DiscordClient client;
private static Cache<Long, String> channelNames = CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.HOURS).build();
public String convert(ILoggingEvent event) {
if (event.getLoggerName().startsWith("discord4j.gateway.inbound.0")) {
for (Object param : event.getArgumentArray()) {
if (param instanceof GatewayPayload) {
GatewayPayload<?> payload = (GatewayPayload) param;
if (Opcode.DISPATCH.equals(payload.getOp()) && EventNames.MESSAGE_CREATE.equals(payload.getType())) {
MessageCreate msg = (MessageCreate) payload.getData();
String channelName = channelNames.getIfPresent(msg.getChannelId());
if (channelName == null) {
channelName = Long.toUnsignedString(msg.getChannelId());
if (client != null) {
Mono<Channel> chan = client.getChannelById(Snowflake.of(msg.getChannelId())).cache();
chan.ofType(GuildChannel.class)
.doOnNext(c -> channelNames.put(msg.getChannelId(), "#" + c.getName()))
.then(chan.ofType(PrivateChannel.class))
.flatMap(c -> c.getRecipients().next())
.doOnNext(u -> channelNames.put(msg.getChannelId(), "[DM]"))
.subscribe();
}
}
return " (" + channelName + " <" + msg.getAuthor().getUsername() + "> " + msg.getContent() + ")";
}
}
}
}
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment