Skip to content

Commit

Permalink
fix: There, now we handle if the API client dies while trying to find…
Browse files Browse the repository at this point in the history
… a game/project
  • Loading branch information
itssimple committed Feb 2, 2024
1 parent 0edb3ef commit 04911e3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CFDiscordBot/Commands/GameLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public async Task GameIdAsync(
int gameId
)
{
CurseForge.APIClient.Models.Games.Game gameInfo = null;
CurseForge.APIClient.Models.Games.Game? gameInfo = null;
try
{
var game = await apiClient.GetGameAsync(gameId);
Expand Down
31 changes: 19 additions & 12 deletions CFDiscordBot/Commands/ProjectLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@ public async Task ProjectIdAsync(
int projectId
)
{
var project = await apiClient.GetModAsync(projectId);
CurseForge.APIClient.Models.Mods.Mod? mod = null;
try
{
var project = await apiClient.GetModAsync(projectId);
mod = project.Data;
}
catch
{
// Empty because, ugh
}

if (project is null)
if (mod is null)
{
await RespondAsync($"Project with id {projectId} was not found.", ephemeral: true);

Expand All @@ -24,8 +33,6 @@ int projectId
return;
}

var mod = project.Data;

//var modFiles = new List<CurseForge.APIClient.Models.Files.File>();

//var files = await apiClient.GetModFilesAsync(projectId, pageSize: 50);
Expand Down Expand Up @@ -87,7 +94,7 @@ int projectId
{
Title = "Project information",
Color = Color.DarkOrange,
Fields = new List<EmbedFieldBuilder>()
Fields = []
};

if (Uri.TryCreate(mod.Logo?.ThumbnailUrl, UriKind.Absolute, out var logoUri))
Expand All @@ -102,13 +109,13 @@ int projectId
var categories = string.Join(", ", mod.Categories.Select(c => $"[{c.Name}]({c.Url})"));

var fields = new List<EmbedFieldBuilder> {
new EmbedFieldBuilder { Name = "Author", Value = string.Join(", ", mod.Authors.Select(c => $"[{c.Name}]({c.Url})")), IsInline = true },
new EmbedFieldBuilder { Name = "Status", Value = mod.Status.ToString(), IsInline = true },
new EmbedFieldBuilder { Name = "Created", Value = $"<t:{mod.DateCreated.ToUnixTimeSeconds()}:F>", IsInline = true },
new EmbedFieldBuilder { Name = "Modified", Value = $"<t:{mod.DateModified.ToUnixTimeSeconds()}:F>", IsInline = true },
new EmbedFieldBuilder { Name = "Released", Value = $"<t:{mod.DateReleased.ToUnixTimeSeconds()}:F>", IsInline = true },
new EmbedFieldBuilder { Name = "Downloads", Value = mod.DownloadCount.ToString("n0"), IsInline = true },
new EmbedFieldBuilder { Name = "Mod Distribution", Value = mod.AllowModDistribution ?? true ? "Allowed" : "Not allowed", IsInline = true },
new() { Name = "Author", Value = string.Join(", ", mod.Authors.Select(c => $"[{c.Name}]({c.Url})")), IsInline = true },
new() { Name = "Status", Value = mod.Status.ToString(), IsInline = true },
new() { Name = "Created", Value = $"<t:{mod.DateCreated.ToUnixTimeSeconds()}:F>", IsInline = true },
new() { Name = "Modified", Value = $"<t:{mod.DateModified.ToUnixTimeSeconds()}:F>", IsInline = true },
new() { Name = "Released", Value = $"<t:{mod.DateReleased.ToUnixTimeSeconds()}:F>", IsInline = true },
new() { Name = "Downloads", Value = mod.DownloadCount.ToString("n0"), IsInline = true },
new() { Name = "Mod Distribution", Value = mod.AllowModDistribution ?? true ? "Allowed" : "Not allowed", IsInline = true },
};

if (!string.IsNullOrWhiteSpace(categories))
Expand Down

0 comments on commit 04911e3

Please sign in to comment.