Update Presence Manager #15

- Added convert functions to convert strings to activities and statuses
- Updated status and activity to use the discord.js consts from convert functions
- Reset default will get and use the default presence from the DB
- Reset default will set default presence in DB if not set
- Reset default will now use the same `this.setPresence()` function to limit variation
This commit is contained in:
Logan Cusano
2024-08-11 18:41:42 -04:00
parent a56c19a466
commit d18ffd4c11

View File

@@ -1,4 +1,5 @@
import { getConfig } from "./configHandler.mjs"; import { getConfig, setConfig } from "../../modules/mongo-wrappers/mongoConfigWrappers.mjs";
import { ActivityType, PresenceUpdateStatus } from 'discord.js';
class PresenceManager { class PresenceManager {
/** /**
@@ -7,10 +8,6 @@ class PresenceManager {
*/ */
constructor(client) { constructor(client) {
this.client = client; this.client = client;
this.defaultStatus = "online";
this.defaultActivityType = "LISTENING";
this.defaultActivityName = "for your commands";
this.defaultUrl = null;
} }
/** /**
@@ -20,9 +17,9 @@ class PresenceManager {
* @param {string} activityName - The name of the activity. * @param {string} activityName - The name of the activity.
* @param {string} [url=null] - The URL for STREAMING activity type (optional). * @param {string} [url=null] - The URL for STREAMING activity type (optional).
*/ */
setPresence(status, activityType, activityName, url = null) { async setPresence(status, activityType, activityName, url = null) {
const activityOptions = { const activityOptions = {
type: activityType.toUpperCase(), type: this.convertActivityType(activityType),
name: activityName, name: activityName,
}; };
@@ -30,8 +27,8 @@ class PresenceManager {
activityOptions.url = url; activityOptions.url = url;
} }
this.client.user.setPresence({ await this.client.user.setPresence({
status: status, status: this.convertStatus(status),
activities: [activityOptions], activities: [activityOptions],
}); });
} }
@@ -39,13 +36,69 @@ class PresenceManager {
/** /**
* Reset the bot's presence to the default state. * Reset the bot's presence to the default state.
*/ */
resetToDefault() { async resetToDefault() {
const defaultPresence = getConfig("presence"); let defaultPresence = await getConfig("presence");
if (!defaultPresence) {
defaultPresence = {
status: 'idle',
activities: [{
name: 'your commands',
type: 'LISTENING'
}]
};
await setConfig('presence', defaultPresence);
}
console.log("Default Presence:", defaultPresence); console.log("Default Presence:", defaultPresence);
// Update your bot's presence using this configuration // Update your bot's presence using this configuration
this.client.user.setPresence(defaultPresence); await this.setPresence(defaultPresence.status, defaultPresence.activities[0].type, defaultPresence.activities[0].name);
}
/**
* Convert a string activity type to the corresponding ActivityType enum.
* @param {string} activityType - The activity type string.
* @returns {ActivityType} - The corresponding ActivityType enum.
*/
convertActivityType(activityType) {
switch (activityType.toUpperCase()) {
case 'PLAYING':
return ActivityType.Playing;
case 'STREAMING':
return ActivityType.Streaming;
case 'LISTENING':
return ActivityType.Listening;
case 'WATCHING':
return ActivityType.Watching;
case 'COMPETING':
return ActivityType.Competing;
default:
throw new Error('Invalid activity type');
} }
} }
/**
* Convert a string status to the corresponding PresenceUpdateStatus enum.
* @param {string} status - The status string.
* @returns {PresenceUpdateStatus} - The corresponding PresenceUpdateStatus enum.
*/
convertStatus(status) {
switch (status.toLowerCase()) {
case 'online':
return PresenceUpdateStatus.Online;
case 'idle':
return PresenceUpdateStatus.Idle;
case 'dnd':
return PresenceUpdateStatus.DoNotDisturb;
case 'invisible':
return PresenceUpdateStatus.Invisible;
default:
throw new Error('Invalid status');
}
}
}
export default PresenceManager; export default PresenceManager;