diff --git a/.gitignore b/.gitignore index 3cceda5..eb78b6c 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,6 @@ # Avoid committing pubspec.lock for library packages; see # https://dart.dev/guides/libraries/private-files#pubspeclock. pubspec.lock + +# Testing +bin/ \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index effe43c..a289218 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +## 1.1.0 +- Changed `webhookclient.send() -> webhookclient.sendText()` +- Add `webhookclient.send(String content, List> embeds)` +- Add `webhookclient.edit(String message-id, {String content, List> embeds})` +- Add `webhookclient.editText(String message-id, String content)` +- Add `webhookclient.get(String message-id)` +- Add `webhookclient.delete(String message-id)` + ## 1.0.0 - Initial version. diff --git a/README.md b/README.md index ef25470..b9b53f5 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,14 @@ # discord.dart +![https://img.shields.io/pub/v/tn_discord?color=red&logo=dart](https://github.com/ThunderNetworkRaD/discord.dart) This package is work in progress. ## Webhooks + +### Initializations ```dart import 'package:tn_discord/tn_discord.dart'; main() { - const webhook = WebookClient(options); - webhook.send(String message); + const webhook = WebookClient(/*Webhook Options*/); } ``` ### Webhook Options diff --git a/lib/src/error_handler.dart b/lib/src/error_handler.dart index 3d77433..1727a01 100644 --- a/lib/src/error_handler.dart +++ b/lib/src/error_handler.dart @@ -1,9 +1,10 @@ -import 'package:http/http.dart'; - -void handleCode(int code, Response fullResponse) { - switch (code) { - default: - break; - } - return; -} +import 'package:http/http.dart'; + +void handleCode(Response res) async { + int code = res.statusCode; + switch (code) { + default: + break; + } + return; +} diff --git a/lib/src/index.dart b/lib/src/index.dart index 6838e4e..5b67b57 100644 --- a/lib/src/index.dart +++ b/lib/src/index.dart @@ -1,3 +1,5 @@ +import "dart:convert"; + import "package:events_emitter/events_emitter.dart"; import "package:http/http.dart"; import "package:tn_discord/src/error_handler.dart"; @@ -27,39 +29,50 @@ class WebhookClient { } } - void sendText(String content) async { + Future> sendText(String content) async { Map body = {"content": content}; Response res = await sendWH(body, token, id); - handleCode(res.statusCode, res); + handleCode(res); + return json.decode(res.body); } - void send({String content = "", List embeds = const []}) async { - Map body = {"content": content, "embeds": embeds}; + Future> send({String? content, List? embeds}) async { + Map body = Utils().createMessage(text: content, embeds: embeds); Response res = await sendWH(body, token, id); - handleCode(res.statusCode, res); + handleCode(res); + return json.decode(res.body); } - void editText(String id, String content) async { + Future> editText(String id, String content) async { Map body = {"content": content}; Response res = await editWH(body, token, this.id, id); - handleCode(res.statusCode, res); + handleCode(res); + return json.decode(res.body); } - void edit(String id, {String content = "", List embeds = const []}) async { + Future> edit(String id, {String content = "", List embeds = const []}) async { Map body = {"content": content, "embeds": embeds}; Response res = await editWH(body, token, this.id, id); - handleCode(res.statusCode, res); + handleCode(res); + return json.decode(res.body); } - Future get(String id) async { + Future> get(String id) async { Response res = await getWH(token, this.id, id); + handleCode(res); - handleCode(res.statusCode, res); + return json.decode(res.body); + } + Future> delete(String id) async { + Response res = await deleteWH(token, this.id, id); + handleCode(res); + + return json.decode(res.body); } } diff --git a/lib/src/message.dart b/lib/src/message.dart index c17ad52..0ca2cb4 100644 --- a/lib/src/message.dart +++ b/lib/src/message.dart @@ -1,17 +1,25 @@ -import 'package:tn_discord/src/index.dart'; -import 'package:http/http.dart' as http; - -Future sendWH(Map content, String token, String id) { - final url = Uri.parse("$apiURL/webhooks/$id/$token"); - return http.post(url, body: content); -} - -Future editWH(Map content, String token, String id, String mid) { - final url = Uri.parse("$apiURL/webhooks/$id/$token/messages/$mid"); - return http.patch(url, body: content); -} - -Future getWH(String token, String id, String mid) { - final url = Uri.parse("$apiURL/webhooks/$id/$token/messages/$mid"); - return http.get(url); -} +import 'package:tn_discord/src/index.dart'; +import 'package:http/http.dart' as http; +import 'dart:convert'; + +final headers = {'Content-Type': 'application/json'}; + +Future sendWH(Map content, String token, String id) { + final url = Uri.parse("$apiURL/webhooks/$id/$token"); + return http.post(url, body: json.encode(content), headers: headers); +} + +Future editWH(Map content, String token, String id, String mid) { + final url = Uri.parse("$apiURL/webhooks/$id/$token/messages/$mid"); + return http.patch(url, body: json.encode(content), headers: headers); +} + +Future getWH(String token, String id, String mid) { + final url = Uri.parse("$apiURL/webhooks/$id/$token/messages/$mid"); + return http.get(url); +} + +Future deleteWH(String token, String id, String mid) { + final url = Uri.parse("$apiURL/webhooks/$id/$token/messages/$mid"); + return http.delete(url); +} diff --git a/lib/src/util.dart b/lib/src/util.dart index 927baaa..fd2b78a 100644 --- a/lib/src/util.dart +++ b/lib/src/util.dart @@ -1,3 +1,5 @@ +import 'package:tn_discord/src/types.dart'; + class Utils { static Map parseWebhookURL(String url) { final RegExp regex = RegExp( @@ -18,4 +20,13 @@ class Utils { "token": token, }; } + + Map createMessage({String? text, List? embeds}) { + Map message = {}; + + if (text != null && text != "") message["content"] = text; + if (embeds != null && embeds.isNotEmpty) message["embeds"] = embeds; + + return message; + } } diff --git a/pubspec.yaml b/pubspec.yaml index 3a6e2ec..039246c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: tn_discord description: A powerful Dart library for interacting with the Discord API. -version: 1.0.0 +version: 1.1.0 repository: https://github.com/ThunderNetworkRaD/discord.dart