Skip to content

Commit

Permalink
Update to nyxx 6.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
abitofevrything committed Sep 10, 2023
1 parent 93d2c54 commit fe8be9c
Show file tree
Hide file tree
Showing 21 changed files with 397 additions and 691 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
env:
TEST_TOKEN: ${{ secrets.TEST_TOKEN }}
steps:
- name: Setup Dart Action
uses: dart-lang/setup-dart@v1
Expand All @@ -25,6 +23,7 @@ jobs:
key: ${{ runner.os }}-pubspec-${{ hashFiles('**/pubspec.lock') }}
restore-keys: |
${{ runner.os }}-pubspec-
- name: Install dependencies
run: dart pub get

Expand All @@ -34,8 +33,6 @@ jobs:
format:
name: Format
runs-on: ubuntu-latest
env:
TEST_TOKEN: ${{ secrets.TEST_TOKEN }}
steps:
- name: Setup Dart Action
uses: dart-lang/setup-dart@v1
Expand All @@ -50,6 +47,7 @@ jobs:
key: ${{ runner.os }}-pubspec-${{ hashFiles('**/pubspec.lock') }}
restore-keys: |
${{ runner.os }}-pubspec-
- name: Install dependencies
run: dart pub get

Expand All @@ -62,6 +60,7 @@ jobs:
runs-on: ubuntu-latest
env:
TEST_TOKEN: ${{ secrets.TEST_TOKEN }}
TEST_GUILD: ${{ secrets.TEST_GUILD }}
steps:
- name: Setup Dart Action
uses: dart-lang/setup-dart@v1
Expand All @@ -81,4 +80,4 @@ jobs:
run: dart pub get

