Skip to content

Commit

Permalink
feat: Discord bot going.. well not live, but soon!
Browse files Browse the repository at this point in the history
  • Loading branch information
itssimple committed Feb 2, 2024
1 parent 3027e2e commit 64e6b6f
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ jobs:
dotnet publish CFLookup.csproj --output published-app --configuration Release -p:FileVersion="$PACKAGE_VERSION" -p:AssemblyVersion="$PACKAGE_VERSION" && \
octo pack --id="whatcurseforgeprojectisthis" --version="$PACKAGE_VERSION" --basePath="./published-app" --outFolder="./published-app" && \
octo push --package="./published-app/whatcurseforgeprojectisthis.$PACKAGE_VERSION.nupkg" --server="${{ secrets.OCTOPUS_SERVER_URL }}" --apiKey="${{ secrets.OCTOPUS_API_KEY }}"
- name: Build and package the bot for publish
run: |
cd CFDiscordBot && \
dotnet publish CFDiscordBot.csproj --output published-bot --configuration Release -p:FileVersion="$PACKAGE_VERSION" -p:AssemblyVersion="$PACKAGE_VERSION" && \
octo pack --id="cflookupbot" --version="$PACKAGE_VERSION" --basePath="./published-bot" --outFolder="./published-bot" && \
octo push --package="./published-bot/cflookupbot.$PACKAGE_VERSION.nupkg" --server="${{ secrets.OCTOPUS_SERVER_URL }}" --apiKey="${{ secrets.OCTOPUS_API_KEY }}"
7 changes: 5 additions & 2 deletions CFDiscordBot/Commands/GameLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ int gameId
return;
}

var mods = await apiClient.SearchModsAsync(gameId, pageSize: 1);

var embed = new EmbedBuilder()
.WithTitle(game.Data.Name)
.WithUrl($"https://curseforge.com/{game.Data.Slug}")
.WithColor(Color.Blue);
.WithDescription($"This game has {mods.Pagination.TotalCount:n0} mods available on CurseForge.")
.WithUrl($"https://www.curseforge.com/{game.Data.Slug}")
.WithColor(Color.DarkOrange);

if (!string.IsNullOrWhiteSpace(game.Data.Assets.IconUrl))
{
Expand Down
169 changes: 162 additions & 7 deletions CFDiscordBot/Commands/ProjectLookup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Discord;
using Discord.Interactions;
using System.Text;
using System.Text.RegularExpressions;

namespace CFDiscordBot.Commands
{
Expand All @@ -22,14 +24,167 @@ int projectId
return;
}

var embed = new EmbedBuilder()
.WithTitle(project.Data.Name)
.WithUrl(project.Data.Links.WebsiteUrl)
.WithDescription(project.Data.Summary)
.WithColor(Color.Blue)
.Build();
var mod = project.Data;

await RespondAsync(embeds: new[] { embed }, ephemeral: true);
//var modFiles = new List<CurseForge.APIClient.Models.Files.File>();

//var files = await apiClient.GetModFilesAsync(projectId, pageSize: 50);
//modFiles.AddRange(files.Data);

//var index = files.Pagination.Index;
//while (modFiles.Count < files.Pagination.TotalCount)
//{
// files = await apiClient.GetModFilesAsync(projectId, index: index++, pageSize: 50);
// modFiles.AddRange(files.Data);
//}

var summaryText = new StringBuilder();
var haveExtraLinebreak = false;

summaryText.AppendLine(mod.Summary);

if (mod.LatestFilesIndexes?.Count > 0)
{
var gameVersionList = new List<string>();
var modloaderList = new List<string>();

/*foreach (var modFile in modFiles)
{
if (modFile?.IsAvailable ?? true)
{
if (!string.IsNullOrWhiteSpace(file.GameVersion))
{
gameVersionList.Add(file.GameVersion);
}
if (!string.IsNullOrWhiteSpace(file.ModLoader?.ToString()))
{
modloaderList.Add(file.ModLoader.Value.ToString());
}
}
}*/

var gameVersions = string.Join(", ", gameVersionList.Distinct().OrderBy(gvt => Regex.Replace(gvt, "\\d+", m => m.Value.PadLeft(10, '0'))));
var modLoaders = string.Join(", ", modloaderList.Distinct().OrderBy(gvt => Regex.Replace(gvt, "\\d+", m => m.Value.PadLeft(10, '0'))));

if ((!string.IsNullOrWhiteSpace(gameVersions) || !string.IsNullOrWhiteSpace(modLoaders)) && !haveExtraLinebreak)
{
summaryText.AppendLine();
haveExtraLinebreak = true;
}

if (!string.IsNullOrWhiteSpace(gameVersions))
{
summaryText.AppendLine($"Game version(s): {gameVersions}");
}

if (!string.IsNullOrWhiteSpace(modLoaders))
{
summaryText.AppendLine($"Modloader(s): {modLoaders}");
}
}

var projectEmbed = new EmbedBuilder
{
Title = "Project information",
Color = Color.DarkOrange,
Fields = new List<EmbedFieldBuilder>()
};

if (Uri.TryCreate(mod.Logo?.ThumbnailUrl, UriKind.Absolute, out var logoUri))
{
projectEmbed.ThumbnailUrl = logoUri.AbsoluteUri;
}
else
{
projectEmbed.ThumbnailUrl = "https://www.curseforge.com/images/flame.svg";
}

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 },
};

