2023-08-06 20:34:01 +02:00
|
|
|
import 'classes/message/message.dart';
|
2023-08-02 12:48:55 +02:00
|
|
|
import 'main.dart';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
final headerswh = {
|
|
|
|
"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: headerswh);
|
|
|
|
}
|
|
|
|
|
|
|
|
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: headerswh);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<String> requestWebSocketURL() async {
|
|
|
|
final url = Uri.parse("$apiURL/gateway");
|
|
|
|
dynamic res = await http.get(url);
|
|
|
|
res = json.decode(res.body);
|
|
|
|
return res["url"];
|
|
|
|
}
|
|
|
|
|
|
|
|
class Sender {
|
2023-08-04 19:19:21 +02:00
|
|
|
final String? _token;
|
2023-08-02 12:48:55 +02:00
|
|
|
Map<String, String> headers = {};
|
|
|
|
|
2023-08-04 19:19:21 +02:00
|
|
|
Sender(this._token) {
|
2023-08-02 12:48:55 +02:00
|
|
|
headers = {
|
|
|
|
"Content-Type": "application/json",
|
2023-08-04 19:19:21 +02:00
|
|
|
"Authorization": "Bot $_token",
|
2023-08-02 12:48:55 +02:00
|
|
|
};
|
|
|
|
}
|
2023-08-02 17:44:35 +02:00
|
|
|
|
2023-08-07 12:34:29 +02:00
|
|
|
Future fetchGuilds({ bool withCounts = false }) async {
|
|
|
|
final url = Uri.parse("$apiURL/users/@me/guilds?with_counts=$withCounts");
|
2023-08-02 17:44:35 +02:00
|
|
|
dynamic res = await http.get(url, headers: headers);
|
|
|
|
res = json.decode(res.body);
|
|
|
|
return res;
|
|
|
|
}
|
2023-08-04 19:19:21 +02:00
|
|
|
|
|
|
|
Future send(Message msg, String cid) async {
|
|
|
|
final url = Uri.parse("$apiURL/channels/$cid/messages");
|
|
|
|
dynamic res = await http.post(url, headers: headers, body: json.encode(msg.exportable()));
|
|
|
|
res = json.decode(res.body);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2023-08-07 12:34:29 +02:00
|
|
|
Future fetchGuild(String id, { bool withCounts = false }) async {
|
|
|
|
dynamic res = await http.get(Uri.parse("$apiURL/guilds/$id?with_counts=$withCounts"), headers: headers);
|
2023-08-04 19:19:21 +02:00
|
|
|
if (res.statusCode != 200) {
|
|
|
|
throw Exception("Error ${res.statusCode} receiving the guild");
|
|
|
|
}
|
|
|
|
res = json.decode(res.body);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future fetchChannel(String id) async {
|
|
|
|
dynamic res = await http.get(Uri.parse("$apiURL/channels/$id"), headers: headers);
|
|
|
|
if (res.statusCode != 200) {
|
|
|
|
throw Exception("Error ${res.statusCode} receiving the channel");
|
|
|
|
}
|
|
|
|
res = json.decode(res.body);
|
|
|
|
return res;
|
|
|
|
}
|
2023-08-06 20:34:01 +02:00
|
|
|
|
|
|
|
Future fetchMember(String gid, String id) async {
|
|
|
|
dynamic res = await http.get(Uri.parse("$apiURL/guilds/$gid/members/$id"), headers: headers);
|
|
|
|
if (res.statusCode != 200) {
|
|
|
|
throw Exception("Error ${res.statusCode} receiving the member");
|
|
|
|
}
|
|
|
|
res = json.decode(res.body);
|
|
|
|
return res;
|
|
|
|
}
|
2023-08-01 21:20:43 +02:00
|
|
|
}
|