Skip to content

Commit

Permalink
fix: update key 'start_human_readable' to 'current_human_readable' fo…
Browse files Browse the repository at this point in the history
…r consistency
  • Loading branch information
chimpdev committed Nov 25, 2024
1 parent f8e43a3 commit 371d790
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 44 deletions.
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Retrieve the data of users with the specified IDs.
"raw": "2021-05-03T00:00:00.000Z"
},
"time": {
"start_human_readable": "00:00",
"current_human_readable": "00:00",
"end_human_readable": "00:00"
}
}
Expand All @@ -126,9 +126,11 @@ Retrieve the data of users with the specified IDs.
"text": "example"
}
},
"start_time": {
"unix": 1620000000,
"raw": "2021-05-03T00:00:00.000Z"
"timestamps": {
"start_time": {
"unix": 1620000000,
"raw": "2021-05-03T00:00:00.000Z"
}
}
}
],
Expand Down Expand Up @@ -208,7 +210,7 @@ Retrieve the data of a user with the specified ID.
"raw": "2021-05-03T00:00:00.000Z"
},
"time": {
"start_human_readable": "00:00",
"current_human_readable": "00:00",
"end_human_readable": "00:00"
}
}
Expand All @@ -235,9 +237,11 @@ Retrieve the data of a user with the specified ID.
"text": "example"
}
},
"start_time": {
"unix": 1620000000,
"raw": "2021-05-03T00:00:00.000Z"
"timestamps": {
"start_time": {
"unix": 1620000000,
"raw": "2021-05-03T00:00:00.000Z"
}
}
}
],
Expand Down
66 changes: 31 additions & 35 deletions src/lib/utils/bot/createUserData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Discord from 'discord.js';
import * as dateFns from 'date-fns';
import type { UserData } from '@/src/types';
import type { UserData, ClientPresenceStatus, ClientPresenceStatusData } from '@/src/types';

/**
* Creates a user data object for a given user ID and key-value storage.
Expand All @@ -17,16 +17,12 @@ function createUserData(user_id: string, kv: Map<string, string> | {}): UserData
const member = guild.members.cache.get(user_id);
if (!member) throw new Error('Member not found.');

const activePlatforms = ['desktop', 'mobile', 'web']
.map(platform => {
if (!member.presence || !member.presence.clientStatus) return { [platform]: 'offline' };

const currentPlatformStatus = member.presence.clientStatus as Record<string, string>;

return { [platform]: currentPlatformStatus[platform] || 'offline' };

})
.reduce((acc, curr) => ({ ...acc, ...curr }), {});
const activePlatforms = {
desktop: member.presence.clientStatus?.desktop as ClientPresenceStatus || 'offline',
mobile: member.presence.clientStatus?.mobile as ClientPresenceStatus || 'offline',
web: member.presence.clientStatus?.web as ClientPresenceStatus || 'offline',
spotify: null
} as ClientPresenceStatusData;

const spotifyActivity = member.presence?.activities.find(activity => activity.name === 'Spotify');

Expand All @@ -40,32 +36,30 @@ function createUserData(user_id: string, kv: Map<string, string> | {}): UserData
const currentHumanReadable = dateFns.format(new Date(elapsedTime * 1000), 'mm:ss');

// Calculate human-readable end time
const totalDuration = dateFns.differenceInSeconds(startTime, endTime);
const totalDuration = dateFns.differenceInSeconds(endTime, startTime);
const endHumanReadable = dateFns.format(new Date(totalDuration * 1000), 'mm:ss');

const artistCount = spotifyActivity.state?.split('; ').length || 0;

Object.assign(activePlatforms, {
spotify: {
track_id: spotifyActivity.syncId,
song: spotifyActivity.details,
artist: artistCount > 1 ? spotifyActivity.state?.split('; ') : spotifyActivity.state,
album: spotifyActivity.assets?.largeText,
album_cover: spotifyActivity.assets?.largeImageURL(),
start_time: {
unix: spotifyActivity.timestamps?.start?.getTime(),
raw: spotifyActivity.timestamps?.start
},
end_time: {
unix: spotifyActivity.timestamps?.end?.getTime(),
raw: spotifyActivity.timestamps?.end
},
time: {
start_human_readable: currentHumanReadable,
end_human_readable: endHumanReadable
}
activePlatforms.spotify = {
track_id: spotifyActivity.syncId,
song: spotifyActivity.details,
artist: artistCount > 1 ? spotifyActivity.state?.split('; ') : spotifyActivity.state,
album: spotifyActivity.assets?.largeText,
album_cover: spotifyActivity.assets?.largeImageURL(),
start_time: {
unix: spotifyActivity.timestamps?.start?.getTime(),
raw: spotifyActivity.timestamps?.start
},
end_time: {
unix: spotifyActivity.timestamps?.end?.getTime(),
raw: spotifyActivity.timestamps?.end
},
time: {
current_human_readable: currentHumanReadable,
end_human_readable: endHumanReadable
}
});
};
}

const parsedActivites = [];
Expand Down Expand Up @@ -117,9 +111,11 @@ function createUserData(user_id: string, kv: Map<string, string> | {}): UserData

if (activity.timestamps) {
Object.assign(activityData, {
start_time: {
unix: activity.timestamps?.start?.getTime(),
raw: activity.timestamps.start
timestamps: {
start_time: {
unix: activity.timestamps?.start?.getTime(),
raw: activity.timestamps.start
}
}
});
}
Expand Down
31 changes: 30 additions & 1 deletion src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,40 @@ export type UserData = {
}
},
status: string,
active_platforms: Record<string, string | Record<string, string>>,
active_platforms: ClientPresenceStatusData,
activities: (CustomStatusActivity | OtherActivity)[],
storage: Map<string, string> | {}
}

export type ClientPresenceStatus = 'online' | 'idle' | 'dnd' | 'offline';

export type ClientPresenceStatusData = {
web: ClientPresenceStatus;
mobile: ClientPresenceStatus;
desktop: ClientPresenceStatus;
spotify: SpotifyActivity | null;
}

type SpotifyActivity = {
track_id: string,
song: string,
artist: string | string[],
album: string,
album_cover: string,
start_time: {
unix: number,
raw: Date
},
end_time: {
unix: number,
raw: Date
},
time: {
current_human_readable: string,
end_human_readable: string
}
}

export type CustomStatusActivity = {
name: 'Custom Status',
type: Discord.ActivityType.Custom,
Expand Down

0 comments on commit 371d790

Please sign in to comment.