diff --git a/config/default.js b/config/default.js index 5e3a76d..9d00c3b 100644 --- a/config/default.js +++ b/config/default.js @@ -22,7 +22,7 @@ module.exports = { greeting: 'Hi, I am aisha, your game host!', usernamePrefix: '@', channels: ['GamesNight'], - games: ['mash', 'trivia', 'letters', 'duck', 'ping', 'say', 'kill', 'help'], + games: ['mash', 'trivia', 'letters', 'duck', 'ping', 'say', 'kill', 'uptime', 'help'], schatColors: { red: 'red', orange: 'orange', diff --git a/package-lock.json b/package-lock.json index 4a03243..63a7e06 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "bhttp": "^1.2.8", "bottleneck": "^2.19.5", "config": "^3.3.6", + "date-fns": "^2.29.3", "html-entities": "^2.3.2", "inflect": "^0.5.0", "irc": "^0.5.2", @@ -1078,6 +1079,18 @@ "node": ">=12" } }, + "node_modules/date-fns": { + "version": "2.29.3", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.29.3.tgz", + "integrity": "sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA==", + "engines": { + "node": ">=0.11" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/date-fns" + } + }, "node_modules/debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -4673,6 +4686,11 @@ "whatwg-url": "^10.0.0" } }, + "date-fns": { + "version": "2.29.3", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.29.3.tgz", + "integrity": "sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA==" + }, "debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", diff --git a/package.json b/package.json index a187a57..8c91eba 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "bhttp": "^1.2.8", "bottleneck": "^2.19.5", "config": "^3.3.6", + "date-fns": "^2.29.3", "html-entities": "^2.3.2", "inflect": "^0.5.0", "irc": "^0.5.2", diff --git a/src/games/uptime.js b/src/games/uptime.js new file mode 100644 index 0000000..4457433 --- /dev/null +++ b/src/games/uptime.js @@ -0,0 +1,32 @@ +'use strict'; + +const config = require('config'); +const os = require('os'); +const { intervalToDuration } = require('date-fns'); + +const start = Date.now(); + +function getDurationString(duration) { + return Object.entries(duration) + .filter(([, value]) => !!value) + .map(([key, value]) => `${value} ${key}`) + .join(', '); +} + +function onCommand(args, context) { + const duration = intervalToDuration({ start, end: Date.now() }); + const durationString = getDurationString(duration); + + if (['sys', 'system'].includes(context.subcommand || args[0]) && config.operators.includes(context.user.username)) { + const osDuration = intervalToDuration({ start: 0, end: os.uptime() * 1000 }); + const osDurationString = getDurationString(osDuration); + + context.sendMessage(`I've been awake for ${durationString}. My host has been running for ${osDurationString}.`, context.room.id, null, context.message.user?.recipient); + + return; + } + + context.sendMessage(`I've been awake for ${durationString}.`, context.room.id, null, context.message.user?.recipient); +} + +module.exports = { onCommand };