Add interactions / replying
This commit is contained in:
parent
51e0c294b5
commit
af8e46110c
4 changed files with 37 additions and 1 deletions
18
lib/src/classes/interaction.dart
Normal file
18
lib/src/classes/interaction.dart
Normal file
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,7 +4,7 @@ class Message {
|
||||||
Message({ this.content });
|
Message({ this.content });
|
||||||
|
|
||||||
/// Returns an object rapresentation of the message
|
/// Returns an object rapresentation of the message
|
||||||
exportable() {
|
Map<String, dynamic> exportable() {
|
||||||
return {
|
return {
|
||||||
"content": content,
|
"content": content,
|
||||||
};
|
};
|
||||||
|
|
|
@ -5,6 +5,7 @@ import "package:events_emitter/events_emitter.dart";
|
||||||
|
|
||||||
import "classes/guild/guild.dart";
|
import "classes/guild/guild.dart";
|
||||||
import "classes/guild/guild_manager.dart";
|
import "classes/guild/guild_manager.dart";
|
||||||
|
import "classes/interaction.dart";
|
||||||
import "requests.dart";
|
import "requests.dart";
|
||||||
|
|
||||||
final version = "10";
|
final version = "10";
|
||||||
|
@ -140,6 +141,9 @@ class Client extends EventEmitter {
|
||||||
emit("GUILD_CREATE");
|
emit("GUILD_CREATE");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case "INTERACTION_CREATE":
|
||||||
|
emit("INTERACTION_CREATE", Interaction(event["d"]));
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}, onDone: () {
|
}, onDone: () {
|
||||||
switch (ws.closeCode) {
|
switch (ws.closeCode) {
|
||||||
|
|
|
@ -86,3 +86,17 @@ class Sender {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future interactionReply(String id, String token, Map<String, dynamic> 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;
|
||||||
|
}
|
Reference in a new issue