Advertisement
Kalgon

Untitled

Feb 20th, 2024
1,704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 2.27 KB | None | 0 0
  1. package com.zenyte;
  2.  
  3. import java.io.OutputStream;
  4. import java.net.HttpURLConnection;
  5. import java.net.URL;
  6. import java.time.LocalDateTime;
  7. import java.time.format.DateTimeFormatter;
  8.  
  9. /**
  10.  
  11. @author Jack @ The CErver | Feb 5th, 2024
  12. This file should be in com.zenyte on a Zenyte code base.
  13. */
  14.  
  15. public class DiscordLog {
  16.     public static final String ERROR_WEBHOOK = "YOUR URL GOES HERE";
  17.     public static void error(String message) {
  18.         try {
  19.             URL url = new URL(ERROR_WEBHOOK);
  20.             LocalDateTime timestamp1 = LocalDateTime.now();
  21.             DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  22.             String timestamp = timestamp1.format(formatter);
  23.             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  24.             connection.setRequestMethod("POST");
  25.             connection.setRequestProperty("Content-Type", "application/json");
  26.             connection.setDoOutput(true);
  27.  
  28.             String jsonPayload = "{\"content\":\"```["+ timestamp + "] The CErver Logs: " + message + "```\"}";
  29.  
  30.             try (OutputStream os = connection.getOutputStream()) {
  31.                 byte[] input = jsonPayload.getBytes("utf-8");
  32.                 os.write(input, 0, input.length);
  33.             }
  34.  
  35.             int responseCode = connection.getResponseCode();
  36.             if (responseCode == 204) {
  37.                 System.out.println("Message sent to Discord successfully.");
  38.             } else {
  39.                 System.out.println("Failed to send message to Discord. HTTP Response Code: " + responseCode);
  40.                 System.out.println("Failed to send to Discord. HTTP Response Code: " + responseCode);
  41.             }
  42.  
  43.         }
  44.         catch (Exception e) {
  45.             e.printStackTrace();
  46.         }
  47.     }
  48.     // 1: Add your custom channel logs here. Copy the public from error to get started.
  49.     // 2: Underneath public static final String ERROR_WEBHOOK, copy the line and rename ERROR_WEBHOOK to your new hook name.
  50.     // 3: Create your void here. Format: public static void NAME(String message) {
  51.     // 4: Fill the contents with the code from error
  52.     // 5: Change the URL line to the NAME_WEBHOOK you defined in step 2
  53.     // 6: Call your function using DiscordLog.name("Log string");
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement