Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address Minimum Length Issue #124

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions music-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ class AppleMusicDiscordRPC {
const activity: Activity = {
// @ts-ignore: "listening to" is allowed in recent Discord versions
type: 2,
details: AppleMusicDiscordRPC.truncateString(props.name),
details: AppleMusicDiscordRPC.ensureStringLengthValid(props.name),
timestamps: { start, end },
assets: { large_image: "appicon" },
};

if (props.artist) {
activity.state = AppleMusicDiscordRPC.truncateString(props.artist);
activity.state = AppleMusicDiscordRPC.ensureStringLengthValid(props.artist);
}

if (props.album) {
Expand All @@ -102,7 +102,7 @@ class AppleMusicDiscordRPC {

activity.assets = {
large_image: infos.artworkUrl ?? "appicon",
large_text: AppleMusicDiscordRPC.truncateString(props.album),
large_text: AppleMusicDiscordRPC.ensureStringLengthValid(props.album),
};

const buttons = [];
Expand Down Expand Up @@ -178,11 +178,19 @@ class AppleMusicDiscordRPC {
return version;
}

static ensureStringLengthValid(value: string, maxLength = 128): string {
return (value.length < 2) ? this.padString(value) : this.truncateString(value, maxLength);
}

static truncateString(value: string, maxLength = 128): string {
return value.length <= maxLength
? value
: `${value.slice(0, maxLength - 3)}...`;
}

static padString(value: string): string {
return ` ${value} `;
}
}

function sleep(ms: number): Promise<void> {
Expand Down