if (!string.IsNullOrWhiteSpace(categories))
{
fields.Add(new EmbedFieldBuilder { Name = "Categories", Value = categories, IsInline = false });
}

projectEmbed.Fields.AddRange(fields);

projectEmbed.Footer = new EmbedFooterBuilder
{
IconUrl = "https://cdn.discordapp.com/avatars/1199770925025984513/3f52d33635a688cfd24f0d78272aaf00.png?size=256",
Text = "CurseForge"
};

if (mod.Screenshots.Count > 0)
{
if (Uri.TryCreate(mod.Screenshots.First().Url, UriKind.Absolute, out var screenshotUri))
{
projectEmbed.ImageUrl = screenshotUri.AbsoluteUri;
}
}

var buttons = new ComponentBuilder();

if (!string.IsNullOrWhiteSpace(mod.Links?.WikiUrl))
{
buttons.WithButton(
style: ButtonStyle.Link,
label: "Wiki",
url: mod.Links.WikiUrl
);
}

if (!string.IsNullOrWhiteSpace(mod.Links?.IssuesUrl))
{
buttons.WithButton(
style: ButtonStyle.Link,
label: "Issues",
url: mod.Links.IssuesUrl
);
}

if (!string.IsNullOrWhiteSpace(mod.Links?.SourceUrl))
{
buttons.WithButton(
style: ButtonStyle.Link,
label: "Source",
url: mod.Links.SourceUrl
);
}

buttons.WithButton(
style: ButtonStyle.Link,
label: "CFLookup",
url: $"https://cflookup.com/{projectId}"
);

if (!string.IsNullOrWhiteSpace(mod.Links?.WebsiteUrl))
{
buttons.WithButton(
style: ButtonStyle.Link,
label: "CurseForge",
url: mod.Links.WebsiteUrl
);
}

await RespondAsync($"Project `{projectId}` is: **[{mod.Name}](https://cflookup.com/{projectId})**\n" +
$"{summaryText}",
embeds: new[] { projectEmbed.Build() },
components: buttons.Build(),
ephemeral: true
);
}

[SlashCommand("cflookup", "Looks up a project by its ID", ignoreGroupNames: true)]
public async Task CFLookup([Summary("id", "The ID")] int projectId) => await ProjectIdAsync(projectId);
}
}

0 comments on commit 64e6b6f

Please sign in to comment.