Skip to content

Commit

Permalink
Merge pull request #108 from mineral-dart/feat-add-permission-structure
Browse files Browse the repository at this point in the history
feat: Add permission structure
  • Loading branch information
LeadcodeDev authored May 13, 2023
2 parents 9e53c40 + 4273666 commit b426ebd
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
19 changes: 19 additions & 0 deletions lib/src/api/client/permission_bit_field.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:mineral/core/api.dart';
import 'package:mineral/src/helper.dart';
import 'package:mineral_ioc/ioc.dart';

class PermissionBitField {
final List<int> _permissions;
final bool _isOwner;

PermissionBitField (this._permissions, this._isOwner);

List<ClientPermission> get all => _permissions.fold([], (previousValue, element) => [
...previousValue,
...Helper.bitfieldToPermissions(element)
]);

bool has (ClientPermission permission, { bool withAdmin = false }) => withAdmin
? all.contains(ClientPermission.administrator)
: all.contains(permission);
}
36 changes: 34 additions & 2 deletions lib/src/api/guilds/guild_member.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import 'package:mineral/src/api/guilds/guild_member_presence.dart';
import 'package:mineral/src/api/managers/guild_role_manager.dart';
import 'package:mineral_ioc/ioc.dart';

import '../client/permission_bit_field.dart';

/// Represents a member of a [Guild] context.
class GuildMember {
User _user;
Expand Down Expand Up @@ -59,7 +61,10 @@ class GuildMember {
: null;

/// Get the permissions of this.
String? get permissions => _permissions;
PermissionBitField get permissions => PermissionBitField(
_roles.cache.values.map((e) => e.permissions).toList(),
guild.owner.id == id
);

/// Get the pending status of this.
bool get pending => _pending;
Expand Down Expand Up @@ -168,12 +173,39 @@ class GuildMember {
.build();
}

/// Returns whether of this has a given is manageable]
/// ```dart
/// final member = member.isManageable;
/// print(member);
/// ```
bool get isManageable {
if (user.id == guild.owner.id) return false;
if (user.id == ioc.use<MineralClient>().user.id) return false;
if (ioc.use<MineralClient>().user.id == guild.owner.id) return true;

return guild.members.me.roles.highest.position < roles.highest.position;
}

/// Returns whether of this has a given can be banned
/// ```dart
/// final member = member.isBannable;
/// print(member);
/// ```
bool get isBannable => isManageable && permissions.has(ClientPermission.banMembers);

/// Returns weather of this is a [Guild] owner.
/// ```dart
/// final member = member.isOwner;
/// print(member);
/// ```
bool get isOwner => guild.owner.id == user.id;

/// Returns a taggable [String] representation of this.
@override
String toString () => '<@${_nickname != null ? '!' : ''}${user.id}>';

/// Returns a clone of this
GuildMember clone () => GuildMember(user, nickname, _avatar, joinedAt, _premiumSince, permissions, pending, _timeoutDuration, roles, voice, guild, presence);
GuildMember clone () => GuildMember(user, nickname, _avatar, joinedAt, _premiumSince, _permissions, pending, _timeoutDuration, roles, voice, guild, presence);

factory GuildMember.from({ required user, required GuildRoleManager roles, required Guild guild, dynamic member, required VoiceManager voice }) {
MemberRoleManager memberRoleManager = MemberRoleManager(manager: roles, memberId: user.id);
Expand Down
3 changes: 3 additions & 0 deletions lib/src/api/managers/member_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class MemberManager extends CacheManager<GuildMember> {

MemberManager(this._guildId);

/// Get the [GuildMember] of the [MineralClient]
GuildMember get me => ioc.use<MineralClient>().user.toGuildMember(_guildId)!;

Future<Map<Snowflake, GuildMember>> sync () async {
Response response = await ioc.use<DiscordApiHttpService>()
.get(url: "/guilds/$_guildId/members")
Expand Down
5 changes: 5 additions & 0 deletions lib/src/api/managers/member_role_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class MemberRoleManager extends CacheManager<Role> {

Guild get guild => _guild;

Role get highest => cache.values.fold(_guild.roles.everyone, (previousValue, element) {
if (element.position > previousValue.position) return element;
return previousValue;
});

/// Add a [Role] to the [GuildMember]
///
/// Example :
Expand Down

0 comments on commit b426ebd

Please sign in to comment.