-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathFortune.cs
260 lines (237 loc) · 10.5 KB
/
Fortune.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using CompatApiClient.Compression;
using CompatBot.Commands.Attributes;
using CompatBot.Database;
using CompatBot.Utils;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using Microsoft.EntityFrameworkCore;
namespace CompatBot.Commands;
[Group("fortune"), Aliases("fortunes")]
[Description("Gives you a fortune once a day")]
internal sealed class Fortune : BaseCommandModuleCustom
{
private static readonly SemaphoreSlim ImportCheck = new(1, 1);
[GroupCommand]
public Task ShowFortune(CommandContext ctx)
=> ShowFortune(ctx.Message, ctx.User);
public static async Task ShowFortune(DiscordMessage message, DiscordUser user)
{
var prefix = DateTime.UtcNow.ToString("yyyyMMdd")+ user.Id.ToString("x16");
var rng = new Random(prefix.GetStableHash());
await using var db = new ThumbnailDb();
Database.Fortune? fortune;
do
{
var totalFortunes = await db.Fortune.CountAsync().ConfigureAwait(false);
if (totalFortunes == 0)
{
await message.ReactWithAsync(Config.Reactions.Failure, "There are no fortunes to tell", true).ConfigureAwait(false);
return;
}
var selectedId = rng.Next(totalFortunes);
fortune = await db.Fortune.AsNoTracking().Skip(selectedId).FirstOrDefaultAsync().ConfigureAwait(false);
} while (fortune is null);
var tmp = new StringBuilder();
var quote = true;
foreach (var l in fortune.Content.FixTypography().Split('\n'))
{
quote &= !l.StartsWith(" ");
if (quote)
tmp.Append("> ");
tmp.Append(l).Append('\n');
}
var msgBuilder = new DiscordMessageBuilder()
.WithContent($"""
{user.Mention}, your fortune for today:
{tmp.ToString().TrimEnd().FixSpaces()}
""")
.WithReply(message.Id);
await message.Channel.SendMessageAsync(msgBuilder).ConfigureAwait(false);
}
[Command("add"), RequiresBotModRole]
[Description("Add a new fortune")]
public async Task Add(CommandContext ctx, [RemainingText] string text)
{
text = text.Replace("\r\n", "\n").Trim();
if (text.Length > 1800)
{
await ctx.ReactWithAsync(Config.Reactions.Failure, "Fortune text is too long", true).ConfigureAwait(false);
return;
}
await using var db = new ThumbnailDb();
await db.Fortune.AddAsync(new() {Content = text}).ConfigureAwait(false);
await db.SaveChangesAsync().ConfigureAwait(false);
await ctx.ReactWithAsync(Config.Reactions.Success).ConfigureAwait(false);
}
[Command("remove"), Aliases("delete"), RequiresBotModRole]
[Description("Removes fortune with specified ID")]
public async Task Remove(CommandContext ctx, int id)
{
await using var db = new ThumbnailDb();
var fortune = await db.Fortune.FirstOrDefaultAsync(f => f.Id == id).ConfigureAwait(false);
if (fortune is null)
{
await ctx.ReactWithAsync(Config.Reactions.Failure, $"Fortune with id {id} wasn't found", true).ConfigureAwait(false);
return;
}
db.Fortune.Remove(fortune);
await db.SaveChangesAsync().ConfigureAwait(false);
await ctx.ReactWithAsync(Config.Reactions.Success).ConfigureAwait(false);
}
[Command("import"), Aliases("append"), RequiresBotModRole, TriggersTyping]
[Description("Imports new fortunes from specified URL or attachment. Data should be formatted as standard UNIX fortune source file.")]
public async Task Import(CommandContext ctx, string? url = null)
{
var msg = await ctx.Channel.SendMessageAsync("Please wait...").ConfigureAwait(false);
if (!await ImportCheck.WaitAsync(0).ConfigureAwait(false))
{
await ctx.ReactWithAsync(Config.Reactions.Failure).ConfigureAwait(false);
await msg.UpdateOrCreateMessageAsync(ctx.Channel, "There is another import in progress already").ConfigureAwait(false);
return;
}
try
{
if (string.IsNullOrEmpty(url))
url = ctx.Message.Attachments.FirstOrDefault()?.Url;
if (string.IsNullOrEmpty(url))
{
await ctx.ReactWithAsync(Config.Reactions.Failure).ConfigureAwait(false);
return;
}
var stopwatch = Stopwatch.StartNew();
await using var db = new ThumbnailDb();
using var httpClient = HttpClientFactory.Create(new CompressionMessageHandler());
using var request = new HttpRequestMessage(HttpMethod.Get, url);
var response = await httpClient.SendAsync(request, Config.Cts.Token).ConfigureAwait(false);
await using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
using var reader = new StreamReader(stream);
var buf = new StringBuilder();
string? line;
int count = 0, skipped = 0;
while (!Config.Cts.IsCancellationRequested
&& ((line = await reader.ReadLineAsync().ConfigureAwait(false)) != null
|| buf.Length > 0)
&& !Config.Cts.IsCancellationRequested)
{
if (line is "%" or null)
{
var content = buf.ToString().Replace("\r\n", "\n").Trim();
if (content.Length > 1900)
{
buf.Clear();
skipped++;
continue;
}
if (db.Fortune.Any(f => f.Content == content))
{
buf.Clear();
skipped++;
continue;
}
var duplicate = false;
foreach (var fortune in db.Fortune.AsNoTracking())
{
if (fortune.Content.GetFuzzyCoefficientCached(content) >= 0.95)
{
duplicate = true;
break;
}
if (Config.Cts.Token.IsCancellationRequested)
break;
}
if (duplicate)
{
buf.Clear();
skipped++;
continue;
}
await db.Fortune.AddAsync(new() {Content = content}).ConfigureAwait(false);
await db.SaveChangesAsync(Config.Cts.Token).ConfigureAwait(false);
buf.Clear();
count++;
}
else
buf.AppendLine(line);
if (line is null)
break;
if (stopwatch.ElapsedMilliseconds > 10_000)
{
var progressMsg = $"Imported {count} fortune{(count == 1 ? "" : "s")}";
if (skipped > 0)
progressMsg += $", skipped {skipped}";
if (response.Content.Headers.ContentLength is long len and > 0)
progressMsg += $" ({stream.Position * 100.0 / len:0.##}%)";
await msg.UpdateOrCreateMessageAsync(ctx.Channel, progressMsg).ConfigureAwait(false);
stopwatch.Restart();
}
}
var result = $"Imported {count} fortune{(count == 1 ? "" : "s")}";
if (skipped > 0)
result += $", skipped {skipped}";
await msg.UpdateOrCreateMessageAsync(ctx.Channel, result).ConfigureAwait(false);
await ctx.ReactWithAsync(Config.Reactions.Success).ConfigureAwait(false);
}
catch (Exception e)
{
await msg.UpdateOrCreateMessageAsync(ctx.Channel, "Failed to import data: " + e.Message).ConfigureAwait(false);
await ctx.ReactWithAsync(Config.Reactions.Failure).ConfigureAwait(false);
}
finally
{
ImportCheck.Release();
}
}
[Command("export"), RequiresBotModRole]
[Description("Exports fortune database into UNIX fortune source format file")]
public async Task Export(CommandContext ctx)
{
try
{
var count = 0;
await using var outputStream = Config.MemoryStreamManager.GetStream();
await using var writer = new StreamWriter(outputStream);
await using var db = new ThumbnailDb();
foreach (var fortune in db.Fortune.AsNoTracking())
{
if (Config.Cts.Token.IsCancellationRequested)
break;
await writer.WriteAsync(fortune.Content).ConfigureAwait(false);
await writer.WriteAsync("\n%\n").ConfigureAwait(false);
count++;
}
await writer.FlushAsync().ConfigureAwait(false);
outputStream.Seek(0, SeekOrigin.Begin);
var builder = new DiscordMessageBuilder()
.WithContent($"Exported {count} fortune{(count == 1 ? "": "s")}")
.AddFile("fortunes.txt", outputStream);
await ctx.Channel.SendMessageAsync(builder).ConfigureAwait(false);
}
catch (Exception e)
{
await ctx.ReactWithAsync(Config.Reactions.Failure, "Failed to export data: " + e.Message).ConfigureAwait(false);
}
}
[Command("clear"), RequiresBotModRole]
[Description("Clears fortune database. Use with caution")]
public async Task Clear(CommandContext ctx, [RemainingText, Description("Must be `with my blessing, I swear I exported the backup`")] string confirmation)
{
if (confirmation is not "with my blessing, I swear I exported the backup")
{
await ctx.ReactWithAsync(Config.Reactions.Failure).ConfigureAwait(false);
return;
}
await using var db = new ThumbnailDb();
db.Fortune.RemoveRange(db.Fortune);
var count = await db.SaveChangesAsync(Config.Cts.Token).ConfigureAwait(false);
await ctx.ReactWithAsync(Config.Reactions.Success, $"Removed {count} fortune{(count == 1 ? "" : "s")}", true).ConfigureAwait(false);
}
}