-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
218 lines (193 loc) · 8.16 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using Discord;
using Discord.WebSocket;
using HLTVDiscordBridge.Modules;
using HLTVDiscordBridge.Shared;
using MongoDB.Driver;
using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using System.Timers;
using Discord.Net;
using HLTVDiscordBridge.Repository;
using HLTVDiscordBridge.Requests;
namespace HLTVDiscordBridge;
internal class Program
{
private static async Task Main()
{
await GetInstance().Start();
await Task.Delay(-1);
}
private static Program _instance;
public DiscordSocketClient Client { get; }
private readonly BotConfig _botConfig;
public static HttpClient DefaultHttpClient { get; } = new();
private Task _bgTask;
//TODO after updating to v4 this needs to be deleted since it's required for the webhook migration.
public static MongoClient DbClient { get; } = new(BotConfig.GetBotConfig().DatabaseLink);
public static readonly JsonSerializerOptions SerializeOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
};
private Program()
{
Client = new DiscordSocketClient( new DiscordSocketConfig
{
GatewayIntents = GatewayIntents.GuildMessages
| GatewayIntents.GuildWebhooks
| GatewayIntents.DirectMessageTyping
| GatewayIntents.Guilds
});
_botConfig = BotConfig.GetBotConfig();
Client.Log += Logger.DiscordLog;
Client.JoinedGuild += GuildJoined;
Client.LeftGuild += GuildLeft;
Client.ButtonExecuted += arg => Tools.RunCommandInBackground(arg, () => ButtonExecuted(arg));
Client.Ready += () => Tools.ExceptionHandler(Ready, true);
Client.SlashCommandExecuted += arg => Tools.RunCommandInBackground(arg, () => SlashCommands.SlashCommandHandler(arg));
Client.SelectMenuExecuted += arg => Tools.RunCommandInBackground(arg, () => SelectMenuExecuted(arg));
}
public static Program GetInstance()
{
return _instance ??= new Program();
}
private async Task Start()
{
await Client.LoginAsync(TokenType.Bot, _botConfig.BotToken);
await Client.StartAsync();
await Client.SetGameAsync("/help");
}
private async Task Ready()
{
if (Environment.GetCommandLineArgs().Length > 1 && Environment.GetCommandLineArgs()[1] == "init")
{
await SlashCommands.InitSlashCommands();
Logger.Log(new MyLogMessage(LogSeverity.Info, "Ready", "Initialized all commands."));
}
await Config.ServerConfigStartUp();
_bgTask ??= BgTask();
}
private static async Task ButtonExecuted(SocketMessageComponent arg)
{
string matchLink = "";
foreach (Embed e in arg.Message.Embeds)
{
matchLink = ((EmbedAuthor)e.Author!).Url;
}
GetMatch request = new GetMatch { Id = Tools.GetIdFromUrl(matchLink) };
Match match = await request.SendRequest<Match>();
if (arg.Data.CustomId == "overallstats_bo1")
{
GetMatchMapStats requestMapStats = new GetMatchMapStats { Id = match.Maps[0].StatsId };
await arg.ModifyOriginalResponseAsync(msg =>
msg.Embed = requestMapStats.SendRequest<MatchMapStats>().Result.ToEmbed());
}
else
{
GetMatchStats requestMatchStats = new GetMatchStats { Id = match.StatsId };
await arg.ModifyOriginalResponseAsync(msg =>
msg.Embed = requestMatchStats.SendRequest<MatchStats>().Result.ToEmbed());
}
}
private static async Task SelectMenuExecuted(SocketMessageComponent arg)
{
switch (arg.Data.CustomId)
{
case "upcomingEventsMenu":
await HltvEvents.SendEvent(arg);
break;
case "ongoingEventsMenu":
await HltvEvents.SendEvent(arg);
break;
}
}
public Task GuildJoined(SocketGuild guild)
{
_ = Task.Run(async () =>
{
try
{
await Config.GuildJoined(guild);
StatsTracker.GetStats().ServerCount = Client.Guilds.Count;
}
catch (Exception ex)
{
if (ex is HttpException)
{
Logger.Log(new MyLogMessage(LogSeverity.Info, "GuildJoined", "Found server with insufficient permissions."));
await Config.SendMessageAfterServerJoin(guild, new EmbedBuilder()
.WithDescription(
"It looks like the bot has insufficient permissions (probably webhooks) on this server" +
". Please use the invite-link and grant all requested permissions.").Build());
}
else
{
Logger.Log(new MyLogMessage(LogSeverity.Error, ex));
await Config.SendMessageAfterServerJoin(guild, new EmbedBuilder()
.WithDescription(
$"An Exception occured while joining this server with the following message: {ex.Message}. Please report this bug!")
.Build());
}
throw;
}
});
return Task.CompletedTask;
}
private Task GuildLeft(SocketGuild guild)
{
_ = Task.Run(async () =>
{
await Tools.ExceptionHandler(async () =>
{
await ServerConfigRepository.Delete(guild.Id);
StatsTracker.GetStats().ServerCount = Client.Guilds.Count;
});
});
return Task.CompletedTask;
}
private async Task MiscellaneousBackground()
{
if (Client.CurrentUser.Id == _botConfig.ProductionBotId)
{
//top.gg
HttpRequestMessage reqTop = new(HttpMethod.Post, $"https://top.gg/api/bots/${Client.CurrentUser.Id}/stats");
reqTop.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(_botConfig.TopGgApiKey);
reqTop.Content = new StringContent($"{{ \"server_count\": {Client.Guilds.Count} }}", Encoding.UTF8, "application/json");
await DefaultHttpClient.SendAsync(reqTop);
//bots.gg
HttpRequestMessage reqBots = new (HttpMethod.Post, $"https://discord.bots.gg/api/v1/bots/${Client.Guilds.Count}/stats");
reqBots.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(_botConfig.BotsGgApiKey);
reqBots.Content = new StringContent($"{{ \"guildCount\": {Client.Guilds.Count} }}", Encoding.UTF8, "application/json");
await DefaultHttpClient.SendAsync(reqBots);
}
CacheCleaner.Clean();
StatsRepository.Update(StatsTracker.GetStats());
await HltvRanking.UpdateTeamRanking();
}
private async Task BgTask()
{
await Tools.ExceptionHandler(MiscellaneousBackground, true);
Timer updateGgStatsTimer = new(1000 * 60 * 60); // 1h interval
updateGgStatsTimer.Elapsed += async (_, _) =>
{
await Tools.ExceptionHandler(MiscellaneousBackground, true);
};
updateGgStatsTimer.Enabled = true;
(Timer, Func<Task>)[] timers = {(new Timer(), HltvNews.SendNewNews), (new Timer(), HltvResults.SendNewResults),
(new Timer(), HltvEvents.SendNewStartedEvents), (new Timer(), HltvEvents.SendNewPastEvents), (new Timer(), HltvMatches.UpdateMatches)};
foreach ((Timer timer, Func<Task> function) in timers)
{
await Tools.ExceptionHandler(function, true);
timer.Interval = _botConfig.CheckResultsTimeInterval;
timer.Elapsed += async (_, _) =>
await Tools.ExceptionHandler(function, true);
timer.Enabled = true;
await Task.Delay(_botConfig.CheckResultsTimeInterval / timers.Length);
}
}
}