Skip to content
Open
Show file tree
Hide file tree
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
12 changes: 8 additions & 4 deletions src/plugins/discord/discord-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { TimerManager } from './timer-manager';
import {
buildDiscordButtons,
padHangulFields,
truncateString,
sanitizeActivityText,
isSeek,
} from './utils';

Expand Down Expand Up @@ -99,13 +99,17 @@ export class DiscordService {
const activityInfo: SetActivity = {
type: ActivityType.Listening,
statusDisplayType: config.statusDisplayType,
details: truncateString(songInfo.alternativeTitle ?? songInfo.title, 128), // Song title
details: sanitizeActivityText(
songInfo.alternativeTitle ?? songInfo.title
), // Song title
detailsUrl: songInfo.url ?? undefined,
state: truncateString(songInfo.tags?.at(0) ?? songInfo.artist, 128), // Artist name
state: sanitizeActivityText(
songInfo.tags?.at(0) ?? songInfo.artist
), // Artist name
stateUrl: songInfo.artistUrl,
largeImageKey: songInfo.imageSrc ?? undefined,
largeImageText: songInfo.album
? truncateString(songInfo.album, 128)
? sanitizeActivityText(songInfo.album)
: undefined,
buttons: buildDiscordButtons(config, songInfo),
};
Expand Down
24 changes: 24 additions & 0 deletions src/plugins/discord/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,30 @@ export const truncateString = (str: string, length: number): string => {
return str;
};

/**
* Sanitizes a string for Discord Activity.
* @param input - The string to sanitize.
* @returns The sanitized string or undefined.
*/
export function sanitizeActivityText(input?: string): string {
if (!input) {
return 'undefined';
}

const trimmed = input.trim();
let safeString = truncateString(trimmed, 128);

if (safeString.length <= 0) {
return 'undefined';
}

if (safeString.length < 2) {
safeString = safeString + '⠀'; // change if you have better replacement
}

return safeString;
}

/**
* Builds the array of buttons for the Discord Rich Presence activity.
* @param config - The plugin configuration.
Expand Down
Loading