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
# https://dart.dev/guides/libraries/private-files#pubspeclock.
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
- Initial version.

View file

@ -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

View file

@ -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;
}

View file

@ -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<Map<String, dynamic>> sendText(String content) async {
Map<String, String> 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<Embed> embeds = const []}) async {
Map body = {"content": content, "embeds": embeds};
Future<Map<String, dynamic>> send({String? content, List<Embed>? embeds}) async {
Map<String, dynamic> 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<Map<String, dynamic>> editText(String id, String content) async {
Map<String, String> 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<Embed> embeds = const []}) async {
Future<Map<String, dynamic>> edit(String id, {String content = "", List<Embed> 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<void> get(String id) async {
Future<Map<String, dynamic>> get(String id) async {
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:http/http.dart' as http;
Future<http.Response> sendWH(Map content, String token, String id) {
final url = Uri.parse("$apiURL/webhooks/$id/$token");
return http.post(url, body: content);
}
Future<http.Response> 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<http.Response> 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<http.Response> 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<http.Response> 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<http.Response> getWH(String token, String id, String mid) {
final url = Uri.parse("$apiURL/webhooks/$id/$token/messages/$mid");
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 {
static Map<String, String> parseWebhookURL(String url) {
final RegExp regex = RegExp(
@ -18,4 +20,13 @@ class Utils {
"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
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