This commit is contained in:
killerboss 2023-07-31 13:24:04 +02:00
parent 26c2288b28
commit 0fba5a8276
8 changed files with 86 additions and 40 deletions

3
.gitignore vendored
View file

@ -5,3 +5,6 @@
# Avoid committing pubspec.lock for library packages; see # Avoid committing pubspec.lock for library packages; see
# https://dart.dev/guides/libraries/private-files#pubspeclock. # https://dart.dev/guides/libraries/private-files#pubspeclock.
pubspec.lock pubspec.lock
# Testing
bin/

View file

@ -1,3 +1,11 @@
## 1.1.0
- Changed `webhookclient.send() -> webhookclient.sendText()`
- Add `webhookclient.send(String content, List<Map<String, dynamic>> embeds)`
- Add `webhookclient.edit(String message-id, {String content, List<Map<String, dynamic>> embeds})`
- Add `webhookclient.editText(String message-id, String content)`
- Add `webhookclient.get(String message-id)`
- Add `webhookclient.delete(String message-id)`
## 1.0.0 ## 1.0.0
- Initial version. - Initial version.

View file

@ -1,12 +1,14 @@
# discord.dart # 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. This package is work in progress.
## Webhooks ## Webhooks
### Initializations
```dart ```dart
import 'package:tn_discord/tn_discord.dart'; import 'package:tn_discord/tn_discord.dart';
main() { main() {
const webhook = WebookClient(options); const webhook = WebookClient(/*Webhook Options*/);
webhook.send(String message);
} }
``` ```
### Webhook Options ### Webhook Options

View file

@ -1,6 +1,7 @@
import 'package:http/http.dart'; import 'package:http/http.dart';
void handleCode(int code, Response fullResponse) { void handleCode(Response res) async {
int code = res.statusCode;
switch (code) { switch (code) {
default: default:
break; break;

View file

@ -1,3 +1,5 @@
import "dart:convert";
import "package:events_emitter/events_emitter.dart"; import "package:events_emitter/events_emitter.dart";
import "package:http/http.dart"; import "package:http/http.dart";
import "package:tn_discord/src/error_handler.dart"; import "package:tn_discord/src/error_handler.dart";
@ -27,39 +29,50 @@ class WebhookClient {
} }
} }
void sendText(String content) async { Future<Map<String, dynamic>> sendText(String content) async {
Map<String, String> body = {"content": content}; Map<String, String> body = {"content": content};
Response res = await sendWH(body, token, id); Response res = await sendWH(body, token, id);
handleCode(res.statusCode, res); handleCode(res);
return json.decode(res.body);
} }
void send({String content = "", List<Embed> embeds = const []}) async { Future<Map<String, dynamic>> send({String? content, List<Embed>? embeds}) async {
Map body = {"content": content, "embeds": embeds}; Map<String, dynamic> body = Utils().createMessage(text: content, embeds: embeds);
Response res = await sendWH(body, token, id); 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<Map<String, dynamic>> editText(String id, String content) async {
Map<String, String> body = {"content": content}; Map<String, String> body = {"content": content};
Response res = await editWH(body, token, this.id, id); 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<Embed> embeds = const []}) async { Future<Map<String, dynamic>> edit(String id, {String content = "", List<Embed> embeds = const []}) async {
Map body = {"content": content, "embeds": embeds}; Map body = {"content": content, "embeds": embeds};
Response res = await editWH(body, token, this.id, id); Response res = await editWH(body, token, this.id, id);
handleCode(res.statusCode, res); handleCode(res);
return json.decode(res.body);
} }
Future<void> get(String id) async { Future<Map<String, dynamic>> get(String id) async {
Response res = await getWH(token, this.id, id); Response res = await getWH(token, this.id, id);
handleCode(res);
handleCode(res.statusCode, res); return json.decode(res.body);
}
Future<Map<String, dynamic>> delete(String id) async {
Response res = await deleteWH(token, this.id, id);
handleCode(res);
return json.decode(res.body);
} }
} }

View file

@ -1,17 +1,25 @@
import 'package:tn_discord/src/index.dart'; import 'package:tn_discord/src/index.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'dart:convert';
final headers = {'Content-Type': 'application/json'};
Future<http.Response> sendWH(Map content, String token, String id) { Future<http.Response> sendWH(Map content, String token, String id) {
final url = Uri.parse("$apiURL/webhooks/$id/$token"); final url = Uri.parse("$apiURL/webhooks/$id/$token");
return http.post(url, body: content); return http.post(url, body: json.encode(content), headers: headers);
} }
Future<http.Response> editWH(Map content, String token, String id, String mid) { Future<http.Response> editWH(Map content, String token, String id, String mid) {
final url = Uri.parse("$apiURL/webhooks/$id/$token/messages/$mid"); final url = Uri.parse("$apiURL/webhooks/$id/$token/messages/$mid");
return http.patch(url, body: content); return http.patch(url, body: json.encode(content), headers: headers);
} }
Future<http.Response> getWH(String token, String id, String mid) { Future<http.Response> getWH(String token, String id, String mid) {
final url = Uri.parse("$apiURL/webhooks/$id/$token/messages/$mid"); final url = Uri.parse("$apiURL/webhooks/$id/$token/messages/$mid");
return http.get(url); return http.get(url);
} }
Future<http.Response> deleteWH(String token, String id, String mid) {
final url = Uri.parse("$apiURL/webhooks/$id/$token/messages/$mid");
return http.delete(url);
}

View file

@ -1,3 +1,5 @@
import 'package:tn_discord/src/types.dart';
class Utils { class Utils {
static Map<String, String> parseWebhookURL(String url) { static Map<String, String> parseWebhookURL(String url) {
final RegExp regex = RegExp( final RegExp regex = RegExp(
@ -18,4 +20,13 @@ class Utils {
"token": token, "token": token,
}; };
} }
Map<String, dynamic> createMessage({String? text, List<Embed>? embeds}) {
Map<String, dynamic> message = {};
if (text != null && text != "") message["content"] = text;
if (embeds != null && embeds.isNotEmpty) message["embeds"] = embeds;
return message;
}
} }

View file

@ -1,6 +1,6 @@
name: tn_discord name: tn_discord
description: A powerful Dart library for interacting with the Discord API. 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 repository: https://github.com/ThunderNetworkRaD/discord.dart