-
Notifications
You must be signed in to change notification settings - Fork 1
/
InfoPostManager.cs
245 lines (227 loc) · 8.49 KB
/
InfoPostManager.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
using Discord;
using Discord.WebSocket;
using FreneticUtilities.FreneticDataSyntax;
using FreneticUtilities.FreneticExtensions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FreneticDiscordBot;
public class InfoPostManager
{
public class InfoPost
{
public ulong GuildID;
public ulong ChannelID;
public string[] Messages;
}
public string GitFolder;
public DiscordSocketClient Client;
public FreneticDiscordBot Bot;
public long LastPullTime = 0;
public List<InfoPost> Posts;
public void Init(FDSSection section, DiscordSocketClient _client, FreneticDiscordBot _bot)
{
Client = _client;
Bot = _bot;
GitFolder = section.GetString("git_folder").Replace('\\', '/').TrimEnd('/') ?? throw new Exception("Missing 'git_folder' in InfoPostManager section!");
if (!Directory.Exists(GitFolder))
{
throw new Exception("Git folder in config does not exist on data drive!");
}
Client.Ready += () =>
{
InitFromGitFolder();
return Task.CompletedTask;
};
Bot.IdleTick += Loop;
}
public void Loop()
{
if (Bot.StopAllLogic)
{
return;
}
if (Posts is null)
{
return;
}
if (Environment.TickCount64 - LastPullTime < 15 * 60 * 1000)
{
return;
}
RunCheck();
}
public void RunCheck()
{
try
{
LastPullTime = Environment.TickCount64;
string head = RunGitProcess("rev-parse HEAD", GitFolder);
RunGitProcess("pull", GitFolder);
string newHead = RunGitProcess("rev-parse HEAD", GitFolder);
if (head == newHead)
{
Console.WriteLine($"[InfoPostManager] Did git pull, no change (head is {head})");
return;
}
Console.WriteLine($"[InfoPostManager] Did git pull, head was {head}, now is {newHead}, will process...");
InitFromGitFolder();
}
catch (Exception ex)
{
Console.WriteLine($"[InfoPostManager] Error in RunCheck: {ex}");
}
}
public static string RunGitProcess(string args, string folder)
{
folder = Path.GetFullPath(folder);
Process p = Process.Start(new ProcessStartInfo("git", args) { WorkingDirectory = folder, RedirectStandardOutput = true, UseShellExecute = false });
p.WaitForExit();
return p.StandardOutput.ReadToEnd().Trim();
}
public void InitFromGitFolder()
{
Console.WriteLine("[InfoPostManager] Doing init...");
try
{
Posts ??= [];
bool anyNew = false;
foreach (string folder in Directory.EnumerateDirectories(GitFolder).Select(f => f.Replace('\\', '/').AfterLast('/')))
{
if (!ulong.TryParse(folder, out ulong guildId))
{
continue;
}
SocketGuild guild = Client.GetGuild(guildId);
if (guild is null)
{
Console.WriteLine($"[InfoPostManager] Guild not found: {guildId}");
continue;
}
foreach (string file in Directory.EnumerateFiles($"{GitFolder}/{folder}", "*.md").Select(f => f.Replace('\\', '/').AfterLast('/')))
{
string channel = file.Before('.');
if (!ulong.TryParse(channel, out ulong channelId))
{
Console.WriteLine($"[InfoPostManager] Invalid channel ID in file name: '{file}'");
continue;
}
SocketTextChannel chan = guild.GetTextChannel(channelId);
if (chan is null)
{
Console.WriteLine($"[InfoPostManager] Channel not found: {guildId}/{channelId}");
continue;
}
string messageText = File.ReadAllText($"{GitFolder}/{folder}/{file}");
string[] messages = messageText.Split("<BREAK>", StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
if (messages.Length == 0)
{
Console.WriteLine($"[InfoPostManager] No messages found in file: {file}");
continue;
}
if (messages.Any(m => m.Length >= 2000))
{
Console.WriteLine($"[InfoPostManager] Message too long in file: {file} - lengths are {messages.Select(m => m.Length).JoinString(", ")}");
continue;
}
if (messages.Length > 25)
{
Console.WriteLine($"[InfoPostManager] Too many messages in file: {file}, has {messages.Length} messages");
continue;
}
InfoPost post = Posts.FirstOrDefault(p => p.GuildID == guildId && p.ChannelID == channelId);
if (post is null)
{
Posts.Add(new InfoPost() { GuildID = guildId, ChannelID = channelId, Messages = messages });
anyNew = true;
continue;
}
if (post.Messages.SequenceEqual(messages))
{
continue;
}
post.Messages = messages;
Console.WriteLine($"[InfoPostManager] Post for {post.GuildID}/{post.ChannelID} ({chan.Name}) was modified, reposting...");
SendPostNow(chan, post).Wait();
Console.WriteLine($"[InfoPostManager] Updated post for {guildId}/{channelId}");
}
}
if (Posts.IsEmpty())
{
Console.WriteLine("[InfoPostManager] No posts found at all! Nothing being maintained.");
}
if (anyNew)
{
LoadCurrentPosts().Wait();
}
}
catch (Exception ex)
{
Console.WriteLine($"[InfoPostManager] Exception in InitFromGitFolder: {ex}");
}
}
public SocketTextChannel ChannelFor(InfoPost post)
{
SocketGuild guild = Client.GetGuild(post.GuildID);
if (guild is null)
{
Console.WriteLine($"[InfoPostManager] Guild not found: {post.GuildID}");
return null;
}
SocketTextChannel chan = guild.GetTextChannel(post.ChannelID);
if (chan is null)
{
Console.WriteLine($"[InfoPostManager] Channel not found: {post.GuildID}/{post.ChannelID}");
return null;
}
return chan;
}
public static async Task<List<IMessage>> MessagesFor(SocketTextChannel channel)
{
return (await channel.GetMessagesAsync(50).FlattenAsync()).ToList();
}
public async Task SendPostNow(SocketTextChannel chan, InfoPost post)
{
List<IMessage> existing = await MessagesFor(chan);
foreach (string msg in post.Messages)
{
await chan.SendMessageAsync(msg, allowedMentions: AllowedMentions.None);
}
foreach (IMessage message in existing)
{
if (message.Author.Id == Client.CurrentUser.Id)
{
await message.DeleteAsync();
}
}
}
public async Task LoadCurrentPosts()
{
foreach (InfoPost post in Posts)
{
SocketTextChannel chan = ChannelFor(post);
if (chan is null)
{
continue;
}
List<string> existingMessages = [];
foreach (IMessage message in await MessagesFor(chan))
{
if (message.Author.Id == Client.CurrentUser.Id)
{
existingMessages.Add(message.Content);
}
}
if (existingMessages.SequenceEqual(post.Messages) || existingMessages.AsEnumerable().Reverse().SequenceEqual(post.Messages))
{
continue;
}
Console.WriteLine($"[InfoPostManager] Post for {post.GuildID}/{post.ChannelID} ({chan.Name}) is out of date in LoadCurrentPosts, reposting...");
await SendPostNow(chan, post);
}
}
}