- 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
105 lines
3.1 KiB
JavaScript
105 lines
3.1 KiB
JavaScript
import { getConfig, setConfig } from "../../modules/mongo-wrappers/mongoConfigWrappers.mjs";
|
|
import { ActivityType, PresenceUpdateStatus } from 'discord.js';
|
|
|
|
class PresenceManager {
|
|
/**
|
|
* Creates an instance of PresenceManager.
|
|
* @param {import('discord.js').Client} client - The Discord client instance.
|
|
*/
|
|
constructor(client) {
|
|
this.client = client;
|
|
}
|
|
|
|
/**
|
|
* Set the bot's presence.
|
|
* @param {"online"|"idle"|"dnd"} status - The status of the bot (online, idle, dnd).
|
|
* @param {"PLAYING"|"STREAMING"|"LISTENING"|"WATCHING"|"COMPETING"} activityType - The type of activity.
|
|
* @param {string} activityName - The name of the activity.
|
|
* @param {string} [url=null] - The URL for STREAMING activity type (optional).
|
|
*/
|
|
async setPresence(status, activityType, activityName, url = null) {
|
|
const activityOptions = {
|
|
type: this.convertActivityType(activityType),
|
|
name: activityName,
|
|
};
|
|
|
|
if (activityType.toUpperCase() === "STREAMING" && url) {
|
|
activityOptions.url = url;
|
|
}
|
|
|
|
await this.client.user.setPresence({
|
|
status: this.convertStatus(status),
|
|
activities: [activityOptions],
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Reset the bot's presence to the default state.
|
|
*/
|
|
async resetToDefault() {
|
|
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);
|
|
|
|
// Update your bot's presence using this configuration
|
|
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;
|