Skip to content

Commit

Permalink
Merge pull request #535 from gowon-bot/534-move-rymimport-to-use-lilac
Browse files Browse the repository at this point in the history
[534] Change !rymimport to use lilac
  • Loading branch information
abbyfour authored Nov 28, 2024
2 parents d1f4bf4 + 997c2cc commit 0ccace6
Show file tree
Hide file tree
Showing 19 changed files with 265 additions and 302 deletions.
1 change: 1 addition & 0 deletions src/commands/Lastfm/NowPlaying/NowPlayingBaseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export abstract class NowPlayingBaseCommand<
dbUser,
username
);

const renderedComponents = await this.nowPlayingService.renderComponents(
this.ctx,
await Promise.resolve(this.getConfig(senderUser!)),
Expand Down
175 changes: 0 additions & 175 deletions src/commands/Lastfm/RateYourMusic/Import.ts

This file was deleted.

73 changes: 73 additions & 0 deletions src/commands/Lastfm/RateYourMusic/ImportRatings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import fetch from "node-fetch";
import streamToString from "stream-to-string";
import { WrongFileFormatAttachedError } from "../../../errors/commands/library";
import { AttachmentArgument } from "../../../lib/context/arguments/argumentTypes/discord/AttachmentArgument";
import { ArgumentsMap } from "../../../lib/context/arguments/types";
import { RatingsImportProgressView } from "../../../lib/ui/views/RatingsImportProgressView";
import { RateYourMusicChildCommand } from "./RateYourMusicChildCommand";

const args = {
ratings: new AttachmentArgument({
index: 0,
description: "The file containing your RateYourMusic ratings",
required: {
customMessage: `Please attach your ratings! (See \`rym help\` for more info)`,
},
}),
} satisfies ArgumentsMap;

export class ImportRatings extends RateYourMusicChildCommand<typeof args> {
idSeed = "sonamoo high d";
aliases = ["rymimport", "rymsimport"];
description =
"Import your RateYourMusic ratings. See rym help for more info on how to import";

arguments = args;

slashCommand = true;

async run() {
await this.getMentions({
senderRequired: true,
syncedRequired: true,
});

const ratings = await this.getRatings();

const ratingsProgress = this.lilacRatingsService.importProgress(this.ctx, {
discordID: this.author.id,
});

const embed = this.minimalEmbed().setDescription(`Preparing import...`);

await this.reply(embed);

const syncProgressView = new RatingsImportProgressView(
this.ctx,
embed,
ratingsProgress
);

syncProgressView.subscribeToObservable();

await this.lilacRatingsService.import(this.ctx, ratings, {
discordID: this.author.id,
});
}

private async getRatings(): Promise<string> {
const ratingsAttachment = this.parsedArguments.ratings;

const file = await fetch(ratingsAttachment.url);

const fileContent = await streamToString(file.body);

const ratings = fileContent.trim();

if (!ratings.startsWith("RYM Album,")) {
throw new WrongFileFormatAttachedError();
}

return ratings;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CommandGroup } from "../../../lib/command/CommandGroup";
import { ParentCommand } from "../../../lib/command/ParentCommand";
import { ArtistRatings } from "./ArtistRatings";
import { Help } from "./Help";
import { ImportRatings } from "./Import";
import { ImportRatings } from "./ImportRatings";
import { Link } from "./Link";
import { Rating } from "./Rating";
import { Ratings } from "./Ratings";
Expand Down
24 changes: 0 additions & 24 deletions src/commands/Lastfm/RateYourMusic/connectors.ts

This file was deleted.

22 changes: 0 additions & 22 deletions src/errors/commands/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,34 +70,12 @@ export class TooManySearchResultsError extends ClientError {
}
}

export class UnknownRatingsImportError extends ClientError {
constructor() {
super("Something went wrong when importing your ratings");
}
}

export class WrongFileFormatAttachedError extends ClientError {
constructor() {
super("Please attach a file with the correct format");
}
}

export class TooManyAttachmentsError extends ClientError {
constructor() {
super(
"Too many attachments! Please attach only one file with your ratings"
);
}
}

export class NoRatingsFileAttatchedError extends ClientError {
constructor(prefix: string) {
super(
`Please attach your ratings! (See \`${prefix}rym help\` for more info)`
);
}
}

export class CouldNotFindRatingError extends ClientError {
constructor() {
super("Couldn't find that album in your ratings!");
Expand Down
4 changes: 2 additions & 2 deletions src/errors/lilac.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApolloError, ServerError } from "@apollo/client";
import { ErrorWithSupernovaID } from "../services/analytics/ReportingService";
import { ErrorWithSupernovaID } from "../services/analytics/ErrorReportingService";
import { ClientError } from "./errors";

export function parseLilacError(error: Error): Error {
Expand All @@ -9,7 +9,7 @@ export function parseLilacError(error: Error): Error {
error instanceof ApolloError &&
isServerError(error.networkError)
) {
if (error.networkError.result.supernova_id) {
if (error?.networkError?.result?.supernova_id) {
return new LilacError(
error.message,
error.networkError.result.supernova_id
Expand Down
2 changes: 1 addition & 1 deletion src/lib/command/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { GowonService } from "../../services/GowonService";
import { NowPlayingEmbedParsingService } from "../../services/NowPlayingEmbedParsingService";
import { ServiceRegistry } from "../../services/ServicesRegistry";
import { TrackingService } from "../../services/TrackingService";
import { ErrorReportingService } from "../../services/analytics/ReportingService";
import { ErrorReportingService } from "../../services/analytics/ErrorReportingService";
import { ArgumentParsingService } from "../../services/arguments/ArgumentsParsingService";
import { MentionsService } from "../../services/arguments/mentions/MentionsService";
import {
Expand Down
20 changes: 11 additions & 9 deletions src/lib/context/arguments/argumentTypes/SlashCommandTypes.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import {
SlashCommandBuilder as _SlashCommandBuilder,
SlashCommandRoleOption,
SlashCommandUserOption,
SlashCommandNumberOption,
SlashCommandStringOption,
SlashCommandAttachmentOption,
SlashCommandBooleanOption,
SlashCommandChannelOption,
SlashCommandIntegerOption,
SlashCommandMentionableOption,
SlashCommandNumberOption,
SlashCommandRoleOption,
SlashCommandStringOption,
SlashCommandUserOption,
} from "@discordjs/builders";

// Discord.js doesn't export any interfaces to help deal with building slash commands
// so I created them myself

export {
SlashCommandRoleOption,
SlashCommandUserOption,
SlashCommandNumberOption,
SlashCommandStringOption,
SlashCommandBooleanOption,
SlashCommandChannelOption,
SlashCommandIntegerOption,
SlashCommandMentionableOption,
SlashCommandNumberOption,
SlashCommandRoleOption,
SlashCommandStringOption,
SlashCommandUserOption,
};

// Any type of slash command option
Expand All @@ -33,7 +34,8 @@ export type SlashCommandOption =
| SlashCommandBooleanOption
| SlashCommandChannelOption
| SlashCommandIntegerOption
| SlashCommandMentionableOption;
| SlashCommandMentionableOption
| SlashCommandAttachmentOption;

export type SlashCommandBuilder = _SlashCommandBuilder;
export type SlashCommandBuilderReturn =
Expand Down
Loading

0 comments on commit 0ccace6

Please sign in to comment.