ConanServerManager/lib/structs/discordHookHelper.dart
2024-06-16 16:42:50 -07:00

57 lines
1.5 KiB
Dart

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;
}