-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
93 lines (75 loc) · 2.99 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Discord;
using Discord.WebSocket;
using DotNetEnv;
namespace DiscordPingBot
{
class Program
{
private readonly DiscordSocketClient _client;
private readonly string _token;
public Program()
{
// Load environment variables from .env file
Env.Load();
// Retrieve the bot token from environment variables
_token = Env.GetString("DISCORD_BOT_TOKEN");
var config = new DiscordSocketConfig
{
LogLevel = LogSeverity.Info,
GatewayIntents = GatewayIntents.Guilds | GatewayIntents.GuildMessages | GatewayIntents.MessageContent
};
_client = new DiscordSocketClient(config);
_client.Log += LogAsync;
_client.Ready += ReadyAsync;
_client.MessageReceived += MessageReceivedAsync;
}
public static Task Main(string[] args) => new Program().MainAsync();
public async Task MainAsync()
{
await _client.LoginAsync(TokenType.Bot, _token);
await _client.StartAsync();
await Task.Delay(-1);
}
private Task LogAsync(LogMessage log)
{
Console.WriteLine(log.ToString());
return Task.CompletedTask;
}
private Task ReadyAsync()
{
Console.WriteLine($"{_client.CurrentUser} is connected and ready!");
return Task.CompletedTask;
}
private async Task MessageReceivedAsync(SocketMessage message)
{
if (message.Author.IsBot) return;
Console.WriteLine($"Received message from {message.Author}: {message.Content}");
if (message.Content == "!ping")
{
Console.WriteLine("Ping command received.");
var stopwatch = Stopwatch.StartNew();
// Measure bot processing time
stopwatch.Stop();
var processingTime = stopwatch.ElapsedMilliseconds;
Console.WriteLine($"Bot processing time: {processingTime} ms");
// Measure response send time
stopwatch.Restart();
var responseMessage = await message.Channel.SendMessageAsync("Pong!");
stopwatch.Stop();
var responseTime = stopwatch.ElapsedMilliseconds;
Console.WriteLine($"Response send time: {responseTime} ms");
// Log Discord API latency
var apiLatency = _client.Latency;
Console.WriteLine($"Discord API latency: {apiLatency} ms");
// Send a detailed response
await responseMessage.ModifyAsync(msg => msg.Content = $"Pong!\n" +
$"Bot processing time: {processingTime} ms\n" +
$"Response send time: {responseTime} ms\n" +
$"Discord API latency: {apiLatency} ms");
}
}
}
}