Removed AbstractPermissions class. Created CacheTextChannel and GuildTextChannel mixin.

Added toSnowflakeEntity extensions to int and String.
Changed api in Guild ban and kick methods to accept SnowflakeEntities.
[ci skip]
This commit is contained in:
Szymon Uglis 2020-06-19 14:28:47 +02:00
parent 329800df29
commit f5f199a615
9 changed files with 250 additions and 122 deletions

View file

@ -157,7 +157,6 @@ part "src/core/application/OAuth2Info.dart";
part "src/core/application/AppTeam.dart";
part "src/core/permissions/Permissions.dart";
part "src/core/permissions/AbstractPermissions.dart";
part "src/core/permissions/PermissionOverrides.dart";
part "src/core/permissions/PermissionsConstants.dart";

View file

@ -33,7 +33,11 @@ class Channel extends SnowflakeEntity {
return GroupDMChannel._new(raw, client);
break;
case 0:
return CachelessTextChannel._new(raw, channelGuild == null ? Snowflake(raw["guild_id"]) : channelGuild.id, client);
if(channelGuild == null) {
return CachelessTextChannel._new(raw, Snowflake(raw["guild_id"]), client);
}
return CacheTextChannel._new(raw, channelGuild, client);
break;
case 2:
if(channelGuild == null) {

View file

@ -106,16 +106,22 @@ abstract class CachelessGuildChannel extends IGuildChannel {
}
}
/// Allows to set permissions for channel. [id] can be either User or Role
/// Throws if [id] isn't [User] or [Role]
/// Allows to set permissions for channel. [entity] can be either User or Role
/// Throws if [entity] isn't [User] or [Role]
@override
Future<void> editChannelPermission(PermissionsBuilder perms, SnowflakeEntity id, {String? auditReason}) {
if (id is! Role || id is! User) {
throw ArgumentError("The `id` property must be either Role or User");
Future<void> editChannelPermission(PermissionsBuilder perms, SnowflakeEntity entity, {String? auditReason}) {
if (entity is! IRole && entity is! User) {
return Future.error(Exception("The `id` property must be either Role or User"));
}
return client._http._execute(BasicRequest._new("/channels/${this.id}/permissions/${id.toString()}",
method: "PUT", body: perms._build()._build(), auditLog: auditReason));
final permSet = perms._build();
return client._http._execute(BasicRequest._new("/channels/${this.id}/permissions/${entity.id.toString()}",
method: "PUT", body: {
"type" : entity is IRole ? "role" : "member",
"allow" : permSet.allow,
"deny" : permSet.deny
}, auditLog: auditReason));
}
/// Deletes permission overwrite for given User or Role [id]
@ -210,7 +216,7 @@ abstract class CacheGuildChannel extends CachelessGuildChannel {
permissions &= ~overEveryone.deny;
permissions |= overEveryone.allow;
// ignore: avoid_catches_without_on_clauses, empty_catches
// ignore: avoid_catches_without_on_clauses, empty_catches
} on Exception {}
try {
@ -218,7 +224,7 @@ abstract class CacheGuildChannel extends CachelessGuildChannel {
permissions &= ~overRole.deny;
permissions |= overRole.allow;
// ignore: avoid_catches_without_on_clauses, empty_catches
// ignore: avoid_catches_without_on_clauses, empty_catches
} on Exception {}
return Permissions.fromInt(permissions);

View file

@ -1,8 +1,7 @@
part of nyxx;
/// [CachelessTextChannel] represents single text channel on [Guild].
/// Inhertits from [MessageChannel] and mixes [CacheGuildChannel].
class CachelessTextChannel extends CachelessGuildChannel with MessageChannel, ISend implements Mentionable, ITextChannel {
/// [ITextChannel] in context of [Guild].
abstract class GuildTextChannel implements Channel, CachelessGuildChannel, ITextChannel {
/// The channel's topic.
late final String? topic;
@ -18,7 +17,7 @@ class CachelessTextChannel extends CachelessGuildChannel with MessageChannel, IS
String get url => "https://discordapp.com/channels/${this.guildId.toString()}"
"/${this.id.toString()}";
CachelessTextChannel._new(Map<String, dynamic> raw, Snowflake guildId, Nyxx client) : super._new(raw, 0, guildId, client) {
void _initialize(Map<String, dynamic> raw, Snowflake guildId, Nyxx client) {
this.topic = raw["topic"] as String?;
this.slowModeThreshold = raw["rate_limit_per_user"] as int? ?? 0;
}
@ -102,3 +101,21 @@ class CachelessTextChannel extends CachelessGuildChannel with MessageChannel, IS
/// Returns mention to channel
String toString() => this.mention;
}
/// [CachelessTextChannel] represents single text channel on [Guild].
/// Inhertits from [MessageChannel] and mixes [CacheGuildChannel].
class CachelessTextChannel extends CachelessGuildChannel with GuildTextChannel, MessageChannel, ISend implements Mentionable, ITextChannel {
CachelessTextChannel._new(Map<String, dynamic> raw, Snowflake guildId, Nyxx client) : super._new(raw, 0, guildId, client) {
_initialize(raw, guildId, client);
}
}
/// [CachelessTextChannel] represents single text channel on [Guild].
/// Inhertits from [MessageChannel] and mixes [CacheGuildChannel].
class CacheTextChannel extends CacheGuildChannel with GuildTextChannel, MessageChannel, ISend implements Mentionable, ITextChannel {
CacheTextChannel._new(Map<String, dynamic> raw, Guild guild, Nyxx client) : super._new(raw, 0, guild, client) {
_initialize(raw, guild.id, client);
}
}

View file

@ -592,8 +592,8 @@ class Guild extends SnowflakeEntity implements Disposable {
///
/// await guild.ban(member);
/// ```
Future<void> ban(CacheMember member, {int deleteMessageDays = 0, String? auditReason}) async =>
client._http._execute(BasicRequest._new("/guilds/${this.id}/bans/${member.id.toString()}",
Future<void> ban(SnowflakeEntity user, {int deleteMessageDays = 0, String? auditReason}) async =>
client._http._execute(BasicRequest._new("/guilds/${this.id}/bans/${user.id.toString()}",
method: "PUT", auditLog: auditReason, body: {"delete-message-days": deleteMessageDays}));
/// Kicks user from guild. Member is removed from guild and he is able to rejoin
@ -601,8 +601,8 @@ class Guild extends SnowflakeEntity implements Disposable {
/// ```
/// await guild.kick(member);
/// ```
Future<void> kick(CacheMember member, {String? auditReason}) async =>
client._http._execute(BasicRequest._new("/guilds/${this.id.toString()}/members/${member.id.toString()}",
Future<void> kick(SnowflakeEntity user, {String? auditReason}) async =>
client._http._execute(BasicRequest._new("/guilds/${this.id.toString()}/members/${user.id.toString()}",
method: "DELTE", auditLog: auditReason));
/// Unbans a user by ID.

View file

@ -1,100 +0,0 @@
part of nyxx;
/// Provides common properties for [Permissions] and [PermissionsBuilder]
abstract class AbstractPermissions {
/// The raw permission code.
late final int raw;
/// True if user can create InstantInvite
late final bool createInstantInvite;
/// True if user can kick members
late final bool kickMembers;
/// True if user can ban members
late final bool banMembers;
/// True if user is administrator
late final bool administrator;
/// True if user can manager channels
late final bool manageChannels;
/// True if user can manager guilds
late final bool manageGuild;
/// Allows to add reactions
late final bool addReactions;
/// Allows for using priority speaker in a voice channel
late final bool prioritySpeaker;
/// Allow to view audit logs
late final bool viewAuditLog;
/// Allow viewing channels (OLD READ_MESSAGES)
late final bool viewChannel;
/// True if user can send messages
late final bool sendMessages;
/// True if user can send TTF messages
late final bool sendTtsMessages;
/// True if user can manage messages
late final bool manageMessages;
/// True if user can send links in messages
late final bool embedLinks;
/// True if user can attach files in messages
late final bool attachFiles;
/// True if user can read messages history
late final bool readMessageHistory;
/// True if user can mention everyone
late final bool mentionEveryone;
/// True if user can use external emojis
late final bool useExternalEmojis;
/// True if user can connect to voice channel
late final bool connect;
/// True if user can speak
late final bool speak;
/// True if user can mute members
late final bool muteMembers;
/// True if user can deafen members
late final bool deafenMembers;
/// True if user can move members
late final bool moveMembers;
/// Allows for using voice-activity-detection in a voice channel
late final bool useVad;
/// True if user can change nick
late final bool changeNickname;
/// True if user can manager others nicknames
late final bool manageNicknames;
/// True if user can manage server's roles
late final bool manageRoles;
/// True if user can manage webhooks
late final bool manageWebhooks;
/// Allows management and editing of emojis
late final bool manageEmojis;
/// Allows the user to go live
late final bool stream;
/// Allows for viewing guild insights
late final bool viewGuildInsights;
}

View file

@ -1,7 +1,103 @@
part of nyxx;
/// Permissions for a role or channel override.
class Permissions extends AbstractPermissions {
class Permissions {
/// The raw permission code.
late final int raw;
/// True if user can create InstantInvite
late final bool createInstantInvite;
/// True if user can kick members
late final bool kickMembers;
/// True if user can ban members
late final bool banMembers;
/// True if user is administrator
late final bool administrator;
/// True if user can manager channels
late final bool manageChannels;
/// True if user can manager guilds
late final bool manageGuild;
/// Allows to add reactions
late final bool addReactions;
/// Allows for using priority speaker in a voice channel
late final bool prioritySpeaker;
/// Allow to view audit logs
late final bool viewAuditLog;
/// Allow viewing channels (OLD READ_MESSAGES)
late final bool viewChannel;
/// True if user can send messages
late final bool sendMessages;
/// True if user can send TTF messages
late final bool sendTtsMessages;
/// True if user can manage messages
late final bool manageMessages;
/// True if user can send links in messages
late final bool embedLinks;
/// True if user can attach files in messages
late final bool attachFiles;
/// True if user can read messages history
late final bool readMessageHistory;
/// True if user can mention everyone
late final bool mentionEveryone;
/// True if user can use external emojis
late final bool useExternalEmojis;
/// True if user can connect to voice channel
late final bool connect;
/// True if user can speak
late final bool speak;
/// True if user can mute members
late final bool muteMembers;
/// True if user can deafen members
late final bool deafenMembers;
/// True if user can move members
late final bool moveMembers;
/// Allows for using voice-activity-detection in a voice channel
late final bool useVad;
/// True if user can change nick
late final bool changeNickname;
/// True if user can manager others nicknames
late final bool manageNicknames;
/// True if user can manage server's roles
late final bool manageRoles;
/// True if user can manage webhooks
late final bool manageWebhooks;
/// Allows management and editing of emojis
late final bool manageEmojis;
/// Allows the user to go live
late final bool stream;
/// Allows for viewing guild insights
late final bool viewGuildInsights;
/// Makes a [Permissions] object from a raw permission code.
Permissions.fromInt(int permissions) {
_construct(permissions);

View file

@ -9,7 +9,103 @@ class _PermissionsSet {
}
/// Builder for permissions.
class PermissionsBuilder extends AbstractPermissions {
class PermissionsBuilder {
/// The raw permission code.
int? raw;
/// True if user can create InstantInvite
bool? createInstantInvite;
/// True if user can kick members
bool? kickMembers;
/// True if user can ban members
bool? banMembers;
/// True if user is administrator
bool? administrator;
/// True if user can manager channels
bool? manageChannels;
/// True if user can manager guilds
bool? manageGuild;
/// Allows to add reactions
bool? addReactions;
/// Allows for using priority speaker in a voice channel
bool? prioritySpeaker;
/// Allow to view audit logs
bool? viewAuditLog;
/// Allow viewing channels (OLD READ_MESSAGES)
bool? viewChannel;
/// True if user can send messages
bool? sendMessages;
/// True if user can send TTF messages
bool? sendTtsMessages;
/// True if user can manage messages
bool? manageMessages;
/// True if user can send links in messages
bool? embedLinks;
/// True if user can attach files in messages
bool? attachFiles;
/// True if user can read messages history
bool? readMessageHistory;
/// True if user can mention everyone
bool? mentionEveryone;
/// True if user can use external emojis
bool? useExternalEmojis;
/// True if user can connect to voice channel
bool? connect;
/// True if user can speak
bool? speak;
/// True if user can mute members
bool? muteMembers;
/// True if user can deafen members
bool? deafenMembers;
/// True if user can move members
bool? moveMembers;
/// Allows for using voice-activity-detection in a voice channel
bool? useVad;
/// True if user can change nick
bool? changeNickname;
/// True if user can manager others nicknames
bool? manageNicknames;
/// True if user can manage server's roles
bool? manageRoles;
/// True if user can manage webhooks
bool? manageWebhooks;
/// Allows management and editing of emojis
bool? manageEmojis;
/// Allows the user to go live
bool? stream;
/// Allows for viewing guild insights
bool? viewGuildInsights;
_PermissionsSet _build() {
final tmp = _PermissionsSet();
@ -45,7 +141,11 @@ class PermissionsBuilder extends AbstractPermissions {
}
// TODO: NNBD - To consider
void _apply(_PermissionsSet perm, bool applies, int constant) {
void _apply(_PermissionsSet perm, bool? applies, int constant) {
if(applies == null) {
return;
}
if (applies) {
perm.allow |= constant;
} else {

View file

@ -4,10 +4,16 @@ part of nyxx;
extension IntExtensions on int {
/// Converts int to [Snowflake]
Snowflake toSnowflake() => Snowflake(this);
/// Converts int to [SnowflakeEntity]
SnowflakeEntity toSnowflakeEntity() => SnowflakeEntity(this.toSnowflake());
}
/// Extension on int
extension StringExtensions on String {
/// Converts String to [Snowflake]
Snowflake toSnowflake() => Snowflake(this);
/// Converts String to [SnowflakeEntity]
SnowflakeEntity toSnowflakeEntity() => SnowflakeEntity(this.toSnowflake());
}