Add sample bots to the solution (#972)
* Add sample bots to the solution * Fix missing attributes, show use of preconditions
This commit is contained in:
69
samples/01_basic_ping_bot/Program.cs
Normal file
69
samples/01_basic_ping_bot/Program.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
|
||||
namespace _01_basic_ping_bot
|
||||
{
|
||||
// This is a minimal, barebones example of using Discord.Net
|
||||
//
|
||||
// If writing a bot with commands, we recommend using the Discord.Net.Commands
|
||||
// framework, rather than handling commands yourself, like we do in this sample.
|
||||
//
|
||||
// You can find samples of using the command framework:
|
||||
// - Here, under the 02_commands_framework sample
|
||||
// - https://github.com/foxbot/DiscordBotBase - a barebones bot template
|
||||
// - https://github.com/foxbot/patek - a more feature-filled bot, utilizing more aspects of the library
|
||||
class Program
|
||||
{
|
||||
private DiscordSocketClient _client;
|
||||
|
||||
// Discord.Net heavily utilizes TAP for async, so we create
|
||||
// an asynchronous context from the beginning.
|
||||
static void Main(string[] args)
|
||||
=> new Program().MainAsync().GetAwaiter().GetResult();
|
||||
|
||||
public async Task MainAsync()
|
||||
{
|
||||
_client = new DiscordSocketClient();
|
||||
|
||||
_client.Log += LogAsync;
|
||||
_client.Ready += ReadyAsync;
|
||||
_client.MessageReceived += MessageReceivedAsync;
|
||||
|
||||
// Tokens should be considered secret data, and never hard-coded.
|
||||
await _client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("token"));
|
||||
await _client.StartAsync();
|
||||
|
||||
// Block the program until it is closed.
|
||||
await Task.Delay(-1);
|
||||
}
|
||||
|
||||
private Task LogAsync(LogMessage log)
|
||||
{
|
||||
Console.WriteLine(log.ToString());
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
// The Ready event indicates that the client has opened a
|
||||
// connection and it is now safe to access the cache.
|
||||
private Task ReadyAsync()
|
||||
{
|
||||
Console.WriteLine($"{_client.CurrentUser} is connected!");
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
// This is not the recommmended way to write a bot - consider
|
||||
// reading over the Commands Framework sample.
|
||||
private async Task MessageReceivedAsync(SocketMessage message)
|
||||
{
|
||||
// The bot should never respond to itself.
|
||||
if (message.Author.Id == _client.CurrentUser.Id)
|
||||
return;
|
||||
|
||||
if (message.Content == "!ping")
|
||||
await message.Channel.SendMessageAsync("pong!");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user