Skip to content

Instantly share code, notes, and snippets.

@tterrag1098
Created March 21, 2019 06:24
Show Gist options
  • Save tterrag1098/03f3b570535c3dbc65b51344afc9187b to your computer and use it in GitHub Desktop.
Save tterrag1098/03f3b570535c3dbc65b51344afc9187b to your computer and use it in GitHub Desktop.
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