Added a litebans webhook (uncomplete)

This commit is contained in:
trouper
2023-08-22 05:50:59 -05:00
parent f678879f65
commit 5c4a59c975
6 changed files with 522 additions and 0 deletions

View File

@@ -15,10 +15,15 @@ repositories {
name = "sonatype"
url = "https://oss.sonatype.org/content/groups/public/"
}
maven {
name = "jitpack"
url = "https://jitpack.io"
}
}
dependencies {
compileOnly "org.spigotmc:spigot-api:1.19.4-R0.1-SNAPSHOT"
compileOnly "com.gitlab.ruany:LiteBansAPI:0.4.1"
}
def targetJavaVersion = 16

0
gradlew vendored Normal file → Executable file
View File

View File

@@ -55,6 +55,7 @@ public final class OgreDupeAlias extends JavaPlugin {
pm.registerEvents(new SPBEventListener(), this);
// event for custom items
pm.registerEvents(new CustomItems(), this);
LiteBansEvents.registerEvents();
// Commands
getCommand("forcefield").setExecutor(new ForceFieldCommand());

View File

@@ -0,0 +1,33 @@
package fun.ogre.ogredupealias.data;
public class Emojis {
public static String space = "<:space:1125871914334818446>";
public static String rightSort = "<:rightSort:1125785837255270520>";
public static String arrowRight = "<:arrowRight:1125785471520354304>";
public static String rightDoubleArrow = "<:rightDoubleArrow:1125785800353783868>";
public static String activity = "<:activity:1125785527468167178>";
public static String alarm = "<:alarm:1125790301873770606>";
public static String target = "<:target:1125788461371232307>";
public static String bot = "<:bot:1125791121851826206>";
public static String cancel = "<:cancel:1125785769471127694>";
public static String creation = "<:creation:1125790610729730109>";
public static String date = "<:date:1125790434443145297>";
public static String kick = "<:kick:1125785612595761212>";
public static String members = "<:members:1125791101199077426>";
public static String mute = "<:mute:1125789032937435247>";
public static String noDM = "<:noDM:1125790359423824022>";
public static String owner = "<:owner:1125791175559876669>";
public static String potentialDanger = "<:potentialDanger:1125788513971998741>";
public static String roles = "<:roles:1125790513933594645>";
public static String separator = "<:separator:1125790817626357861>";
public static String splash = "<:splash:1125791213933563905>";
public static String success = "<:success:1125785728161419356>";
public static String suspicious = "<:suspicious:1125790709371371682>";
public static String trustedAdmin = "<:trustedAdmin:1125785574591180822>";
public static String upvoter = "<:upvoter:1125790659735977994>";
public static String vanity = "<:vanity:1125791060594004039>";
public static String webhook = "<:webhook:1125790545638330388>";
public static String failure = "<:failure:1125241087909429369>";
public static String nuke = "<:nuke:1125244368807280702>";
public static String member = "<:member:1125244044407218176>";
}

View File

@@ -0,0 +1,60 @@
package fun.ogre.ogredupealias.events;
import fun.ogre.ogredupealias.OgreDupeAlias;
import fun.ogre.ogredupealias.data.Emojis;
import fun.ogre.ogredupealias.utils.Webhook;
import litebans.api.Entry;
import litebans.api.Events;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import java.awt.*;
import java.io.IOException;
import java.util.UUID;
public class LiteBansEvents extends Events.Listener {
public static void registerEvents() {
Events.get().register(new Events.Listener() {
@Override
public void entryAdded(Entry entry) {
if (entry.getType().equals("ban")) {
UUID target = UUID.fromString(entry.getUuid());
UUID executor = UUID.fromString(entry.getExecutorUUID());
sendBanLog(target,executor,entry.getReason(),entry.getDurationString());
}
}
@Override
public void entryRemoved(Entry entry) {
if (entry.getType().equals("ban")) {
/* This will be done soon */
}
}
});
}
public static void sendBanLog(UUID target, UUID executerUUID, String reason, String time) {
String name = Bukkit.getPlayer(target).getName();
String executor = Bukkit.getPlayer(executerUUID).getName();
Webhook webhook = new Webhook("https://discord.com/api/webhooks/1110731451982422136/U33AFoT3nVpVo2iTO2kVRuHV4F4PdOtJDp8xsTavkmctU0fDKmW0ckxfGtpKKjobH-Cb");
webhook.setAvatarUrl("https://r2.e-z.host/d440b58a-ba90-4839-8df6-8bba298cf817/3lwit5nt.png");
webhook.setUsername("Staff Logs");
Webhook.EmbedObject embed = new Webhook.EmbedObject()
.setAuthor("Staff Punishment","","")
.setTitle("Staff have banned a member")
.setDescription(
Emojis.rightSort + "Username: `" + name + "` " + Emojis.target + "\\n" +
Emojis.space + Emojis.arrowRight + "UUID: `" + target + "`\\n"
)
.addField("Banned By: ", "`" + executor + "` " + Emojis.trustedAdmin, false)
.addField("Reason: ", reason + " " + Emojis.activity, false)
.setColor(Color.red)
.setThumbnail("https://crafatar.com/avatars/" + target + "?size=64&&overlay");
webhook.addEmbed(embed);
try {
webhook.execute();
} catch (IOException e) {
OgreDupeAlias.log.info(e.toString());
}
}
}

View File

@@ -0,0 +1,423 @@
package fun.ogre.ogredupealias.utils;
import javax.net.ssl.HttpsURLConnection;
import java.awt.*;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.net.URL;
import java.util.List;
import java.util.*;
/**
* Class used to execute Discord Webhooks with low effort
*/
public class Webhook {
private final String url;
private String content;
private String username;
private String avatarUrl;
private boolean tts;
private List<EmbedObject> embeds = new ArrayList<>();
private List<Attachment> attachments = new ArrayList<>();
/**
* Constructs a new DiscordWebhook instance
*
* @param url The webhook URL obtained in Discord
*/
public Webhook(String url) {
this.url = url;
}
public void setContent(String content) {
this.content = content;
}
public void setUsername(String username) {
this.username = username;
}
public void setAvatarUrl(String avatarUrl) {
this.avatarUrl = avatarUrl;
}
public void setTts(boolean tts) {
this.tts = tts;
}
public void addEmbed(EmbedObject embed) {
this.embeds.add(embed);
}
public void addAttachment(String filename, String content) {
attachments.add(new Attachment(filename, content));
}
public void execute() throws IOException {
if (this.content == null && this.embeds.isEmpty() && this.attachments.isEmpty()) {
throw new IllegalArgumentException("Set content, add at least one EmbedObject, or add an attachment");
}
JSONObject json = new JSONObject();
json.put("content", this.content);
json.put("username", this.username);
json.put("avatar_url", this.avatarUrl);
json.put("tts", this.tts);
if (!this.embeds.isEmpty()) {
List<JSONObject> embedObjects = new ArrayList<>();
for (EmbedObject embed : this.embeds) {
JSONObject jsonEmbed = new JSONObject();
jsonEmbed.put("title", embed.getTitle());
jsonEmbed.put("description", embed.getDescription());
jsonEmbed.put("url", embed.getUrl());
if (embed.getColor() != null) {
Color color = embed.getColor();
int rgb = color.getRed();
rgb = (rgb << 8) + color.getGreen();
rgb = (rgb << 8) + color.getBlue();
jsonEmbed.put("color", rgb);
}
EmbedObject.Footer footer = embed.getFooter();
EmbedObject.Image image = embed.getImage();
EmbedObject.Thumbnail thumbnail = embed.getThumbnail();
EmbedObject.Author author = embed.getAuthor();
List<EmbedObject.Field> fields = embed.getFields();
if (footer != null) {
JSONObject jsonFooter = new JSONObject();
jsonFooter.put("text", footer.getText());
jsonFooter.put("icon_url", footer.getIconUrl());
jsonEmbed.put("footer", jsonFooter);
}
if (image != null) {
JSONObject jsonImage = new JSONObject();
jsonImage.put("url", image.getUrl());
jsonEmbed.put("image", jsonImage);
}
if (thumbnail != null) {
JSONObject jsonThumbnail = new JSONObject();
jsonThumbnail.put("url", thumbnail.getUrl());
jsonEmbed.put("thumbnail", jsonThumbnail);
}
if (author != null) {
JSONObject jsonAuthor = new JSONObject();
jsonAuthor.put("name", author.getName());
jsonAuthor.put("url", author.getUrl());
jsonAuthor.put("icon_url", author.getIconUrl());
jsonEmbed.put("author", jsonAuthor);
}
List<JSONObject> jsonFields = new ArrayList<>();
for (EmbedObject.Field field : fields) {
JSONObject jsonField = new JSONObject();
jsonField.put("name", field.getName());
jsonField.put("value", field.getValue());
jsonField.put("inline", field.isInline());
jsonFields.add(jsonField);
}
jsonEmbed.put("fields", jsonFields.toArray());
embedObjects.add(jsonEmbed);
}
json.put("embeds", embedObjects.toArray());
}
if (!this.attachments.isEmpty()) {
List<JSONObject> attachmentObjects = new ArrayList<>();
for (Attachment attachment : this.attachments) {
JSONObject attachmentObject = new JSONObject();
attachmentObject.put("name", attachment.getFilename());
attachmentObject.put("content", attachment.getContent());
attachmentObjects.add(attachmentObject);
}
json.put("attachments", attachmentObjects.toArray());
}
URL url = new URL(this.url);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.addRequestProperty("Content-Type", "application/json");
connection.addRequestProperty("User-Agent", "Java-DiscordWebhook-BY-Gelox_");
connection.setDoOutput(true);
connection.setRequestMethod("POST");
OutputStream stream = connection.getOutputStream();
stream.write(json.toString().getBytes());
stream.flush();
stream.close();
connection.getInputStream().close(); //I'm not sure why but it doesn't work without getting the InputStream
connection.disconnect();
}
public static class EmbedObject {
private String title;
private String description;
private String url;
private Color color;
private Footer footer;
private Thumbnail thumbnail;
private Image image;
private Author author;
private List<Field> fields = new ArrayList<>();
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public String getUrl() {
return url;
}
public Color getColor() {
return color;
}
public Footer getFooter() {
return footer;
}
public Thumbnail getThumbnail() {
return thumbnail;
}
public Image getImage() {
return image;
}
public Author getAuthor() {
return author;
}
public List<Field> getFields() {
return fields;
}
public EmbedObject setTitle(String title) {
this.title = title;
return this;
}
public EmbedObject setDescription(String description) {
this.description = description;
return this;
}
public EmbedObject setUrl(String url) {
this.url = url;
return this;
}
public EmbedObject setColor(Color color) {
this.color = color;
return this;
}
public EmbedObject setFooter(String text, String icon) {
this.footer = new Footer(text, icon);
return this;
}
public EmbedObject setThumbnail(String url) {
this.thumbnail = new Thumbnail(url);
return this;
}
public EmbedObject setImage(String url) {
this.image = new Image(url);
return this;
}
public EmbedObject setAuthor(String name, String url, String icon) {
this.author = new Author(name, url, icon);
return this;
}
public EmbedObject addField(String name, String value, boolean inline) {
this.fields.add(new Field(name, value, inline));
return this;
}
private class Footer {
private String text;
private String iconUrl;
private Footer(String text, String iconUrl) {
this.text = text;
this.iconUrl = iconUrl;
}
private String getText() {
return text;
}
private String getIconUrl() {
return iconUrl;
}
}
private class Thumbnail {
private String url;
private Thumbnail(String url) {
this.url = url;
}
private String getUrl() {
return url;
}
}
private class Image {
private String url;
private Image(String url) {
this.url = url;
}
private String getUrl() {
return url;
}
}
private class Author {
private String name;
private String url;
private String iconUrl;
private Author(String name, String url, String iconUrl) {
this.name = name;
this.url = url;
this.iconUrl = iconUrl;
}
private String getName() {
return name;
}
private String getUrl() {
return url;
}
private String getIconUrl() {
return iconUrl;
}
}
private class Field {
private String name;
private String value;
private boolean inline;
private Field(String name, String value, boolean inline) {
this.name = name;
this.value = value;
this.inline = inline;
}
private String getName() {
return name;
}
private String getValue() {
return value;
}
private boolean isInline() {
return inline;
}
}
}
private class Attachment {
private String filename;
private String content;
private Attachment(String filename, String content) {
this.filename = filename;
this.content = content;
}
private String getFilename() {
return filename;
}
private String getContent() {
return content;
}
}
private class JSONObject {
private final HashMap<String, Object> map = new HashMap<>();
void put(String key, Object value) {
if (value != null) {
map.put(key, value);
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
Set<Map.Entry<String, Object>> entrySet = map.entrySet();
builder.append("{");
int i = 0;
for (Map.Entry<String, Object> entry : entrySet) {
Object val = entry.getValue();
builder.append(quote(entry.getKey())).append(":");
if (val instanceof String) {
builder.append(quote(String.valueOf(val)));
} else if (val instanceof Integer) {
builder.append(Integer.valueOf(String.valueOf(val)));
} else if (val instanceof Boolean) {
builder.append(val);
} else if (val instanceof JSONObject) {
builder.append(val.toString());
} else if (val.getClass().isArray()) {
builder.append("[");
int len = Array.getLength(val);
for (int j = 0; j < len; j++) {
builder.append(Array.get(val, j).toString()).append(j != len - 1 ? "," : "");
}
builder.append("]");
}
builder.append(++i == entrySet.size() ? "}" : ",");
}
return builder.toString();
}
private String quote(String string) {
return "\"" + string + "\"";
}
}
}