Compare commits

..

No commits in common. "main" and "v1.2.0" have entirely different histories.
main ... v1.2.0

17 changed files with 86 additions and 204 deletions

View file

@ -1,13 +1,3 @@
# 1.3.0
- Renamed `calculateIntents` to `intentsCalculator`
- Add set slashcommands
- Add GuildDiscoverySplash
- Adding Docs in Guild Class
- Fix `guild_icon`
- Fix `guild_splash`
- Removed `base_image`
- Removed `notUpdatedGuild` from UnavailableGuild
# 1.2.0 # 1.2.0
- Add `client` - Add `client`
- Add Event Emitter/Listener (`client.on`) - Add Event Emitter/Listener (`client.on`)

View file

@ -2,18 +2,8 @@
[![Pub](https://img.shields.io/pub/v/tn_discord?color=red&logo=dart)](https://github.com/ThunderNetworkRaD/discord-dart) [![Pub](https://img.shields.io/pub/v/tn_discord?color=red&logo=dart)](https://github.com/ThunderNetworkRaD/discord-dart)
⚠️ We don't have tested on Flutter & on Web. ⚠️ We don't have tested on Flutter & on Web.
⚠️ This package is work in progress. ⚠️ This package is work in progress.
## Package Versions
How we use package versions?
a.b.c-d
- a: Big Relase, Rewrites, etc
- b: Small Relase, Small Features, like add a class
- c: Bug Fixes, Template Fixes, Documentation Updates
- d: Beta or Relase Number
## Credits ## Credits
We took inspiration from [discord.js](https://github.com/discordjs/discord.js) and [Grapes-discord.grapes](https://github.com/BlackdestinyXX/Grapes-discord.grapes). We took inspiration from [discord.js](https://github.com/discordjs/discord.js) and [Grapes-discord.grapes](https://github.com/BlackdestinyXX/Grapes-discord.grapes).

View file

@ -1,3 +1,6 @@
import 'package:tn_discord/src/classes/events.dart';
import 'package:tn_discord/src/classes/message/message.dart';
import 'package:tn_discord/src/classes/message/message_sent.dart';
import 'package:tn_discord/tn_discord.dart'; import 'package:tn_discord/tn_discord.dart';
main() async { main() async {
@ -10,13 +13,12 @@ main() async {
]) ])
); );
client.login("ODkwMz1g"); client.login("Your Bot Token");
client.on("READY", (data) async { client.on("READY", (data) async {
// Let we get a guild name // Let we get a guild name
var a = await client.guilds.fetch("913388008307302410"); var a = await client.guilds.fetch("a guild id");
print(a.name); print(a.name);
client.commands.create(Command(name: "test", description: "test1"));
}); });
client.on(Events.MessageCreate, (MessageSent message) async { client.on(Events.MessageCreate, (MessageSent message) async {

View file

@ -13,7 +13,7 @@ class Channel {
} }
Future<MessageSent> send(Message message) async { Future<MessageSent> send(Message message) async {
var res = await _sender.sendMessage(message, id); var res = await _sender.send(message, id);
return MessageSent(message, res["author"]["id"], id, res["id"]); return MessageSent(message, res["author"]["id"], id, res["id"]);
} }
} }

View file

@ -1,15 +0,0 @@
class Command {
late String name;
late String description;
late int type;
Command({ required this.name, required this.description, this.type = 1 });
exportable () {
return {
"type": type,
"name": name,
"description": description
};
}
}

View file

@ -1,16 +0,0 @@
import 'command.dart';
import '../../requests.dart';
class CommandManager {
final Sender _sender;
CommandManager(this._sender);
create(Command cmd, { String? guildID }) async {
if (guildID != null) {
await _sender.createGuildCommands(guildID, cmd);
} else {
await _sender.createGlobalCommands(cmd);
}
}
}

View file

@ -1,4 +1,3 @@
import '../images/guild_discovery_splash.dart';
import '../images/guild_icon.dart'; import '../images/guild_icon.dart';
import '../../requests.dart'; import '../../requests.dart';
@ -18,28 +17,23 @@ class Guild {
MemberManager? members; MemberManager? members;
RoleManager? roles; RoleManager? roles;
/// The guild's id // Param guild create | fetch | fetch-servers
/// The guild's id <br>
/// GuildCreate, GuildUpdate, GuildDelete, FetchOneGuild, FetchAllGuild
late String id; late String id;
/// Whether the guild is available to access. If it is not available, it indicates a server outage /// Whether the guild is available to access. If it is not available, it indicates a server outage
bool unavailable = false; /// GuildCreate, GuildDelete
/// The name of this guild /// FetchOneGuild, FetchAllGuild, GuildUpdate
late String name; late bool unavailable;
/// The icon hash of this guild late String name; // x | x | x
late String? iconHash; late GuildIcon? icon; // x | x | x
/// The icon of this guild late String? iconHash; // x | x |
late GuildIcon? icon; String? splashHash; // x | x |
/// The hash of the guild invite splash image GuildSplash? splash;
late String? splashHash; String? discoverySplash; // x | x |
/// The guild invite splash image of this guild bool? appIsOwner; // | | x
late GuildSplash? splash; Future<Member>? owner; // | |
/// The hash of the guild discovery splash image String? ownerId; // x | x |
late String? discoverySplashHash;
/// The guild discovery splash image of this guild
late GuildDiscoverySplash? discoverySplash;
/// The owner of this guild
late Future<Member>? owner;
/// The owner id of this guild
String? ownerId;
String? permissions; // | | x String? permissions; // | | x
String? afkChannelId; // x | x | String? afkChannelId; // x | x |
int? afkTimeout; // x | x | int? afkTimeout; // x | x |
@ -72,7 +66,6 @@ class Guild {
String? joinedAt; // x | | String? joinedAt; // x | |
bool? large; // x | | bool? large; // x | |
int? memberCount; // x | | int? memberCount; // x | |
// Param guild create | fetch | fetch-servers
Guild(this._sender, Map data) { Guild(this._sender, Map data) {
id = data["id"]; id = data["id"];
@ -100,32 +93,27 @@ class Guild {
roles = RoleManager([]); roles = RoleManager([]);
} }
unavailable = false;
name = data["name"]; name = data["name"];
if (data["icon"] != null) {
iconHash = data["icon_hash"]; icon = GuildIcon(data["icon"], id);
if (iconHash != null) {
icon = GuildIcon(iconHash!, id);
} else { } else {
icon = null; icon = null;
} }
iconHash = data["icon_hash"];
splashHash = data["splash"]; splashHash = data["splash"];
if (splashHash != null) { if (splashHash != null) {
splash = GuildSplash(splashHash!, id); splash = GuildSplash(splashHash.toString(), id);
} else {
splash = null;
} }
if (data["discovery_splash"] != null) {
discoverySplashHash = data["discovery_splash"]; discoverySplash = data["discovery_splash"];
if (discoverySplashHash != null) {
discoverySplash = GuildDiscoverySplash(discoverySplashHash!, id);
} else { } else {
discoverySplash = null; discoverySplash = null;
} }
appIsOwner = data["owner"];
ownerId = data["owner_id"]; ownerId = data["owner_id"];
if (ownerId != null) { if (ownerId != null) {
owner = members!.fetch(ownerId.toString()); owner = members?.fetch(ownerId.toString());
} else { } else {
owner = null; owner = null;
} }

View file

@ -1,9 +1,11 @@
/// A guild involved in a server outage. import "guild.dart";
class UnavailableGuild {
/// The guild's id
late String id;
/// Whether the guild is available to access. If it is not available, it indicates a server outage
bool unavailable = true;
UnavailableGuild(this.id); class UnavailableGuild {
late String id;
late bool unavailable;
late Guild? notUpdatedGuild;
UnavailableGuild(this.id, { this.notUpdatedGuild }) {
unavailable = true;
}
} }

View file

@ -0,0 +1,4 @@
class BaseImage {
late String hash;
BaseImage(this.hash);
}

View file

@ -1,10 +0,0 @@
class GuildDiscoverySplash {
late String id;
late String hash;
GuildDiscoverySplash(this.hash, this.id);
String url({String? extension = "jpeg"}) {
return "https://cdn.discordapp.com/discovery-splashes/$id/$hash.$extension";
}
}

View file

@ -1,8 +1,11 @@
class GuildIcon { import 'base_image.dart';
late String id;
late String hash;
GuildIcon(this.hash, this.id); class GuildIcon extends BaseImage {
late String id;
GuildIcon(String hash, this.id) : super(hash) {
this.hash = hash;
}
String url({String? extension = "jpeg"}) { String url({String? extension = "jpeg"}) {
return "https://cdn.discordapp.com/icons/$id/$hash.$extension"; return "https://cdn.discordapp.com/icons/$id/$hash.$extension";

View file

@ -1,8 +1,11 @@
class GuildSplash { import 'base_image.dart';
late String id;
late String hash;
GuildSplash(this.hash, this.id); class GuildSplash extends BaseImage {
late String id;
GuildSplash(String hash, this.id) : super(hash) {
this.hash = hash;
}
String url({String? extension = "jpeg"}) { String url({String? extension = "jpeg"}) {
return "https://cdn.discordapp.com/splashes/$id/$hash.$extension"; return "https://cdn.discordapp.com/splashes/$id/$hash.$extension";

View file

@ -1,29 +0,0 @@
export "channel/channel.dart";
// // export "channel/channel_manager.dart";
export "commands/command.dart";
// // export "commands/command_manager.dart";
export "guild/guild.dart";
// // export "guild/guild_manager.dart";
export "guild/unavailable_guild.dart";
export "images/guild_icon.dart";
export "images/guild_splash.dart";
export "member/member.dart";
// // export "member/member_manager.dart";
export "message/embed.dart";
export "message/message.dart";
export "message/message_sent.dart";
export "role/role.dart";
// // export "role/role_manager.dart";
export "user/user.dart";
// // export "user/user_manager.dart";
export "events.dart";
export "gateway_intents_bits.dart";
export "interaction.dart";

View file

@ -4,6 +4,7 @@ import "package:http/http.dart" as http;
import '../../../tn_discord.dart'; import '../../../tn_discord.dart';
import '../../collection.dart'; import '../../collection.dart';
import './user.dart';
class UserManager { class UserManager {
final Collection cache = Collection(); final Collection cache = Collection();

View file

@ -3,7 +3,6 @@ import 'dart:io' if (dart.library.html) 'dart:html';
import "dart:convert"; import "dart:convert";
import "package:events_emitter/events_emitter.dart"; import "package:events_emitter/events_emitter.dart";
import "package:tn_discord/src/classes/channel/channel_manager.dart"; import "package:tn_discord/src/classes/channel/channel_manager.dart";
import "package:tn_discord/src/classes/commands/command_manager.dart";
import "classes/guild/guild.dart"; import "classes/guild/guild.dart";
import "classes/guild/guild_manager.dart"; import "classes/guild/guild_manager.dart";
@ -21,7 +20,7 @@ final apiURL = "https://discord.com/api/v$version";
/// This function calculate the intent number required from the gateway. /// This function calculate the intent number required from the gateway.
/// [intents] is a list of multiples of two. You can use GatewayIntentBits class. /// [intents] is a list of multiples of two. You can use GatewayIntentBits class.
/// Return a number. /// Return a number.
int intentsCalculator(List<int> intents) { int calculateIntents(List<int> intents) {
int intentsNumber = 0; int intentsNumber = 0;
for (var element in intents) { for (var element in intents) {
@ -43,8 +42,6 @@ class Client extends EventEmitter {
late ChannelManager channels; late ChannelManager channels;
bool ready = false; bool ready = false;
late User user; late User user;
late CommandManager commands;
/// Create a new Client. /// Create a new Client.
/// [intents] Intents to enable for this connection, it's a multiple of two. /// [intents] Intents to enable for this connection, it's a multiple of two.
@ -97,10 +94,23 @@ class Client extends EventEmitter {
}); });
} }
late Sender sender; Sender sender = Sender(token);
late int n; var i = await sender.fetchGuilds(withCounts: true);
ws.listen((event) async { List<Guild> gg = [];
for (dynamic g in i) {
gg.add(Guild(sender, g));
}
channels = ChannelManager(sender, [], main: true);
sender.channels = channels;
guilds = GuildManager(sender, gg);
int n = i.length;
ws.listen((event) {
event = json.decode(event); event = json.decode(event);
switch (event["op"]) { switch (event["op"]) {
@ -120,34 +130,17 @@ class Client extends EventEmitter {
switch (eventName) { switch (eventName) {
case "READY": case "READY":
sender = Sender(token, event["d"]["user"]["id"]);
var i = await sender.fetchGuilds(withCounts: true);
List<Guild> gg = [];
for (dynamic g in i) {
gg.add(Guild(sender, g));
}
channels = ChannelManager(sender, [], main: true);
sender.channels = channels;
guilds = GuildManager(sender, gg);
n = i.length;
commands["set"] = sender.setCommands;
resumeGatewayURL = event["d"]["resume_gateway_url"]; resumeGatewayURL = event["d"]["resume_gateway_url"];
sessionID = event["d"]["session_id"]; sessionID = event["d"]["session_id"];
user = User(event["d"]["user"]); user = User(event["d"]["user"]);
sender.setID(user.id);
ready = true; ready = true;
commands = CommandManager(sender);
break; break;
case "GUILD_CREATE": case "GUILD_CREATE":
if (guilds.cache.has(event["d"]["id"])) { if (guilds.cache.has(event["d"]["id"])) {
Guild oldGuild = guilds.cache.get(event["d"]["id"]); Guild oldGuild = guilds.cache.get(event["d"]["id"]);
if (oldGuild.appIsOwner != null) {
event['d']["owner"] = oldGuild.appIsOwner;
}
if (oldGuild.permissions != null) { if (oldGuild.permissions != null) {
event['d']["permissions"] = oldGuild.permissions; event['d']["permissions"] = oldGuild.permissions;
} }
@ -163,11 +156,13 @@ class Client extends EventEmitter {
} }
break; break;
case "GUILD_DELETE": case "GUILD_DELETE":
dynamic guild;
if (guilds.cache.has(event["d"]["id"])) { if (guilds.cache.has(event["d"]["id"])) {
guilds.cache.delete(event["d"]["id"]); guild = guilds.cache.get(event["d"]["id"]);
guilds.cache.set(event["d"]["id"], UnavailableGuild(event["d"]["id"],)); guilds.cache.set(event["d"]["id"], UnavailableGuild(event["d"]["id"], notUpdatedGuild: guild));
} else {
guild = event["d"];
} }
var guild = event["d"];
emit("GUILD_DELETE", guild); emit("GUILD_DELETE", guild);
break; break;
case "INTERACTION_CREATE": case "INTERACTION_CREATE":

View file

@ -1,5 +1,3 @@
import 'classes/commands/command.dart';
import 'classes/message/message.dart'; import 'classes/message/message.dart';
import 'main.dart'; import 'main.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
@ -38,22 +36,16 @@ Future<String> requestWebSocketURL() async {
class Sender { class Sender {
final String? _token; final String? _token;
final String id;
Map<String, String> headers = {}; Map<String, String> headers = {};
dynamic channels; dynamic channels;
String? id;
Sender(this._token, this.id) { Sender(this._token) {
headers = { headers = {
"Content-Type": "application/json", "Content-Type": "application/json",
"Authorization": "Bot $_token", "Authorization": "Bot $_token",
}; };
} }
setID(String id) {
this.id = id;
}
Future fetchGuilds({ bool withCounts = false }) async { Future fetchGuilds({ bool withCounts = false }) async {
final url = Uri.parse("$apiURL/users/@me/guilds?with_counts=$withCounts"); final url = Uri.parse("$apiURL/users/@me/guilds?with_counts=$withCounts");
dynamic res = await http.get(url, headers: headers); dynamic res = await http.get(url, headers: headers);
@ -61,7 +53,7 @@ class Sender {
return res; return res;
} }
Future sendMessage(Message msg, String cid) async { Future send(Message msg, String cid) async {
final url = Uri.parse("$apiURL/channels/$cid/messages"); final url = Uri.parse("$apiURL/channels/$cid/messages");
dynamic res = await http.post(url, headers: headers, body: json.encode(msg.exportable())); dynamic res = await http.post(url, headers: headers, body: json.encode(msg.exportable()));
res = json.decode(res.body); res = json.decode(res.body);
@ -94,24 +86,6 @@ class Sender {
res = json.decode(res.body); res = json.decode(res.body);
return res; return res;
} }
Future createGlobalCommands(Command body) async {
dynamic res = await http.post(Uri.parse("$apiURL/applications/$id/commands"), headers: headers, body: json.encode(body.exportable()));
if (res.statusCode != 201 && res.statusCode != 200) {
throw Exception("Error ${res.statusCode} setting the global command\nBody: ${json.decode(res.body)}");
}
res = json.decode(res.body);
return res;
}
Future createGuildCommands(String id, Command body) async {
dynamic res = await http.post(Uri.parse("$apiURL/applications/${this.id}/guilds/$id/commands"), headers: headers, body: json.encode(body.exportable()));
if (res.statusCode != 201 && res.statusCode != 200) {
throw Exception("Error ${res.statusCode} setting the guild command\nBody: ${json.decode(res.body)}");
}
res = json.decode(res.body);
return res;
}
} }
Future interactionReply(String id, String token, Map<String, dynamic> content) async { Future interactionReply(String id, String token, Map<String, dynamic> content) async {

View file

@ -2,4 +2,4 @@ library;
export "src/webhook.dart"; export "src/webhook.dart";
export "src/main.dart"; export "src/main.dart";
export "src/classes/index.dart"; export "src/classes/gateway_intents_bits.dart";