- name: Unit tests
run: dart run test --coverage="coverage" test/unit/**
run: dart run test --coverage="coverage"
1 change: 0 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ linter:
implementation_imports: false

analyzer:
exclude: [build/**, example/**]
language:
strict-raw-types: true
strong-mode:
Expand Down
42 changes: 22 additions & 20 deletions example/example.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import "dart:io";

import "package:nyxx/nyxx.dart";
import "package:nyxx_extensions/emoji.dart" as emoji_extension;
import "package:nyxx_extensions/src/message_resolver/message_resolver.dart" as message_resolver_extension;
import "package:nyxx_extensions/nyxx_extensions.dart";

// nyxx.extensions contains several different extensions
// that could simplify making and implementing bots.
void main() async {
// Emoji extension would allow to download and fetch discord emoji definitions
// from resource shared by Emzi.
// Emoji utils can cache results to do not download json document each time
final allEmojis = emoji_extension.getAllEmojiDefinitions();
final client = await Nyxx.connectGateway(Platform.environment['TOKEN']!, GatewayIntents.guildMessages | GatewayIntents.messageContent);

// Get an emoji by its unicode character...
final heartEmoji = client.getTextEmoji('❤️');

// Its also possible to filter the emojis based on predicate
final filteredEmojis = emoji_extension.filterEmojiDefinitions(
(emojiDefinition) => emojiDefinition.primaryName.startsWith("smile")
);
// ...or list all available emojis
final allEmojis = await client.getTextEmojis();
print('There are currently ${allEmojis.length} emojis!');

// Needed for next extension
final bot = Nyxx("token", GatewayIntents.allUnprivileged);
// Get information about a text emoji!
final heartEmojiInformation = await heartEmoji.getDefinition();
print('The primary name of ${heartEmojiInformation.surrogates} is ${heartEmojiInformation.primaryName}');

// Message Resolver extension allows to transform raw string message content
// to format that user is seeing
final messageResolver = message_resolver_extension.MessageResolver(bot);
// Sanitizing content makes it safe to send to Discord without triggering any mentions
client.onMessageCreate.listen((event) async {
if (event.message.content.startsWith('!sanitize')) {
event.message.channel.sendMessage(MessageBuilder(
content: 'Sanitized content: ${await sanitizeContent(event.message.content, channel: event.message.channel)}',
));
}
});

// resolve method will return message according to set handling settings in
// MessageResolver constructor.
final resolvedMessage = messageResolver.resolve("This is raw content. <!@123>");
// ...and more!
}
12 changes: 4 additions & 8 deletions lib/nyxx_extensions.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
library nyxx_extensions;

export 'src/attachment_extension.dart';
export 'src/embed_builder_extension.dart';
export 'src/emoji/emoji_definition.dart';
export 'src/emoji/emoji_utils.dart';
export 'src/member_extension.dart';
export 'src/message_resolver/message_resolver.dart';
export 'src/utils.dart';
export 'src/guild_extension.dart';
export 'src/emoji.dart';
export 'src/user.dart';
export 'src/sanitizer.dart';
export 'src/guild.dart';
18 changes: 0 additions & 18 deletions lib/src/attachment_extension.dart

This file was deleted.

55 changes: 0 additions & 55 deletions lib/src/embed_builder_extension.dart

This file was deleted.

102 changes: 102 additions & 0 deletions lib/src/emoji.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:nyxx/nyxx.dart';

import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart';
import 'package:nyxx/src/utils/parsing_helpers.dart';

/// {@template emoji_definition}
/// Information about a text emoji.
/// {@endtemplate}
class EmojiDefinition with ToStringHelper {
/// The primary name of this emoji.
final String primaryName;

/// A list of all the names of this emoji.
final List<String> names;

/// The surrogates (string) that make up this emoji.
final String surrogates;

/// The UTF-32 codepoints that make up this emoji.
final List<int> utf32Codepoints;

/// The filename of the asset containing this emoji's image.
final String assetFilename;

/// The URI to this emoji's asset image.
final Uri assetUrl;

/// The category this emoji belongs to.
final String category;

/// An alternate representation of this emoji.
final String? alternateSurrogates;

/// Alternate UTF-32 codepoints for this emoji.
final List<int>? alternateUtf32Codepoints;

/// {@macro emoji_definition}
EmojiDefinition({
required this.primaryName,
required this.names,
required this.surrogates,
required this.utf32Codepoints,
required this.assetFilename,
required this.assetUrl,
required this.category,
required this.alternateSurrogates,
required this.alternateUtf32Codepoints,
});
}

/// Extensions relating to [EmojiDefinition]s on [TextEmoji].
extension TextEmojiDefinition on TextEmoji {
/// Get the definition of this emoji.
Future<EmojiDefinition> getDefinition() async =>
(await getEmojiDefinitions()).singleWhere((definition) => definition.surrogates == name || definition.alternateSurrogates == name);
}

/// Extensions relating to [EmojiDefinition]s on [NyxxRest].
extension NyxxEmojiDefinitions on NyxxRest {
/// List all the text emoji available to this client.
Future<List<TextEmoji>> getTextEmojis() async => (await getEmojiDefinitions())
.map((definition) => TextEmoji(id: Snowflake.zero, manager: guilds[Snowflake.zero].emojis, name: definition.surrogates))
.toList();

/// Get a text emoji by name.
TextEmoji getTextEmoji(String name) => TextEmoji(id: Snowflake.zero, manager: guilds[Snowflake.zero].emojis, name: name);
}

final _emojiDefinitionsUrl = Uri.parse("https://emzi0767.gl-pages.emzi0767.dev/discord-emoji/discordEmojiMap.min.json");
List<EmojiDefinition>? _cachedEmojiDefinitions;
DateTime? _cachedAt;

/// List all the emoji definitions currently available.
///
/// This method caches results for 4 hours.
Future<List<EmojiDefinition>> getEmojiDefinitions() async {
if (_cachedEmojiDefinitions != null && _cachedAt!.add(Duration(hours: 4)).isAfter(DateTime.timestamp())) {
return _cachedEmojiDefinitions!;
} else {
final response = await http.get(_emojiDefinitionsUrl);
final data = jsonDecode(response.body)['emojiDefinitions'];

_cachedAt = DateTime.timestamp();
return _cachedEmojiDefinitions = [
for (final raw in data)
EmojiDefinition(
primaryName: raw['primaryName'] as String,
names: parseMany(raw['names']),
surrogates: raw['surrogates'] as String,
utf32Codepoints: parseMany(raw['utf32codepoints']),
assetFilename: raw['assetFileName'] as String,
assetUrl: Uri.parse(raw['assetUrl']),
category: raw['category'] as String,
alternateSurrogates: raw['alternativeSurrogates'] as String?,
alternateUtf32Codepoints: maybeParseMany(raw['alternativeUtf32codepoints']),
),
];
}
}
34 changes: 0 additions & 34 deletions lib/src/emoji/emoji_definition.dart

This file was deleted.

42 changes: 0 additions & 42 deletions lib/src/emoji/emoji_utils.dart

This file was deleted.

2 changes: 1 addition & 1 deletion lib/src/guild_extension.dart → lib/src/guild.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:nyxx/nyxx.dart';

extension GuildExtension on IGuild {
extension GuildExtension on Guild {
/// The acronym of the guild if no icon is chosen.
String get acronym {
return name.replaceAll(r"'s ", ' ').replaceAllMapped(RegExp(r'\w+'), (match) => match[0]![0]).replaceAll(RegExp(r'\s'), '');
Expand Down
Loading

0 comments on commit fe8be9c

Please sign in to comment.