-
Notifications
You must be signed in to change notification settings - Fork 2
Home
Ethan Lafrenais edited this page Feb 9, 2017
·
22 revisions
Pre-built releases can be downloaded via NuGet here.
If you want to work with the latest unreleased version of Discore you will need:
OR
- Visual Studio 2017 RC with the ".NET Core and Docker" workload
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... :( */ }
}
}
}
}