-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
127 lines (97 loc) · 3.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using Discord;
using Discord.WebSocket;
using EasyNetLog;
using KostyasLairBot.Commands;
using LibGit2Sharp;
using Newtonsoft.Json.Linq;
using System.Diagnostics;
namespace KostyasLairBot;
internal class Program
{
public static EasyNetLogger Logger { get; private set; } = new(msg => $"[<color=red>{DateTime.Now:HH:mm:ss.fff}</color>] {msg}", true, new string[] { "latest.log" });
public static DiscordSocketClient Discord { get; private set; } = new();
private const string configPath = "config.json";
public const ulong Guild = 1034375602502901791;
private static DiscordCommand[] commands =
{
new PingCommand(),
new WhoIsCommand(),
new SendCommand(),
new SetStatusCommand()
};
private static async Task Main()
{
#if LOCAL_TESTING
Logger.Log($"<color=magenta>TESTING MODE</color>");
#endif
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
Logger.Log($"Current dir set to {Directory.GetCurrentDirectory()}");
if (!File.Exists(configPath))
{
Logger.Log($"Please set up a {configPath} file first.");
return;
}
var config = JObject.Parse(await File.ReadAllTextAsync(configPath));
var token = (string?)config["token"];
if (token == null)
{
Logger.Log($"Please set the token in the config file.");
return;
}
Discord.Log += OnDiscordLog;
Discord.Ready += OnBotStart;
await Discord.LoginAsync(TokenType.Bot, token);
await Discord.StartAsync();
#if !LOCAL_TESTING
await GitCheckLoopAsync();
#else
await Task.Delay(-1);
#endif
Logger.Log("<color=red>Exiting...</color>");
}
private static async Task InitializeCommandsAsync()
{
var properties = new ApplicationCommandProperties[commands.Length];
Discord.SlashCommandExecuted += OnCommandExecuted;
for (var i = 0; i < commands.Length; i++)
{
var command = commands[i];
properties[i] = command.GetCommandBuilder().Build();
}
await Discord.GetGuild(Guild).BulkOverwriteApplicationCommandAsync(properties);
}
private static async Task OnCommandExecuted(SocketSlashCommand interaction)
{
var command = commands.FirstOrDefault(x => x.Name == interaction.CommandName);
if (command == null)
return;
await command.OnExecute(interaction);
}
private static async Task OnBotStart()
{
await InitializeCommandsAsync();
}
private static async Task GitCheckLoopAsync()
{
Directory.SetCurrentDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ".."));
Logger.Log($"Current dir set to {Directory.GetCurrentDirectory()}");
var repo = new Repository(Directory.GetCurrentDirectory());
for (; ; )
{
repo.Network.Fetch("origin", new string[] { "production" });
if (repo.Refs.First(x => x.CanonicalName == "refs/heads/production").TargetIdentifier != repo.Refs.First(x => x.CanonicalName == "refs/remotes/origin/production").TargetIdentifier)
{
Logger.Log("New update detected. Pulling and restarting...");
await Process.Start("git", "pull origin production").WaitForExitAsync();
Process.Start("dotnet", "run");
return;
}
await Task.Delay(1 * 60000);
}
}
private static Task OnDiscordLog(LogMessage message)
{
Logger.Log($"[<color=blue>Discord</color>] {message.Message}");
return Task.CompletedTask;
}
}