-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
129 lines (110 loc) · 4.53 KB
/
Program.cs
File metadata and controls
129 lines (110 loc) · 4.53 KB
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
using Discord;
using Discord.WebSocket;
using Discord.Commands;
using System.Text;
using System.Diagnostics;
namespace DonderHelper
{
public class Program()
{
// Discord bot private key
private static readonly string DONDERHELPER_SECRET_KEY = "DONDERHELPER_SECRET_KEY";
private static readonly string __keypath = $"key.txt";
private static string __key = "";
#pragma warning disable CS8618
private static DiscordSocketClient _client;
private static CommandService _commandService;
private static LoggingHandler _logginghandler;
private static CommandsHandler _commandhandler;
private static Stats _stats;
#pragma warning restore CS8618
public static async Task Main(params string[] args)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
if (args.Length > 0)
Console.WriteLine($"Running with args: {string.Join(' ', args)}");
#region Skip Timer
if (!args.Contains("--skip-timer"))
{
Console.WriteLine("Starting in 20 seconds...");
Thread.Sleep(20000);
}
#endregion
Console.WriteLine("Donhirobotスタート! Let's starting!");
if (!Boot()) {
Console.WriteLine("Aborting launch & shutting down.");
return;
}
Console.WriteLine("Communicating with Discord...");
DiscordSocketConfig _config = new()
{
GatewayIntents = GatewayIntents.Guilds,
HandlerTimeout = SongDatabase.TIMEOUT_MS
};
_client = new DiscordSocketClient(_config);
_commandService = new CommandService();
_logginghandler = new(_client, _commandService);
_commandhandler = new(_client, _commandService);
if (File.Exists(__keypath))
__key = File.ReadAllText(__keypath);
if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(DONDERHELPER_SECRET_KEY)))
await _client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable(DONDERHELPER_SECRET_KEY));
else if (!string.IsNullOrWhiteSpace(__key))
await _client.LoginAsync(TokenType.Bot, __key);
else
throw new Exception($"Discord bot key could not be found in environment or text file. Please add your Discord bot's secret key to '{__keypath}' or the environment variable '{DONDERHELPER_SECRET_KEY}'.");
__key = "";
await _client.StartAsync();
_ = UpdateSonglist(Math.Max(1, SongDatabase.STATS_REFRESH_HR));
Console.WriteLine("Logged in successfully!");
// Block this task until the program is closed.
await Process.GetCurrentProcess().WaitForExitAsync();
await _client.LogoutAsync();
await _client.StopAsync();
}
private static bool Boot()
{
try
{
LocaleData.Initialize();
EmoteData.Initialize();
GaidenSonglist.Initialize();
SongDatabase.Initialize();
}
catch (Exception ex)
{
Console.WriteLine("Boot Failed.\n" + ex.ToString());
return false;
}
return true;
}
private static async Task UpdateSonglist(int hour_rate)
{
try
{
int code = await SongDatabase.UpdateStats();
var stats = SongDatabase.Stats;
if (code.IsSuccessStatusCode())
{
await _client.SetCustomStatusAsync($"Drumming along to {stats.TotalSongs} songs!");
}
else
{
await _client.SetCustomStatusAsync($"Drumming along to... how many songs was it again? [E:{code}]");
}
Console.WriteLine($"GetStats() returned code {code}");
SongDatabase.CleanStaleCache();
}
catch (Exception e)
{
Console.Error.WriteLine($"Something went wrong while getting song stats. Details: {e}");
await _client.SetCustomStatusAsync($"Drumming along to... how many songs was it again? [EX]");
}
finally
{
Thread.Sleep(new TimeSpan(hour_rate, 0, 0));
_ = UpdateSonglist(hour_rate);
}
}
}
}