-
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
Showing
41 changed files
with
1,303 additions
and
767 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 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
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,71 @@ | ||
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(); | ||
|
||
// Its also possible to filter the emojis based on predicate | ||
final filteredEmojis = emoji_extension.filterEmojiDefinitions( | ||
(emojiDefinition) => emojiDefinition.primaryName.startsWith("smile") | ||
final client = await Nyxx.connectGateway( | ||
Platform.environment['TOKEN']!, | ||
GatewayIntents.guildMessages | GatewayIntents.messageContent, | ||
options: GatewayClientOptions(plugins: [logging, cliIntegration, pagination]), | ||
); | ||
|
||
// Needed for next extension | ||
final bot = Nyxx("token", GatewayIntents.allUnprivileged); | ||
// Get an emoji by its unicode character... | ||
final heartEmoji = client.getTextEmoji('❤️'); | ||
|
||
// ...or list all available emojis | ||
final allEmojis = await client.getTextEmojis(); | ||
print('There are currently ${allEmojis.length} emojis!'); | ||
|
||
// Get information about a text emoji! | ||
final heartEmojiInformation = await heartEmoji.getDefinition(); | ||
print('The primary name of ${heartEmojiInformation.surrogates} is ${heartEmojiInformation.primaryName}'); | ||
|
||
// 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)}', | ||
)); | ||
} | ||
}); | ||
|
||
// Pagination allows for long segments of text to be sent as one chunk. | ||
const loreumIpsum = ''' | ||
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor | ||
incididunt ut labore et dolore magna aliqua. Donec massa sapien faucibus et. Et | ||
malesuada fames ac turpis egestas maecenas pharetra convallis. Vulputate eu | ||
scelerisque felis imperdiet proin fermentum leo. Amet risus nullam eget felis | ||
eget nunc lobortis mattis aliquam. Id leo in vitae turpis. Adipiscing elit | ||
pellentesque habitant morbi tristique. Adipiscing commodo elit at imperdiet | ||
dui. Ridiculus mus mauris vitae ultricies leo. Sapien pellentesque habitant | ||
morbi tristique senectus. Mauris pellentesque pulvinar pellentesque habitant. | ||
Mus mauris vitae ultricies leo integer malesuada. Sit amet est placerat in | ||
egestas erat. Id leo in vitae turpis massa sed elementum tempus egestas. | ||
Posuere sollicitudin aliquam ultrices sagittis orci a scelerisque purus. | ||
Sagittis orci a scelerisque purus semper eget. Nisl purus in mollis nunc. | ||
Curabitur vitae nunc sed velit. At lectus urna duis convallis convallis tellus | ||
id. Risus nec feugiat in fermentum posuere urna nec. At elementum eu facilisis | ||
sed odio morbi quis. Est ante in nibh mauris. Dictumst quisque sagittis purus | ||
sit amet volutpat consequat. Quis imperdiet massa tincidunt nunc pulvinar | ||
sapien. Viverra tellus in hac habitasse platea dictumst vestibulum. Eu | ||
consequat ac felis donec et. Mauris a diam maecenas sed enim ut sem. Placerat | ||
in egestas erat imperdiet sed. Orci eu lobortis elementum nibh tellus molestie | ||
nunc non blandit. Rutrum tellus pellentesque eu tincidunt tortor aliquam. | ||
Imperdiet proin fermentum leo vel orci porta non. Ullamcorper velit sed | ||
ullamcorper morbi tincidunt ornare. | ||
'''; | ||
|
||
// Message Resolver extension allows to transform raw string message content | ||
// to format that user is seeing | ||
final messageResolver = message_resolver_extension.MessageResolver(bot); | ||
client.onMessageCreate.listen((event) async { | ||
if (event.message.content.startsWith('!pages')) { | ||
await event.message.channel.sendMessage(await pagination.split( | ||
loreumIpsum, | ||
// Split into chunks 100 characters long. | ||
maxLength: 100, | ||
)); | ||
} | ||
}); | ||
|
||
// 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,9 +1,13 @@ | ||
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 'guild_extension.dart'; | ||
export 'src/channel.dart'; | ||
export 'src/date_time.dart'; | ||
export 'src/embed_builder.dart'; | ||
export 'src/emoji.dart'; | ||
export 'src/guild.dart'; | ||
export 'src/message.dart'; | ||
export 'src/pagination.dart'; | ||
export 'src/role.dart'; | ||
export 'src/sanitizer.dart'; | ||
export 'src/user.dart'; | ||
export 'src/utils/formatters.dart'; |
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,12 @@ | ||
import 'package:nyxx/nyxx.dart'; | ||
import 'package:nyxx_extensions/src/utils/formatters.dart'; | ||
|
||
extension PartialChannelUtils on PartialChannel { | ||
/// A mention of this channel. | ||
String get mention => channelMention(id); | ||
} | ||
|
||
extension ChannelUtils on Channel { | ||
/// A URL clients can visit to navigate to this channel. | ||
Uri get url => Uri.https(manager.client.apiOptions.host, '/channels/${this is GuildChannel ? '${(this as GuildChannel).guildId}' : '@me'}/$id'); | ||
} |
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,12 @@ | ||
import 'package:nyxx_extensions/src/utils/formatters.dart'; | ||
|
||
extension TimestampStyleDateTime on DateTime { | ||
/// Formats the [DateTime] into a date string timestamp. | ||
String format([TimestampStyle style = TimestampStyle.none]) => formatDate(this, style); | ||
} | ||
|
||
extension TimestampStyleDuration on Duration { | ||
/// Formats the [Duration] into a date string timestamp. | ||
/// The style will always be relative to represent as a duration on Discord. | ||
String format() => formatDate(DateTime.now().add(this), TimestampStyle.relativeTime); | ||
} |
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,38 @@ | ||
import 'package:nyxx/nyxx.dart'; | ||
|
||
extension EmbedExtension on Embed { | ||
EmbedBuilder toEmbedBuilder() { | ||
return EmbedBuilder( | ||
author: author?._toEmbedAuthorBuilder(), | ||
color: color, | ||
description: description, | ||
fields: fields?.map((field) => field._toEmbedFieldBuilder()).toList(), | ||
footer: footer?._toEmbedFooterBuilder(), | ||
image: image?._toEmbedImageBuilder(), | ||
thumbnail: thumbnail?._toEmbedThumbnailBuilder(), | ||
timestamp: timestamp, | ||
title: title, | ||
url: url, | ||
); | ||
} | ||
} | ||
|
||
extension on EmbedAuthor { | ||
EmbedAuthorBuilder _toEmbedAuthorBuilder() => EmbedAuthorBuilder(name: name, iconUrl: iconUrl, url: url); | ||
} | ||
|
||
extension on EmbedField { | ||
EmbedFieldBuilder _toEmbedFieldBuilder() => EmbedFieldBuilder(name: name, value: value, isInline: inline); | ||
} | ||
|
||
extension on EmbedFooter { | ||
EmbedFooterBuilder _toEmbedFooterBuilder() => EmbedFooterBuilder(text: text, iconUrl: iconUrl); | ||
} | ||
|
||
extension on EmbedImage { | ||
EmbedImageBuilder _toEmbedImageBuilder() => EmbedImageBuilder(url: url); | ||
} | ||
|
||
extension on EmbedThumbnail { | ||
EmbedThumbnailBuilder _toEmbedThumbnailBuilder() => EmbedThumbnailBuilder(url: url); | ||
} |
Oops, something went wrong.