Created
March 21, 2019 06:24
-
-
Save tterrag1098/03f3b570535c3dbc65b51344afc9187b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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