forked from gngrninja/csharpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
79 lines (64 loc) · 2.39 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using Discord;
using Discord.Net;
using Discord.Commands;
using Discord.WebSocket;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
namespace csharpi
{
class Program
{
private readonly DiscordSocketClient _client;
private readonly IConfiguration _config;
public static Task Main(string[] args) => new Program().MainAsync();
public async Task MainAsync(string[] args)
{
}
public Program()
{
_client = new DiscordSocketClient();
//Hook into log event and write it out to the console
_client.Log += Log;
//Hook into the client ready event
_client.Ready += Ready;
//Hook into the message received event, this is how we handle the hello world example
_client.MessageReceived += MessageReceivedAsync;
//Create the configuration
var _builder = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory)
.AddJsonFile(path: "config.json");
_config = _builder.Build();
}
public async Task MainAsync()
{
//This is where we get the Token value from the configuration file
await _client.LoginAsync(TokenType.Bot, _config["Token"]);
await _client.StartAsync();
// Block the program until it is closed.
await Task.Delay(-1);
}
private Task Log(LogMessage log)
{
Console.WriteLine(log.ToString());
return Task.CompletedTask;
}
private Task Ready()
{
Console.WriteLine($"Connected as -> [{_client.CurrentUser}] :)");
return Task.CompletedTask;
}
//I wonder if there's a better way to handle commands (spoiler: there is :))
private async Task MessageReceivedAsync(SocketMessage message)
{
//This ensures we don't loop things by responding to ourselves (as the bot)
if (message.Author.Id == _client.CurrentUser.Id)
return;
if (message.Content == ".hello")
{
await message.Channel.SendMessageAsync("world!");
}
}
}
}