Adding *managers
Co-authored-by: GabrieleYT9768 <GabrieleYT9768@users.noreply.github.com>
This commit is contained in:
parent
1214feb42a
commit
64ebee326b
5 changed files with 223 additions and 31 deletions
0
lib/src/errors.dart
Normal file
0
lib/src/errors.dart
Normal file
|
@ -1,29 +1,38 @@
|
|||
import "dart:async";
|
||||
import "dart:io";
|
||||
import "package:tn_discord/src/requests.dart";
|
||||
import "package:tn_discord/src/types.dart";
|
||||
|
||||
import "requests.dart";
|
||||
|
||||
import "types.dart";
|
||||
import "dart:convert";
|
||||
import "package:events_emitter/events_emitter.dart";
|
||||
|
||||
final version = "10";
|
||||
final apiURL = "https://discord.com/api/v$version";
|
||||
|
||||
int calculateIntents(List<int> intents) {
|
||||
int intentsNumber = 0;
|
||||
|
||||
for (var element in intents) {
|
||||
intentsNumber += element;
|
||||
}
|
||||
|
||||
return intentsNumber;
|
||||
}
|
||||
|
||||
class Client extends EventEmitter {
|
||||
String? token;
|
||||
List<GatewayIntentBits> intents = [];
|
||||
bool logged = false;
|
||||
int intents;
|
||||
dynamic ws;
|
||||
String resume_gateway_url = "";
|
||||
String session_id = "";
|
||||
String resumeGatewayURL = "";
|
||||
String sessionID = "";
|
||||
|
||||
Client({List<GatewayIntentBits> intents = const []});
|
||||
Client({this.intents = 0});
|
||||
|
||||
login(String token) async {
|
||||
final websocket = await requestWebSocketURL();
|
||||
|
||||
this.token = token;
|
||||
logged = true;
|
||||
|
||||
ws = await WebSocket.connect(websocket);
|
||||
|
||||
|
@ -31,30 +40,30 @@ class Client extends EventEmitter {
|
|||
"op": 2,
|
||||
'd': {
|
||||
"token": token,
|
||||
"intents": 32767,
|
||||
"intents": intents,
|
||||
"properties": {
|
||||
"os": "linux",
|
||||
"browser": "chrome",
|
||||
"device": "chrome",
|
||||
"browser": "tn_discord",
|
||||
"device": "tn_discord",
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
ws.add(json.encode(payload));
|
||||
|
||||
reconnect() {
|
||||
reconnect() async {
|
||||
payload = {
|
||||
"op": 6,
|
||||
"d": {"token": token, "session_id": session_id, "seq": 1337}
|
||||
"d": {"token": token, "session_id": sessionID, "seq": 1337}
|
||||
};
|
||||
|
||||
ws = WebSocket.connect(resume_gateway_url);
|
||||
ws = await WebSocket.connect(resumeGatewayURL);
|
||||
ws.add(json.encode(payload));
|
||||
}
|
||||
|
||||
sendHeartBeat() {
|
||||
payload = {"op": 1, "d": null};
|
||||
ws.add(json.encode(payload));
|
||||
print("Sent Heartbeat");
|
||||
}
|
||||
|
||||
heartbeatsender(int ms) {
|
||||
|
@ -64,11 +73,13 @@ class Client extends EventEmitter {
|
|||
});
|
||||
}
|
||||
|
||||
Sender sender = Sender(token);
|
||||
var i = await sender.getServer();
|
||||
GuildManager(i);
|
||||
|
||||
ws.listen((event) {
|
||||
event = json.decode(event);
|
||||
|
||||
print(event);
|
||||
|
||||
switch (event["op"]) {
|
||||
case 1:
|
||||
sendHeartBeat();
|
||||
|
@ -78,6 +89,23 @@ class Client extends EventEmitter {
|
|||
break;
|
||||
case 7:
|
||||
case 9:
|
||||
reconnect();
|
||||
break;
|
||||
}
|
||||
|
||||
var eventName = event["t"];
|
||||
|
||||
switch (eventName) {
|
||||
case "READY":
|
||||
resumeGatewayURL = event["d"]["resume_gateway_url"];
|
||||
sessionID = event["d"]["session_id"];
|
||||
emit("READY", event["d"]);
|
||||
break;
|
||||
case "GUILD_CREATE":
|
||||
break;
|
||||
}
|
||||
}, onDone: () {
|
||||
switch (ws.closeCode) {
|
||||
case 4000:
|
||||
case 4001:
|
||||
case 4002:
|
||||
|
@ -89,7 +117,7 @@ class Client extends EventEmitter {
|
|||
reconnect();
|
||||
break;
|
||||
case 4004:
|
||||
throw Exception("[4014] Disallowed Intents");
|
||||
throw Exception("[4004] Disallowed Intents");
|
||||
case 4010:
|
||||
throw Exception("[4010] Invalid Shard");
|
||||
case 4011:
|
||||
|
@ -101,13 +129,6 @@ class Client extends EventEmitter {
|
|||
case 4014:
|
||||
throw Exception("[4014] Disallowed Intents");
|
||||
}
|
||||
|
||||
var eventName = event["t"];
|
||||
|
||||
switch (eventName) {
|
||||
case "READY":
|
||||
emit("READY", event);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,4 +43,12 @@ class Sender {
|
|||
"Authorization": "Bot $token"
|
||||
};
|
||||
}
|
||||
|
||||
Future getServer() async {
|
||||
final url = Uri.parse("$apiURL/users/@me/guilds");
|
||||
dynamic res = await http.get(url, headers: headers);
|
||||
res = json.decode(res.body);
|
||||
return res;
|
||||
|
||||
}
|
||||
}
|
|
@ -1,4 +1,10 @@
|
|||
// ignore_for_file: non_constant_identifier_names, constant_identifier_names
|
||||
// ignore_for_file: constant_identifier_names
|
||||
|
||||
import "dart:convert";
|
||||
|
||||
import "package:http/http.dart" as http;
|
||||
|
||||
import "main.dart";
|
||||
|
||||
class GatewayIntentBits {
|
||||
static const Guilds = 1;
|
||||
|
@ -22,5 +28,164 @@ class GatewayIntentBits {
|
|||
static const AutoModerationExecution = 2097152;
|
||||
}
|
||||
|
||||
class Collection {
|
||||
final Map<String, dynamic> _variables = {};
|
||||
Collection();
|
||||
|
||||
dynamic set(String key, dynamic value) {
|
||||
_variables[key] = value;
|
||||
return value;
|
||||
}
|
||||
|
||||
dynamic get(String key) {
|
||||
return _variables[key];
|
||||
}
|
||||
|
||||
void remove(String key) {
|
||||
_variables.remove(key);
|
||||
}
|
||||
|
||||
dynamic add(String key, num value) {
|
||||
_variables[key] += value;
|
||||
return value + _variables[key];
|
||||
}
|
||||
|
||||
dynamic subtract(String key, num value) {
|
||||
_variables[key] -= value;
|
||||
return value - _variables[key];
|
||||
}
|
||||
}
|
||||
|
||||
class Guild {
|
||||
String id = '';
|
||||
String name = '';
|
||||
String owner = '';
|
||||
String description = '';
|
||||
|
||||
Guild(Map data) {
|
||||
id = data["id"];
|
||||
name = data["name"];
|
||||
description = data["description"];
|
||||
owner = data["owner_id"];
|
||||
}
|
||||
}
|
||||
|
||||
class GuildManager {
|
||||
final Collection cache = Collection();
|
||||
|
||||
GuildManager(List<Guild> guilds) {
|
||||
for (var guild in guilds) {
|
||||
cache.set(guild.id, guild);
|
||||
}
|
||||
}
|
||||
|
||||
Future<Guild> fetch(String id) async {
|
||||
var res = await http.get(Uri.parse("$apiURL/guilds/$id"));
|
||||
if (res.statusCode != 200) {
|
||||
throw Exception("Error ${res.statusCode} receiving the guild");
|
||||
}
|
||||
final guild = Guild(json.decode(res.body));
|
||||
cache.set(guild.id, guild);
|
||||
return guild;
|
||||
}
|
||||
}
|
||||
|
||||
class Channel {
|
||||
String id = '';
|
||||
String name = '';
|
||||
|
||||
Channel(Map data) {
|
||||
id = data["id"];
|
||||
name = data["name"];
|
||||
}
|
||||
}
|
||||
|
||||
class ChannelManager {
|
||||
final Collection cache = Collection();
|
||||
|
||||
ChannelManager(List<Channel> channels) {
|
||||
for (var channel in channels) {
|
||||
cache.set(channel.id, channel);
|
||||
}
|
||||
}
|
||||
|
||||
Future<Channel> fetch(String id) async {
|
||||
var res = await http.get(Uri.parse("$apiURL/channels/$id"));
|
||||
if (res.statusCode != 200) {
|
||||
throw Exception("Error ${res.statusCode} receiving the channel");
|
||||
}
|
||||
dynamic channel = Channel(json.decode(res.body));
|
||||
cache.set(channel.id, channel);
|
||||
return channel;
|
||||
}
|
||||
}
|
||||
|
||||
class Role {
|
||||
//
|
||||
}
|
||||
|
||||
class RoleManager {
|
||||
//
|
||||
}
|
||||
|
||||
class Member {
|
||||
User? user;
|
||||
RoleManager? roles;
|
||||
String joinedAt = '';
|
||||
int flags = 0;
|
||||
|
||||
|
||||
Member(Map data) {
|
||||
user = User(data["user"]);
|
||||
joinedAt = data["joined_at"];
|
||||
int flags = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class MemberManager {
|
||||
//
|
||||
}
|
||||
|
||||
class User {
|
||||
String id = '';
|
||||
bool bot = false;
|
||||
String username = '';
|
||||
String globalName = '';
|
||||
String displayName = '';
|
||||
int discriminator = 0;
|
||||
String avatar = '';
|
||||
|
||||
User(Map data) {
|
||||
id = data["id"];
|
||||
bot = data["bot"];
|
||||
username = data["username"];
|
||||
globalName = data["global_name"];
|
||||
displayName = data["display_name"];
|
||||
discriminator = data["discriminator"];
|
||||
avatar = "https://cdn.discordapp.com/avatars/$id/${data["avatar"]}.webp";
|
||||
}
|
||||
}
|
||||
|
||||
class UserManager {
|
||||
final Collection cache = Collection();
|
||||
|
||||
UserManager(List<User> users) {
|
||||
for (var user in users) {
|
||||
cache.set(user.id, user);
|
||||
}
|
||||
}
|
||||
|
||||
Future<User> fetch(String id) async {
|
||||
var res = await http.get(Uri.parse("$apiURL/users/$id"));
|
||||
if (res.statusCode != 200) {
|
||||
throw Exception("Error ${res.statusCode} receiving the user");
|
||||
}
|
||||
dynamic user = User(json.decode(res.body));
|
||||
cache.set(user.id, user);
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
typedef Embed = Map<String, String>;
|
||||
typedef Message = Map<String, String>;
|
|
@ -29,8 +29,7 @@ class WebhookClient {
|
|||
return json.decode(res.body);
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> send(
|
||||
{String? content, List<Embed>? embeds}) async {
|
||||
Future<Map<String, dynamic>> send({String? content, List<Embed>? embeds}) async {
|
||||
Map<String, dynamic> body =
|
||||
Utils().createMessage(text: content, embeds: embeds);
|
||||
|
||||
|
@ -48,8 +47,7 @@ class WebhookClient {
|
|||
return json.decode(res.body);
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> 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);
|
||||
|
||||
|
|
Reference in a new issue