From af8e46110cf77b5dae95e324a2163867c116b3e6 Mon Sep 17 00:00:00 2001 From: KillerBossOrig Date: Mon, 7 Aug 2023 23:19:38 +0200 Subject: [PATCH] Add interactions / replying --- lib/src/classes/interaction.dart | 18 ++++++++++++++++++ lib/src/classes/message/message.dart | 2 +- lib/src/main.dart | 4 ++++ lib/src/requests.dart | 14 ++++++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 lib/src/classes/interaction.dart diff --git a/lib/src/classes/interaction.dart b/lib/src/classes/interaction.dart new file mode 100644 index 0000000..3c3ca36 --- /dev/null +++ b/lib/src/classes/interaction.dart @@ -0,0 +1,18 @@ +import 'package:tn_discord/src/requests.dart'; + +import 'message/message.dart'; + +class Interaction { + late String token; + late String id; + + Interaction(dynamic data) { + token = data["token"]; + id = data["id"]; + print(data); + } + + reply(Message message) { + return interactionReply(id, token, message.exportable()); + } +} \ No newline at end of file diff --git a/lib/src/classes/message/message.dart b/lib/src/classes/message/message.dart index bdf56e4..f852f5e 100644 --- a/lib/src/classes/message/message.dart +++ b/lib/src/classes/message/message.dart @@ -4,7 +4,7 @@ class Message { Message({ this.content }); /// Returns an object rapresentation of the message - exportable() { + Map exportable() { return { "content": content, }; diff --git a/lib/src/main.dart b/lib/src/main.dart index fd347d6..9dddcac 100644 --- a/lib/src/main.dart +++ b/lib/src/main.dart @@ -5,6 +5,7 @@ import "package:events_emitter/events_emitter.dart"; import "classes/guild/guild.dart"; import "classes/guild/guild_manager.dart"; +import "classes/interaction.dart"; import "requests.dart"; final version = "10"; @@ -140,6 +141,9 @@ class Client extends EventEmitter { emit("GUILD_CREATE"); } break; + case "INTERACTION_CREATE": + emit("INTERACTION_CREATE", Interaction(event["d"])); + break; } }, onDone: () { switch (ws.closeCode) { diff --git a/lib/src/requests.dart b/lib/src/requests.dart index 7d0c8af..775017b 100644 --- a/lib/src/requests.dart +++ b/lib/src/requests.dart @@ -85,4 +85,18 @@ class Sender { res = json.decode(res.body); return res; } +} + +Future interactionReply(String id, String token, Map content) async { + var bd = { + "type": 4, + "data": content + }; + + dynamic res = await http.post(Uri.parse("$apiURL/interactions/$id/$token/callback"), body: json.encode(bd), headers: { "Content-Type": "application/json" }); + if (res.statusCode != 204) { + throw Exception("Error ${res.statusCode} replying to the interaction"); + } + res = json.decode(res.body); + return res; } \ No newline at end of file