Skip to content

Commit

Permalink
Use setInterval for repeating tasks (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
JstnMcBrd authored Apr 6, 2024
1 parent 9fd51ae commit 8ed8133
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 39 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
"@colors/colors": "^1.6.0",
"cleverbot-free": "^2.0.2",
"discord.js": "^14.14.1",
"dotenv": "^16.4.3"
"dotenv": "^16.4.5"
},
"devDependencies": {
"@jstnmcbrd/eslint-config": "^1.0.0",
"eslint": "^8.56.0",
"typescript": "^5.3.3"
"typescript": "^5.4.4"
}
}
37 changes: 19 additions & 18 deletions src/activity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* This manager takes care of keeping the client user's activity regularly updated.
*
* This is necessary because after being set, the user's activity eventually expire.
* This is necessary because after being set, the user's activity eventually expires.
* https://github.com/JstnMcBrd/discord-cleverbot/issues/42
*/

import { ActivityType } from 'discord.js';
Expand All @@ -10,7 +11,7 @@ import type { ActivityOptions, Client } from 'discord.js';
import { error, info } from './logger.js';

/** How often to update the activity (in seconds). */
const activityUpdateFrequency = 5 * 60;
const activityUpdateFrequency = 1 * 60 * 60;

/** The activity the bot should use. */
const activityOptions: ActivityOptions = {
Expand All @@ -21,7 +22,7 @@ const activityOptions: ActivityOptions = {

/**
* // FIXME wait until Discord supports custom statuses for bots.
* https://github.com/discord/discord-api-docs/issues/1160#issuecomment-546549516
* https://github.com/JstnMcBrd/discord-cleverbot/issues/13
*/
// const activityOptions: ActivityOptions = {
// name: 'Custom Status',
Expand All @@ -39,18 +40,21 @@ const activityOptions: ActivityOptions = {
* @param client The current logged-in client
*/
export function start(client: Client<true>): void {
info('Updating activity...');
function update() {
info('Updating activity...');

try {
setActivity(client);
}
catch (err) {
error(err);
try {
setActivity(client);
}
catch (err) {
error(err);
}
}

setTimeout(() => {
start(client);
}, activityUpdateFrequency * 1000);
// Do now
update();
// Then repeat
setInterval(update, activityUpdateFrequency * 1000);
}

/**
Expand All @@ -59,15 +63,12 @@ export function start(client: Client<true>): void {
* @throws If the activity does not update correctly
*/
function setActivity(client: Client<true>): void {
const { activities } = client.user.setActivity(activityOptions);
const activity = activities.at(0);
const updatedPresence = client.user.setActivity(activityOptions);

// Double check to see if it worked
// FIXME this currently always returns true, but discord.js doesn't have a better way to check
if (!activity
|| activity.name !== activityOptions.name
|| activity.type !== activityOptions.type
|| activity.url !== activityOptions.url) {
// https://github.com/JstnMcBrd/discord-cleverbot/issues/3
if (!client.user.presence.equals(updatedPresence)) {
throw new Error('User presence did not update correctly.');
}
}
26 changes: 15 additions & 11 deletions src/refresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,25 @@ const refreshFrequency = 1 * 60 * 60;
* @param client The current logged-in client
*/
export async function refresh(client: Client<true>): Promise<void> {
info('Refreshing...');
async function update() {
info('Refreshing...');

// Validate the whitelist
await loadWhitelist(client);
// Validate the whitelist
await loadWhitelist(client);

// Update context for all whitelisted channels (in parallel)
await Promise.all(
getWhitelist().map(generateContext),
);
// Update context for all whitelisted channels (in parallel)
await Promise.all(
getWhitelist().map(generateContext),
);

// Follow-up on any missed messages
resumeConversations();
// Follow-up on any missed messages
resumeConversations();
}

// Repeat at regular intervals
setTimeout(() => void refresh(client), refreshFrequency * 1000);
// Do now
await update();
// Then repeat
setInterval(() => void update(), refreshFrequency * 1000);
}

/**
Expand Down

0 comments on commit 8ed8133

Please sign in to comment.