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

Update to nyxx 6.0.0 #10

Merged
merged 9 commits into from
Sep 10, 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
3 changes: 1 addition & 2 deletions .github/workflows/deploy_docs.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

name: deploy dev docs

on:
Expand Down Expand Up @@ -30,7 +29,7 @@ jobs:
run: dart pub get

- name: Generate docs
run: dartdoc
run: dart doc

- name: Extract branch name
shell: bash
Expand Down
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"
39 changes: 0 additions & 39 deletions .pubignore

This file was deleted.

5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.2.0

- Bump nyxx to `4.2.0`
- Correctly export `acronym` property on guild

## 3.1.0
__15.11.2022__

Expand Down
5 changes: 1 addition & 4 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ include: package:lints/recommended.yaml

linter:
rules:
unrelated_type_equality_checks: false
implementation_imports: false

analyzer:
exclude: [build/**, example/**]
language:
strict-raw-types: true
strong-mode:
implicit-casts: false
strict-casts: false
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!
}
3 changes: 0 additions & 3 deletions lib/attachment_extension.dart

This file was deleted.

3 changes: 0 additions & 3 deletions lib/embed_builder_extension.dart

This file was deleted.

11 changes: 0 additions & 11 deletions lib/emoji.dart

This file was deleted.

3 changes: 0 additions & 3 deletions lib/member_extension.dart

This file was deleted.

4 changes: 0 additions & 4 deletions lib/message_resolver.dart

This file was deleted.

10 changes: 4 additions & 6 deletions lib/nyxx_extensions.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
library nyxx_extensions;

export 'attachment_extension.dart';
export 'embed_builder_extension.dart';
export 'emoji.dart';
export 'member_extension.dart';
export 'message_resolver.dart';
export 'utils.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'] as List),
surrogates: raw['surrogates'] as String,
utf32Codepoints: parseMany(raw['utf32codepoints'] as List),
assetFilename: raw['assetFileName'] as String,
assetUrl: Uri.parse(raw['assetUrl']),
category: raw['category'] as String,
alternateSurrogates: raw['alternativeSurrogates'] as String?,
alternateUtf32Codepoints: maybeParseMany(raw['alternativeUtf32codepoints']),
),
];
}
}
Loading
Loading