-
Notifications
You must be signed in to change notification settings - Fork 2
Home
Ethan Lafrenais edited this page Mar 30, 2017
·
22 revisions
Pre-built releases can be downloaded via NuGet here.
The project can be built with Visual Studio 2017 (e.g. via the ".NET Core 1.0 - 1.1 development tools").
If you wish to test your Discore installation, try this example bot. Just enter your bot's user token for the token
variable and fire away!
using Discore;
using Discore.WebSocket;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace DiscorePingPong
{
public class Program
{
public static void Main(string[] args)
{
Program program = new Program();
program.Run().Wait();
}
public async Task Run()
{
// Create authenticator using a bot user token.
DiscordBotUserToken token = new DiscordBotUserToken("<bot user token goes here>");
// Create a WebSocket application.
DiscordWebSocketApplication app = new DiscordWebSocketApplication(token);
// Create and start a single shard.
Shard shard = app.ShardManager.CreateSingleShard();
await shard.StartAsync(CancellationToken.None);
// Subscribe to the message creation event.
shard.Gateway.OnMessageCreated += Gateway_OnMessageCreated;
// Wait for the shard to end before closing the program.
while (shard.IsRunning)
await Task.Delay(1000);
}
private static async void Gateway_OnMessageCreated(object sender, MessageEventArgs e)
{
Shard shard = e.Shard;
DiscordMessage message = e.Message;
if (message.Author == shard.User)
// Ignore messages created by our bot.
return;
if (message.Content == "!ping")
{
// Grab the DM or guild text channel this message was posted in from cache.
ITextChannel textChannel = (ITextChannel)shard.Cache.Channels.Get(message.ChannelId);
try
{
// Reply to the user who posted "!ping".
await textChannel.SendMessage($"<@{message.Author.Id}> Pong!");
}
catch (Exception) { /* Message failed to send... :( */ }
}
}
}
}