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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue