Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add permission structure #108

Merged
merged 5 commits into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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