akon1248

Discord Logger

Feb 19th, 2020
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. options:
  2. token: <BOT_TOKEN>
  3. channelId: <CHANNEL_ID>
  4. import:
  5. com.btk5h.skriptmirror.JavaType
  6. org.apache.logging.log4j.LogManager
  7. javassist.ClassPool
  8. javassist.CtField
  9. javassist.CtMethod
  10. javassist.CtNewConstructor
  11. org.bukkit.Bukkit
  12. on load:
  13. set {_enabledvixio} to Bukkit.getPluginManager().isPluginEnabled("Vixio")
  14. set {_enableddiscordsrv} to Bukkit.getPluginManager().isPluginEnabled("DiscordSRV")
  15. {_enabledvixio} or {_enableddiscordsrv}
  16. set {_cp} to ClassPool.getDefault()
  17. try {_cp}.get("com.akon.discordlogger.DiscordLogger") is not set
  18. if {_enabledvixio}:
  19. {_cp}.insertClassPath(Bukkit.getPluginManager().getPlugin("Vixio").[org.bukkit.plugin.java.JavaPlugin]getFile().toString())
  20. else if {_enableddiscordsrv}:
  21. {_cp}.insertClassPath(Bukkit.getPluginManager().getPlugin("DiscordSRV").[org.bukkit.plugin.java.JavaPlugin]getFile().toString())
  22. {_cp}.importPackage("net.dv8tion.jda.core")
  23. {_cp}.importPackage("net.dv8tion.jda.core.entities")
  24. {_cp}.importPackage("org.apache.logging.log4j")
  25. {_cp}.importPackage("org.apache.logging.log4j.core")
  26. set {_ctclass} to {_cp}.makeClass("com.akon.discordlogger.DiscordLogger")
  27. {_ctclass}.setSuperclass({_cp}.get("org.apache.logging.log4j.core.filter.AbstractFilter"))
  28. {_ctclass}.addField(CtField.make("private JDA bot = null;", {_ctclass}))
  29. {_ctclass}.addField(CtField.make("private TextChannel channel = null;", {_ctclass}))
  30. {_ctclass}.addConstructor(CtNewConstructor.make("public DiscordLogger(String token, long channelId) {super(org.apache.logging.log4j.core.Filter.Result.NEUTRAL, org.apache.logging.log4j.core.Filter.Result.NEUTRAL); try {this.bot = new JDABuilder(AccountType.BOT).setToken(token).build().awaitReady(); this.channel = this.bot.getTextChannelById(channelId);} catch (Exception ex) {ex.printStackTrace();}}", {_ctclass}))
  31. {_ctclass}.addMethod(CtMethod.make("public void sendMessageToDiscord(String message) {this.channel.sendMessage(new MessageBuilder(message).build()).queue();}", {_ctclass}))
  32. {_ctclass}.addMethod(CtMethod.make("private org.apache.logging.log4j.core.Filter.Result log(Level level, String message, Throwable throwable) {if (this.bot != null && this.channel != null) {message = message.replaceAll((char)27 + ""\\[(\\d{1,2}(;\\d{1,2})*)?m"", """"); java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(""HH:mm:ss""); java.util.Date date = new java.util.Date(System.currentTimeMillis()); this.sendMessageToDiscord(""["" + format.format(date) + "" "" + level.name() + ""]: "" + message);} return org.apache.logging.log4j.core.Filter.Result.NEUTRAL;}", {_ctclass}))
  33. {_ctclass}.addMethod(CtMethod.make("public org.apache.logging.log4j.core.Filter.Result filter(Logger logger, Level level, Marker marker, String s, Object[] object) {return this.log(level, s, null);}", {_ctclass}))
  34. {_ctclass}.addMethod(CtMethod.make("public org.apache.logging.log4j.core.Filter.Result filter(Logger logger, Level level, Marker marker, Object o, Throwable throwable) {return this.log(level, o.toString(), throwable);}", {_ctclass}))
  35. {_ctclass}.addMethod(CtMethod.make("public org.apache.logging.log4j.core.Filter.Result filter(Logger logger, Level level, Marker marker, org.apache.logging.log4j.message.Message message, Throwable throwable) {return this.log(level, message.getFormattedMessage(), throwable);}", {_ctclass}))
  36. {_ctclass}.addMethod(CtMethod.make("public org.apache.logging.log4j.core.Filter.Result filter(LogEvent logEvent) {return this.log(logEvent.getLevel(), logEvent.getMessage().getFormattedMessage(), logEvent.getThrown());}", {_ctclass}))
  37. set {_javatype} to new JavaType({_ctclass}.toClass(Bukkit.getPluginManager().getPlugin("Skript").[org.bukkit.plugin.java.JavaPlugin]getClassLoader()))
  38. set {discordlogger} to new {_javatype}("{@token}", {@channelId})
  39. LogManager.getRootLogger().addFilter({discordlogger})
Advertisement
Add Comment
Please, Sign In to add comment