All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.princesslana.smalld.examples.GatewayPayloadLogger Maven / Gradle / Ivy

The newest version!
package com.github.princesslana.smalld.examples;

import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.WriterConfig;
import com.github.princesslana.smalld.SmallD;

/**
 * A bot that will log all payloads received to a channel.
 *
 * 

Requires two environment variables * *

    *
  • SMALLD_TOKEN the bot token *
  • CHANNEL_ID the id of the channel to log payloads *
*/ public class GatewayPayloadLogger { /** * Entrypoint to run the bot. * * @param args command line args */ public static void main(String[] args) { String channel = System.getenv("CHANNEL_ID"); SmallD.run( System.getenv("SMALLD_TOKEN"), smalld -> smalld.onGatewayPayload( p -> { JsonObject in = Json.parse(p).asObject(); if (!isMessageFromBot(in)) { String out = in.toString(WriterConfig.PRETTY_PRINT); if (out.length() > 1950) { out = out.substring(0, 1950) + "..."; } String content = "```javascript\n" + out + "\n```"; smalld.post( "/channels/" + channel + "/messages", Json.object().add("content", content).toString()); } })); } private static boolean isMessageFromBot(JsonObject payload) { if (payload.getInt("op", -1) != 0) { return false; } if (!payload.getString("t", "").equals("MESSAGE_CREATE")) { return false; } return payload.get("d").asObject().get("author").asObject().getBoolean("bot", false); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy