From 98a87005b048ed6df1f7b1b90e0d94c2f9b7d304 Mon Sep 17 00:00:00 2001 From: KillerBossOriginal Date: Sun, 13 Aug 2023 12:54:23 +0200 Subject: [PATCH 1/2] updating --- CHANGELOG.md | 9 +++ lib/src/classes/guild/guild.dart | 60 +++++++++++-------- lib/src/classes/guild/unavailable_guild.dart | 12 ++-- lib/src/classes/images/base_image.dart | 4 -- .../images/guild_discovery_splash.dart | 10 ++++ lib/src/classes/images/guild_icon.dart | 9 +-- lib/src/classes/images/guild_splash.dart | 9 +-- lib/src/main.dart | 13 ++-- 8 files changed, 70 insertions(+), 56 deletions(-) delete mode 100644 lib/src/classes/images/base_image.dart create mode 100644 lib/src/classes/images/guild_discovery_splash.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b7c523..c557534 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +# 1.3.0 +- Renamed `calculateIntents` to `intentsCalculator` +- Add Docs in Guild Class +- Add GuildDiscoverySplash +- Fix `guild_icon` +- Fix `guild_splash` +- Removed `base_image` +- Removed `notUpdatedGuild` from UnavailableGuild + # 1.2.0 - Add `client` - Add Event Emitter/Listener (`client.on`) diff --git a/lib/src/classes/guild/guild.dart b/lib/src/classes/guild/guild.dart index 94fabe1..1a73315 100644 --- a/lib/src/classes/guild/guild.dart +++ b/lib/src/classes/guild/guild.dart @@ -1,3 +1,4 @@ +import '../images/guild_discovery_splash.dart'; import '../images/guild_icon.dart'; import '../../requests.dart'; @@ -17,23 +18,28 @@ class Guild { MemberManager? members; RoleManager? roles; - // Param guild create | fetch | fetch-servers - /// The guild's id
- /// ✅ GuildCreate, GuildUpdate, GuildDelete, FetchOneGuild, FetchAllGuild + /// The guild's id late String id; /// Whether the guild is available to access. If it is not available, it indicates a server outage - /// ✅ GuildCreate, GuildDelete - /// ❎ FetchOneGuild, FetchAllGuild, GuildUpdate - late bool unavailable; - late String name; // x | x | x - late GuildIcon? icon; // x | x | x - late String? iconHash; // x | x | - String? splashHash; // x | x | - GuildSplash? splash; - String? discoverySplash; // x | x | - bool? appIsOwner; // | | x - Future? owner; // | | - String? ownerId; // x | x | + bool unavailable = false; + /// The name of this guild + late String name; + /// The icon hash of this guild + late String? iconHash; + /// The icon of this guild + late GuildIcon? icon; + /// The hash of the guild invite splash image + late String? splashHash; + /// The guild invite splash image of this guild + late GuildSplash? splash; + /// The hash of the guild discovery splash image + late String? discoverySplashHash; + /// The guild discovery splash image of this guild + late GuildDiscoverySplash? discoverySplash; + /// The owner of this guild + late Future? owner; + /// The owner id of this guild + String? ownerId; String? permissions; // | | x String? afkChannelId; // x | x | int? afkTimeout; // x | x | @@ -66,6 +72,7 @@ class Guild { String? joinedAt; // x | | bool? large; // x | | int? memberCount; // x | | + // Param guild create | fetch | fetch-servers Guild(this._sender, Map data) { id = data["id"]; @@ -93,27 +100,32 @@ class Guild { roles = RoleManager([]); } - unavailable = false; name = data["name"]; - if (data["icon"] != null) { - icon = GuildIcon(data["icon"], id); + + iconHash = data["icon_hash"]; + if (iconHash != null) { + icon = GuildIcon(iconHash!, id); } else { icon = null; } - iconHash = data["icon_hash"]; + splashHash = data["splash"]; if (splashHash != null) { - splash = GuildSplash(splashHash.toString(), id); + splash = GuildSplash(splashHash!, id); + } else { + splash = null; } - if (data["discovery_splash"] != null) { - discoverySplash = data["discovery_splash"]; + + discoverySplashHash = data["discovery_splash"]; + if (discoverySplashHash != null) { + discoverySplash = GuildDiscoverySplash(discoverySplashHash!, id); } else { discoverySplash = null; } - appIsOwner = data["owner"]; + ownerId = data["owner_id"]; if (ownerId != null) { - owner = members?.fetch(ownerId.toString()); + owner = members!.fetch(ownerId.toString()); } else { owner = null; } diff --git a/lib/src/classes/guild/unavailable_guild.dart b/lib/src/classes/guild/unavailable_guild.dart index e977dcf..3bbfd09 100644 --- a/lib/src/classes/guild/unavailable_guild.dart +++ b/lib/src/classes/guild/unavailable_guild.dart @@ -1,11 +1,9 @@ -import "guild.dart"; - +/// A guild involved in a server outage. class UnavailableGuild { + /// The guild's id late String id; - late bool unavailable; - late Guild? notUpdatedGuild; + /// Whether the guild is available to access. If it is not available, it indicates a server outage + bool unavailable = true; - UnavailableGuild(this.id, { this.notUpdatedGuild }) { - unavailable = true; - } + UnavailableGuild(this.id); } \ No newline at end of file diff --git a/lib/src/classes/images/base_image.dart b/lib/src/classes/images/base_image.dart deleted file mode 100644 index bf0a9e1..0000000 --- a/lib/src/classes/images/base_image.dart +++ /dev/null @@ -1,4 +0,0 @@ -class BaseImage { - late String hash; - BaseImage(this.hash); -} \ No newline at end of file diff --git a/lib/src/classes/images/guild_discovery_splash.dart b/lib/src/classes/images/guild_discovery_splash.dart new file mode 100644 index 0000000..1c1171a --- /dev/null +++ b/lib/src/classes/images/guild_discovery_splash.dart @@ -0,0 +1,10 @@ +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"; + } +} \ No newline at end of file diff --git a/lib/src/classes/images/guild_icon.dart b/lib/src/classes/images/guild_icon.dart index ab2c941..f2128ba 100644 --- a/lib/src/classes/images/guild_icon.dart +++ b/lib/src/classes/images/guild_icon.dart @@ -1,11 +1,8 @@ -import 'base_image.dart'; - -class GuildIcon extends BaseImage { +class GuildIcon { late String id; + late String hash; - GuildIcon(String hash, this.id) : super(hash) { - this.hash = hash; - } + GuildIcon(this.hash, this.id); String url({String? extension = "jpeg"}) { return "https://cdn.discordapp.com/icons/$id/$hash.$extension"; diff --git a/lib/src/classes/images/guild_splash.dart b/lib/src/classes/images/guild_splash.dart index eb82b15..8932f6f 100644 --- a/lib/src/classes/images/guild_splash.dart +++ b/lib/src/classes/images/guild_splash.dart @@ -1,11 +1,8 @@ -import 'base_image.dart'; - -class GuildSplash extends BaseImage { +class GuildSplash { late String id; + late String hash; - GuildSplash(String hash, this.id) : super(hash) { - this.hash = hash; - } + GuildSplash(this.hash, this.id); String url({String? extension = "jpeg"}) { return "https://cdn.discordapp.com/splashes/$id/$hash.$extension"; diff --git a/lib/src/main.dart b/lib/src/main.dart index 9c5f6f8..db31a76 100644 --- a/lib/src/main.dart +++ b/lib/src/main.dart @@ -20,7 +20,7 @@ final apiURL = "https://discord.com/api/v$version"; /// This function calculate the intent number required from the gateway. /// [intents] is a list of multiples of two. You can use GatewayIntentBits class. /// Return a number. -int calculateIntents(List intents) { +int intentsCalculator(List intents) { int intentsNumber = 0; for (var element in intents) { @@ -138,9 +138,6 @@ class Client extends EventEmitter { case "GUILD_CREATE": if (guilds.cache.has(event["d"]["id"])) { Guild oldGuild = guilds.cache.get(event["d"]["id"]); - if (oldGuild.appIsOwner != null) { - event['d']["owner"] = oldGuild.appIsOwner; - } if (oldGuild.permissions != null) { event['d']["permissions"] = oldGuild.permissions; } @@ -156,13 +153,11 @@ class Client extends EventEmitter { } break; case "GUILD_DELETE": - dynamic guild; if (guilds.cache.has(event["d"]["id"])) { - guild = guilds.cache.get(event["d"]["id"]); - guilds.cache.set(event["d"]["id"], UnavailableGuild(event["d"]["id"], notUpdatedGuild: guild)); - } else { - guild = event["d"]; + guilds.cache.delete(event["d"]["id"]); + guilds.cache.set(event["d"]["id"], UnavailableGuild(event["d"]["id"],)); } + var guild = event["d"]; emit("GUILD_DELETE", guild); break; case "INTERACTION_CREATE": From 16eaf7dfc57e0c5d6cfc6ad50b74d6b8345f5b87 Mon Sep 17 00:00:00 2001 From: KillerBossOriginal Date: Sun, 13 Aug 2023 13:56:37 +0200 Subject: [PATCH 2/2] Add set commands --- CHANGELOG.md | 3 ++- lib/src/main.dart | 38 ++++++++++++++++++++++---------------- lib/src/requests.dart | 12 +++++++++++- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c557534..6866a2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ # 1.3.0 - Renamed `calculateIntents` to `intentsCalculator` -- Add Docs in Guild Class +- Add set slashcommands - Add GuildDiscoverySplash +- Adding Docs in Guild Class - Fix `guild_icon` - Fix `guild_splash` - Removed `base_image` diff --git a/lib/src/main.dart b/lib/src/main.dart index db31a76..a817acf 100644 --- a/lib/src/main.dart +++ b/lib/src/main.dart @@ -42,6 +42,7 @@ class Client extends EventEmitter { late ChannelManager channels; bool ready = false; late User user; + var commands = {}; /// Create a new Client. /// [intents] Intents to enable for this connection, it's a multiple of two. @@ -94,23 +95,10 @@ class Client extends EventEmitter { }); } - Sender sender = Sender(token); - var i = await sender.fetchGuilds(withCounts: true); + late Sender sender; + late int n; - List 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) { + ws.listen((event) async { event = json.decode(event); switch (event["op"]) { @@ -130,6 +118,24 @@ class Client extends EventEmitter { switch (eventName) { case "READY": + sender = Sender(token, event["d"]["user"]["id"]); + var i = await sender.fetchGuilds(withCounts: true); + + List 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"]; sessionID = event["d"]["session_id"]; user = User(event["d"]["user"]); diff --git a/lib/src/requests.dart b/lib/src/requests.dart index 3aa9269..f9444e9 100644 --- a/lib/src/requests.dart +++ b/lib/src/requests.dart @@ -36,10 +36,11 @@ Future requestWebSocketURL() async { class Sender { final String? _token; + final String id; Map headers = {}; dynamic channels; - Sender(this._token) { + Sender(this._token, this.id) { headers = { "Content-Type": "application/json", "Authorization": "Bot $_token", @@ -86,6 +87,15 @@ class Sender { res = json.decode(res.body); return res; } + + Future setCommands(List commands) async { + dynamic res = await http.put(Uri.parse("$apiURL/applications/$id/commands"), headers: headers, body: json.encode(commands)); + if (res.statusCode != 200 || res.statusCode != 201) { + throw Exception("Error ${res.statusCode} receiving the member"); + } + res = json.decode(res.body); + return res; + } } Future interactionReply(String id, String token, Map content) async {