From 99f14e1174e69160fe5f3595b934610c52c8ddee Mon Sep 17 00:00:00 2001 From: Szymon Uglis Date: Mon, 19 Apr 2021 21:37:51 +0200 Subject: [PATCH] Fix unneeded code from Nyxx; Move token check at the top of constructor; Add userCachePolicyLocation to CacheOptions; Fix docs typos; Fix sticker deserialization --- nyxx/lib/src/ClientOptions.dart | 3 ++ nyxx/lib/src/Nyxx.dart | 37 +++---------------- nyxx/lib/src/core/channel/DMChannel.dart | 2 +- nyxx/lib/src/core/channel/TextChannel.dart | 2 +- .../core/channel/guild/TextGuildChannel.dart | 2 +- nyxx/lib/src/core/guild/Guild.dart | 2 +- nyxx/lib/src/core/message/Message.dart | 10 ++++- nyxx/lib/src/core/message/Sticker.dart | 6 ++- nyxx/lib/src/core/user/Member.dart | 4 +- nyxx/lib/src/events/GuildEvents.dart | 8 ++-- nyxx/lib/src/events/MessageEvents.dart | 3 +- 11 files changed, 34 insertions(+), 45 deletions(-) diff --git a/nyxx/lib/src/ClientOptions.dart b/nyxx/lib/src/ClientOptions.dart index 105bb787..ce0d10cf 100644 --- a/nyxx/lib/src/ClientOptions.dart +++ b/nyxx/lib/src/ClientOptions.dart @@ -13,6 +13,9 @@ class CacheOptions { /// Defines which channel entities are preserved in cache. CachePolicy channelCachePolicy = ChannelCachePolicy.def; + + /// Defines in which places user can be cached + CachePolicyLocation userCachePolicyLocation = CachePolicyLocation(); } /// Optional client settings which can be used when creating new instance diff --git a/nyxx/lib/src/Nyxx.dart b/nyxx/lib/src/Nyxx.dart index 1a387c89..26e41be0 100644 --- a/nyxx/lib/src/Nyxx.dart +++ b/nyxx/lib/src/Nyxx.dart @@ -107,6 +107,9 @@ class NyxxRest extends INyxx { bool ignoreExceptions = true, bool useDefaultLogger = true, Level? defaultLoggerLogLevel}) { + if (_token.isEmpty) { + throw MissingTokenError(); + } if (useDefaultLogger) { Logger.root.level = defaultLoggerLogLevel ?? Level.ALL; @@ -117,11 +120,7 @@ class NyxxRest extends INyxx { }); } - this._logger.info("Starting bot with pid: $pid"); - - if (_token.isEmpty) { - throw MissingTokenError(); - } + this._logger.info("Starting bot with pid: $pid. To stop the bot gracefully send SIGTERM or SIGKILL"); if (!Platform.isWindows) { ProcessSignal.sigterm.watch().forEach((event) async { @@ -323,13 +322,13 @@ class Nyxx extends NyxxRest { this._ws = _ConnectionManager(this); } - /// The client"s uptime. + /// The client's uptime. Duration get uptime => DateTime.now().difference(_startTime); /// [DateTime] when client was started DateTime get startTime => _startTime; - /// This endpoint is only for Public guilds if bot is not int the guild. + /// This endpoint is only for public guilds if bot is not int the guild. Future fetchGuildPreview(Snowflake guildId) async => this._httpEndpoints.fetchGuildPreview(guildId); @@ -351,29 +350,6 @@ class Nyxx extends NyxxRest { Future fetchUser(Snowflake userId) => this._httpEndpoints.fetchUser(userId); - // /// Creates new guild with provided builder. - // /// Only for bots with less than 10 guilds otherwise it will return Future with error. - // /// - // /// ``` - // /// var guildBuilder = GuildBuilder() - // /// ..name = "Example Guild" - // /// ..roles = [RoleBuilder()..name = "Example Role] - // /// var newGuild = await client.createGuild(guildBuilder); - // /// ``` - // Future createGuild(GuildBuilder builder) async { - // if (this.guilds.count >= 10) { - // return Future.error(ArgumentError("Guild cannot be created if bot is in 10 or more guilds")); - // } - // - // final response = await this._http._execute(BasicRequest._new("/guilds", method: "POST")); - // - // if (response is HttpResponseSuccess) { - // return Guild._new(this, response.jsonBody as Map); - // } - // - // return Future.error(response); - // } - /// Gets a webhook by its id and/or token. /// If token is supplied authentication is not needed. Future fetchWebhook(Snowflake id, {String token = ""}) => @@ -421,7 +397,6 @@ class Nyxx extends NyxxRest { await this._events.dispose(); await guilds.dispose(); await users.dispose(); - await guilds.dispose(); this._logger.info("Exiting..."); exit(0); diff --git a/nyxx/lib/src/core/channel/DMChannel.dart b/nyxx/lib/src/core/channel/DMChannel.dart index f1980333..aafce3d1 100644 --- a/nyxx/lib/src/core/channel/DMChannel.dart +++ b/nyxx/lib/src/core/channel/DMChannel.dart @@ -58,7 +58,7 @@ class DMChannel extends IChannel implements TextChannel { client._httpEndpoints.fetchMessage(this.id, messageId); @override - Future getMessage(Snowflake id) => Future.value(this.messageCache[id]); + Message? getMessage(Snowflake id) => this.messageCache[id]; @override Future sendMessage({dynamic content, EmbedBuilder? embed, List? files, bool? tts, AllowedMentions? allowedMentions, MessageBuilder? builder, ReplyBuilder? replyBuilder}) => diff --git a/nyxx/lib/src/core/channel/TextChannel.dart b/nyxx/lib/src/core/channel/TextChannel.dart index 4daa4012..0c4335a6 100644 --- a/nyxx/lib/src/core/channel/TextChannel.dart +++ b/nyxx/lib/src/core/channel/TextChannel.dart @@ -8,7 +8,7 @@ abstract class TextChannel implements IChannel, ISend { MessageCache get messageCache; /// Returns [Message] with given id from CACHE - Future getMessage(Snowflake id); + Message? getMessage(Snowflake id); /// Returns [Message] downloaded from API Future fetchMessage(Snowflake id); diff --git a/nyxx/lib/src/core/channel/guild/TextGuildChannel.dart b/nyxx/lib/src/core/channel/guild/TextGuildChannel.dart index 37804773..a195b98e 100644 --- a/nyxx/lib/src/core/channel/guild/TextGuildChannel.dart +++ b/nyxx/lib/src/core/channel/guild/TextGuildChannel.dart @@ -79,7 +79,7 @@ class TextGuildChannel extends GuildChannel implements TextChannel { client._httpEndpoints.fetchMessage(this.id, messageId); @override - Future getMessage(Snowflake id) => Future.value(this.messageCache[id]); + Message? getMessage(Snowflake id) => this.messageCache[id]; @override Future sendMessage({dynamic content, EmbedBuilder? embed, List? files, bool? tts, AllowedMentions? allowedMentions, MessageBuilder? builder, ReplyBuilder? replyBuilder}) => diff --git a/nyxx/lib/src/core/guild/Guild.dart b/nyxx/lib/src/core/guild/Guild.dart index 46446f4d..59d03537 100644 --- a/nyxx/lib/src/core/guild/Guild.dart +++ b/nyxx/lib/src/core/guild/Guild.dart @@ -408,7 +408,7 @@ class Guild extends SnowflakeEntity { /// Request members from gateway. Requires privileged intents in order to work. void requestChunking() => this.shard.requestMembers(this.id); - /// Allows to create new guil channel + /// Allows to create new guild channel Future createChannel(ChannelBuilder channelBuilder) => this.client.httpEndpoints.createGuildChannel(this.id, channelBuilder); diff --git a/nyxx/lib/src/core/message/Message.dart b/nyxx/lib/src/core/message/Message.dart index beb812ac..613190a9 100644 --- a/nyxx/lib/src/core/message/Message.dart +++ b/nyxx/lib/src/core/message/Message.dart @@ -191,7 +191,10 @@ class DMMessage extends Message { if (user == null) { final authorData = raw["author"] as Map; this.author = User._new(client, authorData); - this.client.users.add(this.author.id, this.author); + + if (client._cacheOptions.userCachePolicyLocation.objectConstructor) { + this.client.users[this.author.id] = this.author; + } } else { this.author = user; } @@ -240,7 +243,10 @@ class GuildMessage extends Message { this.author = Webhook._new(raw["author"] as Map, client); } else { this.author = User._new(client, raw["author"] as Map); - this.client.users[this.author.id] = this.author as User; + + if (client._cacheOptions.userCachePolicyLocation.objectConstructor) { + this.client.users[this.author.id] = this.author as User; + } } if (raw["member"] != null) { diff --git a/nyxx/lib/src/core/message/Sticker.dart b/nyxx/lib/src/core/message/Sticker.dart index 7ae7016f..652218b7 100644 --- a/nyxx/lib/src/core/message/Sticker.dart +++ b/nyxx/lib/src/core/message/Sticker.dart @@ -19,7 +19,7 @@ class Sticker extends SnowflakeEntity { late final String asset; /// Sticker preview asset hash - late final String assetPreview; + late final String? assetPreview; /// Type of sticker format late final StickerFormat format; @@ -36,7 +36,9 @@ class Sticker extends SnowflakeEntity { this.description = raw["description"] as String; this.tags = raw["tags"] as String?; this.asset = raw["asset"] as String; - this.assetPreview = raw["preview_asset"] as String; + this.assetPreview = raw["preview_asset"] != null + ? raw["preview_asset"] as String + : null; this.format = StickerFormat.from(raw["format_type"] as int); } } diff --git a/nyxx/lib/src/core/user/Member.dart b/nyxx/lib/src/core/user/Member.dart index 43849b17..1fc0eb3b 100644 --- a/nyxx/lib/src/core/user/Member.dart +++ b/nyxx/lib/src/core/user/Member.dart @@ -31,7 +31,7 @@ class Member extends SnowflakeEntity { /// Voice state of member. Null if not connected to channel or voice state not cached VoiceState? get voiceState => this.guild.getFromCache()?.voiceStates[this.id]; - /// When the user starting boosting the guild + /// When the user starting boosting the guild late DateTime? boostingSince; // TODO: is everything okay? @@ -70,7 +70,7 @@ class Member extends SnowflakeEntity { this.joinedAt = DateTime.parse(raw["joined_at"] as String).toUtc(); } - if (!client.users.hasKey(this.id)) { + if (!client.users.hasKey(this.id) && client._cacheOptions.userCachePolicyLocation.objectConstructor) { final userRaw = raw["user"] as Map; if (userRaw["id"] != null && userRaw.length != 1) { diff --git a/nyxx/lib/src/events/GuildEvents.dart b/nyxx/lib/src/events/GuildEvents.dart index 51e1de3c..eed8c0a2 100644 --- a/nyxx/lib/src/events/GuildEvents.dart +++ b/nyxx/lib/src/events/GuildEvents.dart @@ -80,7 +80,9 @@ class GuildMemberUpdateEvent { this.member = _MemberCacheable(client, Snowflake(raw["d"]["user"]["id"]), guild); final user = User._new(client, raw["d"]["user"] as Map); - client.users[user.id] = user; + if (client._cacheOptions.userCachePolicyLocation.event) { + client.users[user.id] = user; + } final memberInstance = this.member.getFromCache(); if (memberInstance == null) { @@ -116,7 +118,7 @@ class GuildMemberAddEvent { this.member = Member._new(client, raw["d"] as Map, this.guild.id); this.user = User._new(client, raw["d"]["user"] as Map); - if (!client.users.hasKey(this.user.id)) { + if (!client.users.hasKey(this.user.id) && client._cacheOptions.userCachePolicyLocation.event) { client.users[user.id] = user; } @@ -235,7 +237,7 @@ class RoleUpdateEvent { /// The guild that the member was banned from. late final Cacheable guild; - + RoleUpdateEvent._new(Map raw, Nyxx client) { this.guild = _GuildCacheable(client, Snowflake(raw["d"]["guild_id"])); this.role = Role._new(client, raw["d"]["role"] as Map, this.guild.id); diff --git a/nyxx/lib/src/events/MessageEvents.dart b/nyxx/lib/src/events/MessageEvents.dart index b2ab031e..c930232b 100644 --- a/nyxx/lib/src/events/MessageEvents.dart +++ b/nyxx/lib/src/events/MessageEvents.dart @@ -75,7 +75,8 @@ abstract class MessageReactionEvent { /// Channel on which event was fired late final Cacheable channel; - /// Reference to guild if event happend in guild + // TODO: Probably not working + /// Reference to guild if event happened in guild late final Cacheable guild; /// Message reference