-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathPsn.cs
78 lines (73 loc) · 3.26 KB
/
Psn.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
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using CompatBot.Commands.Attributes;
using CompatBot.Database;
using CompatBot.EventHandlers;
using CompatBot.ThumbScrapper;
using CompatBot.Utils;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using PsnClient;
namespace CompatBot.Commands;
[Group("psn")]
[Description("Commands related to PSN metadata")]
internal sealed partial class Psn: BaseCommandModuleCustom
{
private static readonly Client Client = new();
[Command("rename"), Aliases("setname", "settitle"), RequiresBotModRole]
[Description("Command to set or change game title for specific product code")]
public async Task Rename(CommandContext ctx, [Description("Product code such as BLUS12345")] string productCode, [RemainingText, Description("New game title to save in the database")] string title)
{
productCode = productCode.ToUpperInvariant();
await using var db = new ThumbnailDb();
var item = db.Thumbnail.FirstOrDefault(t => t.ProductCode == productCode);
if (item == null)
await ctx.ReactWithAsync(Config.Reactions.Failure, $"Unknown product code {productCode}", true).ConfigureAwait(false);
else
{
item.Name = title;
await db.SaveChangesAsync().ConfigureAwait(false);
await ctx.ReactWithAsync(Config.Reactions.Success, "Title updated successfully").ConfigureAwait(false);
}
}
[Command("add"), RequiresBotModRole]
[Description("Add new product code with specified title to the bot database")]
public async Task Add(CommandContext ctx, [Description("Product code such as BLUS12345")] string contentId, [RemainingText, Description("New game title to save in the database")] string title)
{
contentId = contentId.ToUpperInvariant();
var productCodeMatch = ProductCodeLookup.Pattern().Match(contentId);
var contentIdMatch = PsnScraper.ContentIdMatcher().Match(contentId);
string productCode;
if (contentIdMatch.Success)
{
productCode = contentIdMatch.Groups["product_id"].Value;
}
else if (productCodeMatch.Success)
{
productCode = productCodeMatch.Groups["letters"].Value + productCodeMatch.Groups["numbers"].Value;
contentId = "";
}
else
{
await ctx.ReactWithAsync(Config.Reactions.Failure, "Invalid content id", true).ConfigureAwait(false);
return;
}
await using var db = new ThumbnailDb();
var item = db.Thumbnail.FirstOrDefault(t => t.ProductCode == productCode);
if (item is null)
{
item = new Thumbnail
{
ProductCode = productCode,
ContentId = string.IsNullOrEmpty(contentId) ? null : contentId,
Name = title,
};
await db.AddAsync(item).ConfigureAwait(false);
await db.SaveChangesAsync().ConfigureAwait(false);
await ctx.ReactWithAsync(Config.Reactions.Success, "Title added successfully").ConfigureAwait(false);
}
else
await ctx.ReactWithAsync(Config.Reactions.Failure, $"Product code {contentId} already exists", true).ConfigureAwait(false);
}
}