-
Notifications
You must be signed in to change notification settings - Fork 34
/
Program.cs
59 lines (50 loc) · 2.55 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
using System;
using System.Threading.Tasks;
using DSharpPlus.Entities;
namespace DSharpPlus.ExampleBots.Core.HelloWorld
{
// We're sealing it because nothing will be inheriting this class
public sealed class Program
{
// Remember to make your main method async! You no longer need to have both a Main and MainAsync method in the same class.
public static async Task Main()
{
// For the sake of examples, we're going to load our Discord token from an environment variable.
string? token = Environment.GetEnvironmentVariable("DISCORD_TOKEN");
if (string.IsNullOrWhiteSpace(token))
{
Console.WriteLine("Please specify a token in the DISCORD_TOKEN environment variable.");
Environment.Exit(1);
// For the compiler's nullability, unreachable code.
return;
}
// Next, we instantiate our client.
DiscordConfiguration config = new()
{
Token = token,
// We're asking for unprivileged intents, which means we won't receive any member or presence updates.
// Privileged intents must be enabled in the Discord Developer Portal.
// TODO: Enable the message content intent in the Discord Developer Portal.
// The !ping command will not work without it.
Intents = DiscordIntents.AllUnprivileged | DiscordIntents.MessageContents
};
DiscordClient client = new(config);
// Register a simple ping command
client.MessageCreated += async (client, eventArgs) =>
{
// We use StringComparison.OrdinalIgnoreCase for case-insensitive matching.
if (eventArgs.Message.Content.Equals("!ping", StringComparison.OrdinalIgnoreCase))
{
// Respond to the message with "Pong!"
await eventArgs.Message.RespondAsync($"Pong! The gateway latency is {client.Ping}ms.");
}
};
// We can specify a status for our bot. Let's set it to "online" and set the activity to "with fire".
DiscordActivity status = new("with fire", ActivityType.Playing);
// Now we connect and log in.
await client.ConnectAsync(status, UserStatus.Online);
// And now we wait infinitely so that our bot actually stays connected.
await Task.Delay(-1);
}
}
}