-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
111 lines (90 loc) · 2.63 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System.Collections.Concurrent;
using System.Reflection;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.DependencyInjection;
using Minomuncher;
internal class Program
{
private DiscordSocketClient _client;
private CommandService _commands;
private IServiceProvider _services;
public static ulong DISCORD_BOT_ADMIN { get; private set; }
ConcurrentDictionary<ulong, DateTime> _rateLimitHistory = new();
private static void Main(string[] args)
=> new Program().MainAsync().Wait();
private async Task MainAsync()
{
DotNetEnv.Env.Load("./.env");
await TetrioAPI.Initialize();
DISCORD_BOT_ADMIN = ulong.Parse(DotNetEnv.Env.GetString("DISCORD_BOT_ADMIN"));
var config = new DiscordSocketConfig
{
GatewayIntents = GatewayIntents.AllUnprivileged | GatewayIntents.MessageContent
};
_client = new DiscordSocketClient(config);
_commands = new CommandService();
_services = new ServiceCollection()
.AddSingleton(_client)
.AddSingleton(_commands)
.BuildServiceProvider();
var token = DotNetEnv.Env.GetString("DISCORD_API");
_client.Log += Client_Log;
await _client.LoginAsync(TokenType.Bot, token);
await _client.StartAsync();
await RegisterCommandsAsync();
_client.Ready += Client_Ready;
await Task.Delay(-1);
}
public async Task RegisterCommandsAsync()
{
_client.MessageReceived += HandleCommandAsync;
await _commands.AddModulesAsync(assembly: Assembly.GetEntryAssembly(),
services: null);
}
private async Task HandleCommandAsync(SocketMessage messageParam)
{
Task.Run(async () =>
{
var message = messageParam as SocketUserMessage;
if (message == null) return;
int argPos = 0;
if (!(message.HasCharPrefix('!', ref argPos) &&
!message.Author.IsBot))
return;
var context = new SocketCommandContext(_client, message);
if (_rateLimitHistory.TryGetValue(context.User.Id, out var value))
{
var diff = DateTime.Now - value;
if (Math.Abs(diff.TotalSeconds) < 2)
{
await context.Message.ReplyAsync("You are being rate limited, try again later");
return;
}
else
{
_rateLimitHistory.AddOrUpdate(context.User.Id, DateTime.Now, (Key, Value) => Value);
}
}
else
{
_rateLimitHistory.AddOrUpdate(context.User.Id, DateTime.Now, (Key, Value) => Value);
}
await _commands.ExecuteAsync(
context: context,
argPos: argPos,
services: _services);
});
}
private Task Client_Ready()
{
Console.WriteLine("Bot is connected!");
return Task.CompletedTask;
}
private Task Client_Log(LogMessage arg)
{
Console.WriteLine(arg.ToString());
return Task.CompletedTask;
}
}