selene 0.3.2
selene: ^0.3.2 copied to clipboard
A Discord client for Dart
example/selene_example.dart
import 'dart:async';
import 'package:logging/logging.dart';
import 'package:selene/selene.dart' as discord;
void main() {
runBot();
}
Future runBot() async {
// Create our client instance
var client = new discord.Client();
// Listen for log events
Logger.root.onRecord.listen((LogRecord record) {
print('[${record.loggerName}] ${record.level.name}: ${record.time}: ${record
.message}');
});
// Event Guidelines
// - You MUST catch exceptions thrown in your event handlers. Due to Dart limitations,
// the library CANNOT catch exceptions thrown from stream subscriptions.
// If you don't catch them, they will KILL the WebSocket connection and the bot
// will go offline!
// Listen to the message created event
client.dispatcher.onMessageReceive.listen((discord.Message message) async {
if (message.content == '!ping') {
// Pong
await message.channel.sendMessage(
content:
'Pong!'); // sendMessage takes either content, embed or both. At least one must be supplied.
}
if (message.content == '!whoareyou' || message.content == '!bot') {
var bot = client.state.currentUser; // Get the client's current user
// Create an embed
var embed = new discord.EmbedBuilder();
embed.author = new discord.EmbedAuthorBuilder.fromUser(
bot); // Fill in properties from user
embed.description =
"Hello there! I am ${bot.username}#${bot.discriminator}!";
await message.channel
.sendMessage(embed: embed); // Construct embed
}
});
// Login and authorize
client.login(
'MyToken', /* type: 'Bot' -- This parameter is optional and defaults to Bot. */
);
// Initiate WebSocket connection
await client.start();
// Client will automatically block the thread
}