-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93d2c54
commit fe8be9c
Showing
21 changed files
with
397 additions
and
691 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']), | ||
), | ||
]; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.