ConanServerManager/lib/structs/discordHookHelper.dart
2024-06-16 20:13:35 -07:00

63 lines
1.8 KiB
Dart

import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:libac_dart/nbt/NbtUtils.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.enabled) 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;
bool enabled = false;
DiscordHookProps(
{required this.url, required this.serverName, required this.enabled});
CompoundTag serialize() {
CompoundTag ct = CompoundTag();
ct.put(TAG_URL, StringTag.valueOf(url));
ct.put(TAG_SERVER_NAME, StringTag.valueOf(serverName));
NbtUtils.writeBoolean(ct, TAG_ENABLED, enabled);
return ct;
}
static DiscordHookProps deserialize(CompoundTag ct) {
return DiscordHookProps(
url: ct.get(TAG_URL)!.asString(),
serverName: ct.get(TAG_SERVER_NAME)!.asString(),
enabled: NbtUtils.readBoolean(ct, TAG_ENABLED));
}
static const String TAG_URL = "url";
static const String TAG_SERVER_NAME = "serverName";
static const String TAG_NAME = "discord";
static const String TAG_ENABLED = "enabled";
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;
}