Skip to content

Commit

Permalink
Merge pull request #297 from nyxx-discord/dev
Browse files Browse the repository at this point in the history
Release 3.2.6
  • Loading branch information
l7ssha authored Jan 31, 2022
2 parents 31bd2e2 + 4b41efd commit 8f8cdb2
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 48 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.2.6
__01.02.2022__

- bugfix: Fix permission serialisation

## 3.2.5
__30.01.2022__

Expand Down
2 changes: 1 addition & 1 deletion lib/src/internal/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Constants {
static const int apiVersion = 9;

/// Version of Nyxx
static const String version = "3.2.5";
static const String version = "3.2.6";

/// Url to Nyxx repo
static const String repoUrl = "https://github.com/nyxx-discord/nyxx";
Expand Down
8 changes: 2 additions & 6 deletions lib/src/internal/http_endpoints.dart
Original file line number Diff line number Diff line change
Expand Up @@ -890,18 +890,14 @@ class HttpEndpoints implements IHttpEndpoints {

@override
Future<void> editChannelPermissions(Snowflake channelId, PermissionsBuilder perms, SnowflakeEntity entity, {String? auditReason}) async {
final permSet = perms.build();

await executeSafe(BasicRequest("/channels/$channelId/permissions/${entity.id.toString()}",
method: "PUT", body: {"type": entity is Role ? "role" : "member", "allow": permSet.allow, "deny": permSet.deny}, auditLog: auditReason));
method: "PUT", body: {"type": entity is IRole ? 0 : 1, ...perms.build()}, auditLog: auditReason));
}

@override
Future<void> editChannelPermissionOverrides(Snowflake channelId, PermissionOverrideBuilder permissionBuilder, {String? auditReason}) async {
final permSet = permissionBuilder.build();

await executeSafe(BasicRequest("/channels/$channelId/permissions/${permissionBuilder.id.toString()}",
method: "PUT", body: {"type": permissionBuilder.type, "allow": permSet.allow, "deny": permSet.deny}, auditLog: auditReason));
method: "PUT", body: permissionBuilder.build(), auditLog: auditReason));
}

@override
Expand Down
3 changes: 1 addition & 2 deletions lib/src/utils/builders/channel_builder.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:nyxx/nyxx.dart';
import 'package:nyxx/src/core/channel/channel.dart';

/// Builder for creating mini channel instance
abstract class ChannelBuilder implements Builder {
Expand All @@ -20,7 +19,7 @@ abstract class ChannelBuilder implements Builder {
if (type != null) "type": type!.value,
if (position != null) "position": position,
if (parentChannel != null) "parent_id": parentChannel!.id.toString(),
if (permissionOverrides != null) "permission_overwrites": permissionOverrides!.map((e) => e.build().build()).toList(),
if (permissionOverrides != null) "permission_overwrites": permissionOverrides!.map((e) => e.build()).toList(),
};
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/utils/builders/guild_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class RoleBuilder extends Builder {
if (color != null) "color": color!.value,
if (hoist != null) "hoist": hoist,
if (position != null) "position": position,
if (permission != null) "permission": permission!.build().build(),
if (permission != null) "permissions": permission!.calculatePermissionValue().toString(),
if (mentionable != null) "mentionable": mentionable,
if (roleIcon != null) "icon": roleIcon!.getBase64(),
if (roleIconEmoji != null) "unicode_emoji": roleIconEmoji
Expand Down
28 changes: 21 additions & 7 deletions lib/src/utils/builders/permissions_builder.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import 'package:nyxx/src/core/snowflake.dart';
import 'package:nyxx/src/core/snowflake_entity.dart';
import 'package:nyxx/nyxx.dart';
import 'package:nyxx/src/core/guild/role.dart';
import 'package:nyxx/src/core/permissions/permissions.dart';
import 'package:nyxx/src/core/permissions/permissions_constants.dart';
import 'package:nyxx/src/typedefs.dart';

/// Set of permissions ints
class _PermissionsSet {
Expand Down Expand Up @@ -32,10 +29,17 @@ class PermissionOverrideBuilder extends PermissionsBuilder {
: type = entity is IRole ? 0 : 1,
id = entity.id,
super();

@override
RawApiMap build() => {
...super.build(),
"id": id.toString(),
"type": type,
};
}

/// Builder for permissions.
class PermissionsBuilder {
class PermissionsBuilder extends Builder {
/// The raw permission code.
int? raw;

Expand Down Expand Up @@ -189,12 +193,12 @@ class PermissionsBuilder {

/// Calculates permission int
int calculatePermissionValue() {
final set = build();
final set = _calculatePermissionSet();

return set.allow & ~set.deny;
}

_PermissionsSet build() {
_PermissionsSet _calculatePermissionSet() {
final permissionSet = _PermissionsSet();

_apply(permissionSet, createInstantInvite, PermissionsConstants.createInstantInvite);
Expand Down Expand Up @@ -234,6 +238,16 @@ class PermissionsBuilder {
return permissionSet;
}

@override
RawApiMap build() {
_PermissionsSet permissionSet = _calculatePermissionSet();

return {
"allow": permissionSet.allow.toString(),
"deny": permissionSet.deny.toString(),
};
}

void _apply(_PermissionsSet perm, bool? applies, int constant) {
if (applies == null) {
return;
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: nyxx
version: 3.2.5
version: 3.2.6
description: A Discord library for Dart. Simple, robust framework for creating discord bots for Dart language.
homepage: https://github.com/nyxx-discord/nyxx
repository: https://github.com/nyxx-discord/nyxx
Expand Down
72 changes: 42 additions & 30 deletions test/unit/builders_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ main() {
final builder = TextChannelBuilder.create("test");
builder.permissionOverrides = [PermissionOverrideBuilder.from(0, Snowflake.zero(), Permissions.empty())];

final expectedResult = {'permission_overwrites': [{'allow': 0, 'deny': 122406567679}], 'name': 'test'};
final expectedResult = {
'permission_overwrites': [
{'allow': "0", 'deny': "122406567679", 'id': '0', 'type': 0}
],
'name': 'test'
};
expect(builder.build(), expectedResult);
});
});
Expand Down Expand Up @@ -98,49 +103,47 @@ main() {

test('PermissionOverrideBuilder', () {
final builder = PermissionOverrideBuilder(0, Snowflake.zero());
expect(builder.build().build(), equals({"allow": 0, "deny": 0}));
expect(builder.build(), equals({"allow": "0", "deny": "0", 'id': '0', 'type': 0}));

final fromBuilder = PermissionOverrideBuilder.from(0, Snowflake.zero(), Permissions.empty());
expect(fromBuilder.build().build(), equals({"allow": 0, "deny": 122406567679}));
expect(fromBuilder.build(), equals({"allow": "0", "deny": "122406567679", 'id': '0', 'type': 0}));
expect(fromBuilder.calculatePermissionValue(), equals(0));

final ofBuilder = PermissionOverrideBuilder.of(MockMember(Snowflake.zero()))
..sendMessages = true
..addReactions = false;

expect(ofBuilder.build().build(), equals({"allow": 1 << 11, "deny": 1 << 6}));
expect(ofBuilder.build(), equals({"allow": (1 << 11).toString(), "deny": (1 << 6).toString(), 'id': '0', 'type': 1}));
expect(ofBuilder.calculatePermissionValue(), equals(1 << 11));
});

group('MemberBuilder', () {
test('channel empty', () {
final builder = MemberBuilder()
..channel = Snowflake.zero();
final builder = MemberBuilder()..channel = Snowflake.zero();

expect({}, builder.build());
});

test('channel with value', () {
final builder = MemberBuilder()
..channel = Snowflake(123);
final builder = MemberBuilder()..channel = Snowflake(123);

expect({'channel_id': '123'}, builder.build());
});

test('timeout empty', () {
final now = DateTime.now();

final builder = MemberBuilder()
..timeoutUntil = now;
final builder = MemberBuilder()..timeoutUntil = now;

expect({'communication_disabled_until': now.toIso8601String()}, builder.build());
});

test('roles serialization', () {
final builder = MemberBuilder()
..roles = [Snowflake(1), Snowflake(2)];
final builder = MemberBuilder()..roles = [Snowflake(1), Snowflake(2)];

expect({'roles': ['1', '2']}, builder.build());
expect({
'roles': ['1', '2']
}, builder.build());
});
});

Expand All @@ -156,29 +159,38 @@ main() {

final result = builder.build();

expect(result, equals({'embeds': [{'description': 'test1'}, {'description': 'test2'}]}));
expect(
result,
equals({
'embeds': [
{'description': 'test1'},
{'description': 'test2'}
]
}));
});

test('text', () {
final dateTime = DateTime(2000);

final builder = MessageBuilder()
..appendSpoiler('spoiler')
..appendNewLine()
..appendItalics('italics')
..appendBold('bold')
..appendStrike('strike')
..appendCodeSimple('this is code simple')
..appendMention(MockMember(Snowflake.zero()))
..appendTimestamp(dateTime)
..appendCode('dart', 'final int = 124;');

expect(builder.build(), equals({
'content': '||spoiler||\n'
'*italics***bold**~~strike~~`this is code simple`<@0><t:${dateTime.millisecondsSinceEpoch ~/ 1000}:f>\n'
'```dart\n'
'final int = 124;```'
}));
..appendSpoiler('spoiler')
..appendNewLine()
..appendItalics('italics')
..appendBold('bold')
..appendStrike('strike')
..appendCodeSimple('this is code simple')
..appendMention(MockMember(Snowflake.zero()))
..appendTimestamp(dateTime)
..appendCode('dart', 'final int = 124;');

expect(
builder.build(),
equals({
'content': '||spoiler||\n'
'*italics***bold**~~strike~~`this is code simple`<@0><t:${dateTime.millisecondsSinceEpoch ~/ 1000}:f>\n'
'```dart\n'
'final int = 124;```'
}));

expect(builder.getMappedFiles(), isEmpty);
expect(builder.canBeUsedAsNewMessage(), isTrue);
Expand Down

0 comments on commit 8f8cdb2

Please sign in to comment.