Add discord webhooks
This commit is contained in:
parent
cbf0279c42
commit
a17e0452a0
6 changed files with 208 additions and 2 deletions
57
lib/structs/discordHookHelper.dart
Normal file
57
lib/structs/discordHookHelper.dart
Normal file
|
@ -0,0 +1,57 @@
|
|||
import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:libac_dart/nbt/impl/CompoundTag.dart';
|
||||
import 'package:libac_dart/nbt/impl/StringTag.dart';
|
||||
|
||||
class DiscordHookHelper {
|
||||
static Future<void> sendWebHook(DiscordHookProps? props, int colorCode,
|
||||
String title, String content) async {
|
||||
if (props == null) return; // The webhook setting is not yet set up
|
||||
var js = json.encode({
|
||||
"content": "",
|
||||
"embeds": [
|
||||
{
|
||||
"title": title,
|
||||
"description": content,
|
||||
"color": colorCode,
|
||||
"author": {"name": props.serverName}
|
||||
}
|
||||
],
|
||||
"attachments": []
|
||||
});
|
||||
|
||||
Dio dio = Dio();
|
||||
dio.post(props.url, data: js);
|
||||
}
|
||||
}
|
||||
|
||||
class DiscordHookProps {
|
||||
String url;
|
||||
String serverName;
|
||||
|
||||
DiscordHookProps({required this.url, required this.serverName});
|
||||
|
||||
CompoundTag serialize() {
|
||||
CompoundTag ct = CompoundTag();
|
||||
ct.put(TAG_URL, StringTag.valueOf(url));
|
||||
ct.put(TAG_SERVER_NAME, StringTag.valueOf(serverName));
|
||||
|
||||
return ct;
|
||||
}
|
||||
|
||||
static DiscordHookProps deserialize(CompoundTag ct) {
|
||||
return DiscordHookProps(
|
||||
url: ct.get(TAG_URL)!.asString(),
|
||||
serverName: ct.get(TAG_SERVER_NAME)!.asString());
|
||||
}
|
||||
|
||||
static const String TAG_URL = "url";
|
||||
static const String TAG_SERVER_NAME = "serverName";
|
||||
static const String TAG_NAME = "discord";
|
||||
|
||||
static const int ONLINE_ALERT = 1869056;
|
||||
static const int OFFLINE_ALERT = 8716288;
|
||||
static const int ALERT = 21893; // non-intrusive
|
||||
static const int ALERT_INTRUSIVE = 6291589;
|
||||
}
|
|
@ -5,11 +5,13 @@ import 'package:libac_dart/nbt/impl/ListTag.dart';
|
|||
import 'package:libac_dart/utils/TimeUtils.dart';
|
||||
import 'package:servermanager/structs/autorestarts.dart';
|
||||
import 'package:servermanager/structs/credentials.dart';
|
||||
import 'package:servermanager/structs/discordHookHelper.dart';
|
||||
import 'package:servermanager/structs/mod.dart';
|
||||
import 'package:servermanager/structs/serversettings.dart';
|
||||
|
||||
class SettingsEntry {
|
||||
List<Mod> mods = [];
|
||||
DiscordHookProps? discord;
|
||||
Credentials? steam_creds;
|
||||
bool pterodactylMode = true; // Default is to be compatible
|
||||
AutomaticRestartInfo timer =
|
||||
|
@ -33,6 +35,10 @@ class SettingsEntry {
|
|||
st.pterodactylMode = NbtUtils.readBoolean(tag, "pterodactyl");
|
||||
}
|
||||
|
||||
if (tag.containsKey(DiscordHookProps.TAG_NAME))
|
||||
st.discord = DiscordHookProps.deserialize(
|
||||
tag.get(DiscordHookProps.TAG_NAME)!.asCompoundTag());
|
||||
|
||||
st.mods.clear();
|
||||
ListTag lMods = tag.get("mods") as ListTag;
|
||||
for (Tag tag in lMods.value) {
|
||||
|
@ -55,6 +61,9 @@ class SettingsEntry {
|
|||
}
|
||||
tag.put("mods", lMods);
|
||||
|
||||
if (discord != null)
|
||||
tag.put(DiscordHookProps.TAG_NAME, discord!.serialize());
|
||||
|
||||
return tag;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue