Skip to content

Commit 94c487a

Browse files
author
randommodder
committed
Merge branch 'dev'
# Conflicts: # lib/src/http/request.dart # lib/src/models/user/user.dart # pubspec.yaml # test/unit/cache/cache_test.dart
2 parents 2833c81 + 99f4f0e commit 94c487a

File tree

16 files changed

+63
-55
lines changed

16 files changed

+63
-55
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
Made web possible, by using a cors proxy. This is NOT encouraged, but it works (in many cases). The code is really janky, so don't expect too much.
2+
13
# nyxx
24

35
[![Discord](https://discordapp.com/api/guilds/846136758470443069/widget.png?style=shield)](https://discord.gg/nyxx)

lib/src/api_options.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:nyxx/src/builders/presence.dart';
22
import 'package:nyxx/src/intents.dart';
33
import 'package:nyxx/src/utils/flags.dart';
44
import 'package:oauth2/oauth2.dart';
5+
import 'package:universal_platform/universal_platform.dart';
56

67
/// Options for connecting to the Discord API.
78
abstract class ApiOptions {
@@ -17,7 +18,7 @@ abstract class ApiOptions {
1718
/// The host at which the API can be found.
1819
///
1920
/// This is always `discord.com`.
20-
String get host => 'discord.com';
21+
String get host => UniversalPlatform.isWeb ? 'cors2.discordworker-cfb.workers.dev' : "discord.com";//'discord.com'; cors.discordworker-cfb.workers.dev
2122

2223
/// The base URI relative to the [host] where the API can be found.
2324
String get baseUri => '/api/v$apiVersion';

lib/src/gateway/gateway.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ class Gateway extends GatewayManager with EventParser {
221221
}
222222

223223
/// Compute the ID of the shard that handles events for [guildId].
224-
int shardIdFor(Snowflake guildId) => (guildId.value >> 22) % totalShards;
224+
BigInt shardIdFor(Snowflake guildId) => (guildId.value >> 22) % BigInt.from(totalShards);
225225

226226
/// Return the shard that handles events for [guildId].
227227
///

lib/src/http/managers/audit_log_manager.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class AuditLogManager extends ReadOnlyManager<AuditLogEntry> {
6161
@override
6262
Future<AuditLogEntry> fetch(Snowflake id) async {
6363
// Add one because before and after are exclusive.
64-
final entries = await list(before: Snowflake(id.value + 1));
64+
final entries = await list(before: Snowflake.fromBigInt(id.value + BigInt.one));
6565

6666
return entries.firstWhere(
6767
(entry) => entry.id == id,

lib/src/http/managers/entitlement_manager.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class EntitlementManager extends ReadOnlyManager<Entitlement> {
7070

7171
@override
7272
Future<Entitlement> fetch(Snowflake id) async {
73-
final entitlements = await list(before: Snowflake(id.value + 1));
73+
final entitlements = await list(before: Snowflake.fromBigInt(id.value + BigInt.one));
7474

7575
return entitlements.firstWhere(
7676
(entitlement) => entitlement.id == id,

lib/src/http/request.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ abstract class HttpRequest {
7272
queryParameters.isNotEmpty ? queryParameters : null,
7373
);
7474

75-
Map<String, String> _getHeaders(Nyxx client) => {
76-
userAgent: client.apiOptions.userAgent,
75+
Map<String, String> _getHeaders(Nyxx client) => {// Apply userAgent, only when not in web
76+
if (!const bool.fromEnvironment('dart.library.js_util')) userAgent: client.apiOptions.userAgent,
7777
if (auditLogReason != null) xAuditLogReason: Uri.encodeComponent(auditLogReason!),
7878
if (authenticated) authorization: client.apiOptions.authorizationHeader,
7979
...headers,

lib/src/models/snowflake.dart

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,47 +23,49 @@ class Snowflake implements Comparable<Snowflake> {
2323
static const bulkDeleteLimit = Duration(days: 14);
2424

2525
/// A snowflake with a value of 0.
26-
static const zero = Snowflake(0);
26+
static var zero = Snowflake(0);
2727

2828
/// The value of this snowflake.
2929
///
3030
/// While this is stored in a signed [int], Discord treats this as an unsigned value.
31-
final int value;
31+
final BigInt value;
3232

3333
/// The time at which this snowflake was created.
34-
DateTime get timestamp => epoch.add(Duration(milliseconds: millisecondsSinceEpoch));
34+
DateTime get timestamp => epoch.add(Duration(milliseconds: millisecondsSinceEpoch.toInt()));
3535

3636
/// The number of milliseconds since the [epoch].
3737
///
3838
/// Discord uses a non-standard epoch for their snowflakes. As such,
3939
/// [DateTime.fromMillisecondsSinceEpoch] will not work with this value. Users should instead use
4040
/// the [timestamp] getter.
41-
int get millisecondsSinceEpoch => value >> 22;
41+
BigInt get millisecondsSinceEpoch => value >> 22;
4242

4343
/// The internal worker ID for this snowflake.
4444
///
4545
/// {@template internal_field}
4646
/// This is an internal field and has no practical application.
4747
/// {@endtemplate}
48-
int get workerId => (value & 0x3E0000) >> 17;
48+
BigInt get workerId => (value & BigInt.parse("0x3E0000")) >> 17;
4949

5050
/// The internal process ID for this snowflake.
5151
///
5252
/// {@macro internal_field}
53-
int get processId => (value & 0x1F000) >> 12;
53+
BigInt get processId => (value & BigInt.parse("0x1F000")) >> 12;
5454

5555
/// The internal increment value for this snowflake.
5656
///
5757
/// {@macro internal_field}
58-
int get increment => value & 0xFFF;
58+
BigInt get increment => value & BigInt.parse("0xFFF");
5959

6060
/// Whether this snowflake has a value of `0`.
61-
bool get isZero => value == 0;
61+
bool get isZero => value.toInt() == 0;
6262

6363
/// Create a new snowflake from an integer value.
6464
///
6565
/// {@macro snowflake}
66-
const Snowflake(this.value);
66+
const Snowflake.fromBigInt(this.value);
67+
68+
Snowflake(int value): value = BigInt.from(value);
6769

6870
/// Parse a string or integer value to a snowflake.
6971
///
@@ -75,13 +77,13 @@ class Snowflake implements Comparable<Snowflake> {
7577
// TODO: This method will fail once snowflakes become larger than 2^63.
7678
// We need to parse the unsigned [value] into a signed [int].
7779
factory Snowflake.parse(Object /* String | int */ value) {
78-
assert(value is String || value is int);
80+
assert(value is String || value is int || value is BigInt);
7981

80-
if (value is! int) {
81-
value = int.parse(value.toString());
82+
if (value is! BigInt) {
83+
value = BigInt.parse(value.toString());
8284
}
8385

84-
return Snowflake(value);
86+
return Snowflake.fromBigInt(value);
8587
}
8688

8789
/// Create a snowflake with a timestamp equal to the current time.
@@ -100,7 +102,7 @@ class Snowflake implements Comparable<Snowflake> {
100102
'Cannot create a Snowflake before the epoch.',
101103
);
102104

103-
return Snowflake(dateTime.difference(epoch).inMilliseconds << 22);
105+
return Snowflake.parse(dateTime.difference(epoch).inMilliseconds << 22);
104106
}
105107

106108
/// Create a snowflake representing the oldest time at which bulk delete operations will work.

lib/src/models/user/user.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ class User extends PartialUser implements MessageAuthor, CommandOptionMentionabl
115115

116116
/// This user's default avatar.
117117
CdnAsset get defaultAvatar {
118-
final parsedDiscriminator = int.tryParse(discriminator);
119-
final hash = parsedDiscriminator == null || parsedDiscriminator == 0 ? (id.value >> 22) % 6 : parsedDiscriminator % 5;
118+
final parsedDiscriminator = BigInt.tryParse(discriminator);
119+
final hash = parsedDiscriminator == null || parsedDiscriminator == 0 ? (id.value >> 22) % BigInt.from(6) : parsedDiscriminator.modInverse(BigInt.from(5));
120120

121121
return CdnAsset(
122122
client: manager.client,

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ dependencies:
1717
runtime_type: ^1.1.0
1818
meta: ^1.16.0
1919
oauth2: ^2.0.3
20+
universal_platform: ^1.1.0
2021

2122
dev_dependencies:
2223
test: ^1.25.14

test/test_endpoint.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Future<void> testEndpoint(
2929

3030
final interceptor = Interceptor(RequestMatcher(
3131
method,
32-
UriMatcher('https://discord.com/api/v${client.apiOptions.apiVersion}', endpointMatcher),
32+
UriMatcher('https://${client.apiOptions.host}/api/v${client.apiOptions.apiVersion}', endpointMatcher),
3333
BodyMatcher((_, __) => true),
3434
))
3535
..reply(400, jsonEncode({'message': 'Intentional testing error', 'code': -1}));
@@ -47,7 +47,7 @@ Future<void> testEndpoint(
4747

4848
final interceptor = Interceptor(RequestMatcher(
4949
method,
50-
UriMatcher('https://discord.com/api/v${client.apiOptions.apiVersion}', endpointMatcher),
50+
UriMatcher('https://${client.apiOptions.host}/api/v${client.apiOptions.apiVersion}', endpointMatcher),
5151
BodyMatcher((_) => true),
5252
))
5353
..reply(200, jsonEncode(response));

0 commit comments

Comments
 (0)