Compare commits
No commits in common. "db8af460177487bd608fdd8188e6b9eb88a186bd" and "f7113b72ce087f2e65144989233073dfbfd3a1ff" have entirely different histories.
db8af46017
...
f7113b72ce
|
@ -1,21 +1,26 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
platform: 'schat',
|
|
||||||
user: {
|
user: {
|
||||||
nick: 'aisha',
|
id: 'clive',
|
||||||
username: 'Aisha',
|
key: 'abcdefgh12345678',
|
||||||
realName: 'Aisha',
|
username: 'Clive',
|
||||||
|
// optional
|
||||||
|
gender: 'male',
|
||||||
|
countryCode: 'GB',
|
||||||
|
birthdate: new Date(1952, 11, 10),
|
||||||
|
avatar: 'https://i.imgur.com/IZwrjjG.png',
|
||||||
},
|
},
|
||||||
operators: ['admin'],
|
operators: ['admin'],
|
||||||
server: 'irc.libera.chat',
|
uniqueUsername: false,
|
||||||
port: 6697,
|
socket: 'ws://127.0.0.1:3000/socket',
|
||||||
|
api: 'http://127.0.0.1:3000/api',
|
||||||
reconnectDelay: 10, // seconds
|
reconnectDelay: 10, // seconds
|
||||||
prefix: '~',
|
prefix: '~',
|
||||||
labels: true,
|
style: {
|
||||||
greeting: 'Hi, I am aisha, your game host!',
|
color: 'var(--message-56)',
|
||||||
usernamePrefix: '@',
|
},
|
||||||
channels: ['##pendulum'],
|
channels: ['GamesNight'],
|
||||||
games: ['mash', 'trivia', 'duck', 'ping', 'say', 'kill'],
|
games: ['mash', 'trivia', 'duck', 'ping', 'say', 'kill'],
|
||||||
trivia: {
|
trivia: {
|
||||||
mode: 'first', // first or timeout
|
mode: 'first', // first or timeout
|
||||||
|
@ -24,6 +29,5 @@ module.exports = {
|
||||||
},
|
},
|
||||||
duck: {
|
duck: {
|
||||||
interval: [10, 3600], // seconds
|
interval: [10, 3600], // seconds
|
||||||
duck: ':duck:',
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,21 +1,18 @@
|
||||||
{
|
{
|
||||||
"name": "schat2-clive",
|
"name": "schat2-clive",
|
||||||
"version": "1.8.0",
|
"version": "1.7.7",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "schat2-clive",
|
"name": "schat2-clive",
|
||||||
"version": "1.8.0",
|
"version": "1.7.7",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"bhttp": "^1.2.8",
|
"bhttp": "^1.2.8",
|
||||||
"bottleneck": "^2.19.5",
|
"bottleneck": "^2.19.5",
|
||||||
"config": "^3.3.6",
|
"config": "^3.3.6",
|
||||||
"html-entities": "^2.3.2",
|
"html-entities": "^2.3.2",
|
||||||
"irc": "^0.5.2",
|
|
||||||
"irc-colors": "^1.5.0",
|
|
||||||
"irc-upd": "^0.11.0",
|
|
||||||
"jsdom": "^18.1.0",
|
"jsdom": "^18.1.0",
|
||||||
"linkify-it": "^3.0.3",
|
"linkify-it": "^3.0.3",
|
||||||
"simple-node-logger": "^21.8.12",
|
"simple-node-logger": "^21.8.12",
|
||||||
|
@ -1982,19 +1979,6 @@
|
||||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
|
||||||
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
|
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
|
||||||
},
|
},
|
||||||
"node_modules/iconv": {
|
|
||||||
"version": "2.2.3",
|
|
||||||
"resolved": "https://registry.npmjs.org/iconv/-/iconv-2.2.3.tgz",
|
|
||||||
"integrity": "sha512-evIiYeKdt5nEGYKNkQcGPQy781sYgbBKi3gEkt1s4CwteCdOHSjGGRyyp6lP8inYFZwvzG3lgjXEvGUC8nqQ5A==",
|
|
||||||
"hasInstallScript": true,
|
|
||||||
"optional": true,
|
|
||||||
"dependencies": {
|
|
||||||
"nan": "^2.3.5"
|
|
||||||
},
|
|
||||||
"engines": {
|
|
||||||
"node": ">=0.8.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/iconv-lite": {
|
"node_modules/iconv-lite": {
|
||||||
"version": "0.4.24",
|
"version": "0.4.24",
|
||||||
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
|
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
|
||||||
|
@ -2094,62 +2078,6 @@
|
||||||
"node": ">= 0.4"
|
"node": ">= 0.4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/irc": {
|
|
||||||
"version": "0.5.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/irc/-/irc-0.5.2.tgz",
|
|
||||||
"integrity": "sha512-KnrvkV05Y71SWmRWHtnlWEIH7LA/YeDul6l7tncCGLNEw4B6Obtmkatb3ACnSLj0kOJ6UBiuhss9e+eRG3zlxw==",
|
|
||||||
"dependencies": {
|
|
||||||
"irc-colors": "^1.1.0"
|
|
||||||
},
|
|
||||||
"engines": {
|
|
||||||
"node": ">=0.10.0"
|
|
||||||
},
|
|
||||||
"optionalDependencies": {
|
|
||||||
"iconv": "~2.2.1",
|
|
||||||
"node-icu-charset-detector": "~0.2.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/irc-colors": {
|
|
||||||
"version": "1.5.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/irc-colors/-/irc-colors-1.5.0.tgz",
|
|
||||||
"integrity": "sha512-HtszKchBQTcqw1DC09uD7i7vvMayHGM1OCo6AHt5pkgZEyo99ClhHTMJdf+Ezc9ovuNNxcH89QfyclGthjZJOw==",
|
|
||||||
"engines": {
|
|
||||||
"node": ">=6"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/irc-upd": {
|
|
||||||
"version": "0.11.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/irc-upd/-/irc-upd-0.11.0.tgz",
|
|
||||||
"integrity": "sha512-A1hV5cUkl5HZsKWRYcszD2Usfz33hB8igSSox8dEmrMyfy8/Ra6T/o4jwzs7jYMZ7ljLquSIWzcvSZHZ/bEAZA==",
|
|
||||||
"dependencies": {
|
|
||||||
"irc-colors": "^1.5.0"
|
|
||||||
},
|
|
||||||
"engines": {
|
|
||||||
"node": ">=0.10.0"
|
|
||||||
},
|
|
||||||
"optionalDependencies": {
|
|
||||||
"chardet": "^1.2.1",
|
|
||||||
"iconv-lite": "^0.6.2"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/irc-upd/node_modules/chardet": {
|
|
||||||
"version": "1.5.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/chardet/-/chardet-1.5.0.tgz",
|
|
||||||
"integrity": "sha512-Nj3VehbbFs/1ZnJJJaL3ztEf3Nu5Fs6YV/NBs6lyz/iDDHUU+X9QNk5QgPy1/5Rjtb/cGVf+NyazP7kVEJqKRg==",
|
|
||||||
"optional": true
|
|
||||||
},
|
|
||||||
"node_modules/irc-upd/node_modules/iconv-lite": {
|
|
||||||
"version": "0.6.3",
|
|
||||||
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz",
|
|
||||||
"integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==",
|
|
||||||
"optional": true,
|
|
||||||
"dependencies": {
|
|
||||||
"safer-buffer": ">= 2.1.2 < 3.0.0"
|
|
||||||
},
|
|
||||||
"engines": {
|
|
||||||
"node": ">=0.10.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/is-bigint": {
|
"node_modules/is-bigint": {
|
||||||
"version": "1.0.4",
|
"version": "1.0.4",
|
||||||
"resolved": "https://registry.npmjs.org/is-bigint/-/is-bigint-1.0.4.tgz",
|
"resolved": "https://registry.npmjs.org/is-bigint/-/is-bigint-1.0.4.tgz",
|
||||||
|
@ -2633,31 +2561,12 @@
|
||||||
"integrity": "sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==",
|
"integrity": "sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"node_modules/nan": {
|
|
||||||
"version": "2.17.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/nan/-/nan-2.17.0.tgz",
|
|
||||||
"integrity": "sha512-2ZTgtl0nJsO0KQCjEpxcIr5D+Yv90plTitZt9JBfQvVJDS5seMl3FOvsh3+9CoYWXf/1l5OaZzzF6nDm4cagaQ==",
|
|
||||||
"optional": true
|
|
||||||
},
|
|
||||||
"node_modules/natural-compare": {
|
"node_modules/natural-compare": {
|
||||||
"version": "1.4.0",
|
"version": "1.4.0",
|
||||||
"resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz",
|
"resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz",
|
||||||
"integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=",
|
"integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"node_modules/node-icu-charset-detector": {
|
|
||||||
"version": "0.2.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/node-icu-charset-detector/-/node-icu-charset-detector-0.2.0.tgz",
|
|
||||||
"integrity": "sha512-DYOFJ3NfKdxEi9hPbmoCss6WydGhJsxpSleUlZfAWEbZt3AU7JuxailgA9tnqQdsHiujfUY9VtDfWD9m0+ThtQ==",
|
|
||||||
"hasInstallScript": true,
|
|
||||||
"optional": true,
|
|
||||||
"dependencies": {
|
|
||||||
"nan": "^2.3.3"
|
|
||||||
},
|
|
||||||
"engines": {
|
|
||||||
"node": ">=0.6"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node_modules/node-releases": {
|
"node_modules/node-releases": {
|
||||||
"version": "2.0.1",
|
"version": "2.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.1.tgz",
|
||||||
|
@ -5310,15 +5219,6 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"iconv": {
|
|
||||||
"version": "2.2.3",
|
|
||||||
"resolved": "https://registry.npmjs.org/iconv/-/iconv-2.2.3.tgz",
|
|
||||||
"integrity": "sha512-evIiYeKdt5nEGYKNkQcGPQy781sYgbBKi3gEkt1s4CwteCdOHSjGGRyyp6lP8inYFZwvzG3lgjXEvGUC8nqQ5A==",
|
|
||||||
"optional": true,
|
|
||||||
"requires": {
|
|
||||||
"nan": "^2.3.5"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"iconv-lite": {
|
"iconv-lite": {
|
||||||
"version": "0.4.24",
|
"version": "0.4.24",
|
||||||
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
|
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
|
||||||
|
@ -5397,48 +5297,6 @@
|
||||||
"side-channel": "^1.0.4"
|
"side-channel": "^1.0.4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"irc": {
|
|
||||||
"version": "0.5.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/irc/-/irc-0.5.2.tgz",
|
|
||||||
"integrity": "sha512-KnrvkV05Y71SWmRWHtnlWEIH7LA/YeDul6l7tncCGLNEw4B6Obtmkatb3ACnSLj0kOJ6UBiuhss9e+eRG3zlxw==",
|
|
||||||
"requires": {
|
|
||||||
"iconv": "~2.2.1",
|
|
||||||
"irc-colors": "^1.1.0",
|
|
||||||
"node-icu-charset-detector": "~0.2.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"irc-colors": {
|
|
||||||
"version": "1.5.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/irc-colors/-/irc-colors-1.5.0.tgz",
|
|
||||||
"integrity": "sha512-HtszKchBQTcqw1DC09uD7i7vvMayHGM1OCo6AHt5pkgZEyo99ClhHTMJdf+Ezc9ovuNNxcH89QfyclGthjZJOw=="
|
|
||||||
},
|
|
||||||
"irc-upd": {
|
|
||||||
"version": "0.11.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/irc-upd/-/irc-upd-0.11.0.tgz",
|
|
||||||
"integrity": "sha512-A1hV5cUkl5HZsKWRYcszD2Usfz33hB8igSSox8dEmrMyfy8/Ra6T/o4jwzs7jYMZ7ljLquSIWzcvSZHZ/bEAZA==",
|
|
||||||
"requires": {
|
|
||||||
"chardet": "^1.2.1",
|
|
||||||
"iconv-lite": "^0.6.2",
|
|
||||||
"irc-colors": "^1.5.0"
|
|
||||||
},
|
|
||||||
"dependencies": {
|
|
||||||
"chardet": {
|
|
||||||
"version": "1.5.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/chardet/-/chardet-1.5.0.tgz",
|
|
||||||
"integrity": "sha512-Nj3VehbbFs/1ZnJJJaL3ztEf3Nu5Fs6YV/NBs6lyz/iDDHUU+X9QNk5QgPy1/5Rjtb/cGVf+NyazP7kVEJqKRg==",
|
|
||||||
"optional": true
|
|
||||||
},
|
|
||||||
"iconv-lite": {
|
|
||||||
"version": "0.6.3",
|
|
||||||
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz",
|
|
||||||
"integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==",
|
|
||||||
"optional": true,
|
|
||||||
"requires": {
|
|
||||||
"safer-buffer": ">= 2.1.2 < 3.0.0"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"is-bigint": {
|
"is-bigint": {
|
||||||
"version": "1.0.4",
|
"version": "1.0.4",
|
||||||
"resolved": "https://registry.npmjs.org/is-bigint/-/is-bigint-1.0.4.tgz",
|
"resolved": "https://registry.npmjs.org/is-bigint/-/is-bigint-1.0.4.tgz",
|
||||||
|
@ -5787,27 +5645,12 @@
|
||||||
"integrity": "sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==",
|
"integrity": "sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"nan": {
|
|
||||||
"version": "2.17.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/nan/-/nan-2.17.0.tgz",
|
|
||||||
"integrity": "sha512-2ZTgtl0nJsO0KQCjEpxcIr5D+Yv90plTitZt9JBfQvVJDS5seMl3FOvsh3+9CoYWXf/1l5OaZzzF6nDm4cagaQ==",
|
|
||||||
"optional": true
|
|
||||||
},
|
|
||||||
"natural-compare": {
|
"natural-compare": {
|
||||||
"version": "1.4.0",
|
"version": "1.4.0",
|
||||||
"resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz",
|
"resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz",
|
||||||
"integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=",
|
"integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"node-icu-charset-detector": {
|
|
||||||
"version": "0.2.0",
|
|
||||||
"resolved": "https://registry.npmjs.org/node-icu-charset-detector/-/node-icu-charset-detector-0.2.0.tgz",
|
|
||||||
"integrity": "sha512-DYOFJ3NfKdxEi9hPbmoCss6WydGhJsxpSleUlZfAWEbZt3AU7JuxailgA9tnqQdsHiujfUY9VtDfWD9m0+ThtQ==",
|
|
||||||
"optional": true,
|
|
||||||
"requires": {
|
|
||||||
"nan": "^2.3.3"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"node-releases": {
|
"node-releases": {
|
||||||
"version": "2.0.1",
|
"version": "2.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.1.tgz",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "schat2-clive",
|
"name": "schat2-clive",
|
||||||
"version": "1.8.0",
|
"version": "1.7.7",
|
||||||
"description": "Game host for SChat 2-powered chat sites",
|
"description": "Game host for SChat 2-powered chat sites",
|
||||||
"main": "src/app.js",
|
"main": "src/app.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
@ -22,9 +22,6 @@
|
||||||
"bottleneck": "^2.19.5",
|
"bottleneck": "^2.19.5",
|
||||||
"config": "^3.3.6",
|
"config": "^3.3.6",
|
||||||
"html-entities": "^2.3.2",
|
"html-entities": "^2.3.2",
|
||||||
"irc": "^0.5.2",
|
|
||||||
"irc-colors": "^1.5.0",
|
|
||||||
"irc-upd": "^0.11.0",
|
|
||||||
"jsdom": "^18.1.0",
|
"jsdom": "^18.1.0",
|
||||||
"linkify-it": "^3.0.3",
|
"linkify-it": "^3.0.3",
|
||||||
"simple-node-logger": "^21.8.12",
|
"simple-node-logger": "^21.8.12",
|
||||||
|
|
342
src/app.js
342
src/app.js
|
@ -1,15 +1,343 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const config = require('config');
|
const config = require('config');
|
||||||
|
const { setTimeout: delay } = require('timers/promises');
|
||||||
|
const bhttp = require('bhttp');
|
||||||
|
const WebSocket = require('ws');
|
||||||
|
const fs = require('fs').promises;
|
||||||
|
const logger = require('simple-node-logger').createSimpleLogger();
|
||||||
|
const { argv } = require('yargs');
|
||||||
|
|
||||||
const initSchat = require('./schat');
|
const instance = process.env.NODE_APP_INSTANCE || 'main';
|
||||||
const initIrc = require('./irc');
|
const points = {};
|
||||||
|
|
||||||
if (config.platform === 'irc') {
|
logger.setLevel(argv.level || 'info');
|
||||||
initIrc();
|
|
||||||
return;
|
async function auth() {
|
||||||
|
const httpSession = bhttp.session();
|
||||||
|
const username = config.uniqueUsername ? `${config.user.username}-${new Date().getTime().toString().slice(-5)}` : config.user.username;
|
||||||
|
|
||||||
|
const res = await httpSession.post(`${config.api}/session`, {
|
||||||
|
...config.user,
|
||||||
|
username,
|
||||||
|
}, {
|
||||||
|
encodeJSON: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res.statusCode !== 200) {
|
||||||
|
throw new Error(`Failed to authenticate: ${res.body.toString()}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info(`Authenticated as '${username}' with ${config.api}`);
|
||||||
|
|
||||||
|
return {
|
||||||
|
user: res.body,
|
||||||
|
httpSession,
|
||||||
|
sessionCookie: res.headers['set-cookie'][0],
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.platform === 'schat') {
|
async function getWsId(httpSession) {
|
||||||
initSchat();
|
const res = await httpSession.get(`${config.api}/socket`);
|
||||||
|
|
||||||
|
if (res.statusCode !== 200) {
|
||||||
|
throw new Error(`Failed to retrieve WebSocket ID: ${res.body.toString()}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.body;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function setPoints(defaultKey, user, value, { mode = 'add', key } = {}) {
|
||||||
|
const gameKey = key || defaultKey;
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
logger.warn(`Failed to set ${gameKey} points for missing user`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const userKey = `${user.id}:${user.username}`;
|
||||||
|
|
||||||
|
if (!points[gameKey]) {
|
||||||
|
points[gameKey] = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mode === 'add') {
|
||||||
|
points[gameKey][userKey] = (points[gameKey][userKey] || 0) + value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mode === 'set') {
|
||||||
|
points[gameKey][userKey] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
await fs.writeFile(`./points-${instance}.json`, JSON.stringify(points, null, 4));
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPoints(game, rawUsername, { user, room, command }) {
|
||||||
|
const username = rawUsername?.replace(/^@/, '');
|
||||||
|
const gamePoints = points[command] || points[game.key];
|
||||||
|
|
||||||
|
const userPoints = username
|
||||||
|
? Object.entries(gamePoints || {}).find(([identifier]) => identifier.split(':')[1] === username)?.[1]
|
||||||
|
: gamePoints?.[`${user?.id}:${user?.username}`];
|
||||||
|
|
||||||
|
game.sendMessage(`${username ? `**${username}** has` : 'You have'} scored **${userPoints || 0}** points in ${game.name}, @${user.username}`, room.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getLeaderboard(game, { user, room, command }) {
|
||||||
|
const leaderboard = points[command] || points[game.key];
|
||||||
|
|
||||||
|
if (!leaderboard || Object.keys(leaderboard).length === 0) {
|
||||||
|
game.sendMessage(`No points scored in ${game.name} yet!`, room.id);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const curatedLeaderboard = Object.entries(leaderboard)
|
||||||
|
.sort(([, scoreA], [, scoreB]) => scoreB - scoreA)
|
||||||
|
.map(([userKey, score]) => {
|
||||||
|
const username = userKey.split(':')[1];
|
||||||
|
return `**${username === user.username ? '@' : ''}${username}** at **${score}** points`;
|
||||||
|
})
|
||||||
|
.slice(0, 10)
|
||||||
|
.join(', ');
|
||||||
|
|
||||||
|
game.sendMessage(`The top ${Math.min(Object.keys(leaderboard).length, 10)} *${game.name}* players are: ${curatedLeaderboard}`, room.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
function onConnect(data, bot) {
|
||||||
|
bot.socket.transmit('joinRooms', { rooms: config.channels });
|
||||||
|
}
|
||||||
|
|
||||||
|
function onRooms({ rooms, users }, bot) {
|
||||||
|
logger.info(`Joined ${rooms.map((room) => room.name).join(', ')}`);
|
||||||
|
|
||||||
|
/* eslint-disable no-param-reassign */
|
||||||
|
bot.rooms = rooms.reduce((acc, room) => ({ ...acc, [room.id]: room }), {});
|
||||||
|
bot.users = { ...bot.users, ...users };
|
||||||
|
/* eslint-enable no-param-reassign */
|
||||||
|
|
||||||
|
rooms.forEach((room) => {
|
||||||
|
bot.socket.transmit('message', {
|
||||||
|
roomId: room.id,
|
||||||
|
body: `Hi, I am ${config.user.username}, your game host!`,
|
||||||
|
style: config.style,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/* eslint-disable no-param-reassign */
|
||||||
|
function onJoin(data, bot) {
|
||||||
|
if (bot.rooms[data.roomId] && !bot.rooms[data.roomId].userIds?.includes(data.user.id)) {
|
||||||
|
bot.users[data.user.id] = data.user;
|
||||||
|
bot.rooms[data.roomId].userIds.push(data.user.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function onLeave(data, bot) {
|
||||||
|
if (bot.rooms[data.roomId]) {
|
||||||
|
bot.rooms[data.roomId].userIds = bot.rooms[data.roomId].userIds.filter((userId) => userId !== data.userId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function onMessage(message, bot, games) {
|
||||||
|
const body = message.originalBody || message.body;
|
||||||
|
const [, command, subcommand] = body?.match(new RegExp(`^${config.prefix}(\\w+)(?:\\:(\\w+))?`)) || [];
|
||||||
|
const user = bot.users[message.userId] || message.user;
|
||||||
|
const room = bot.rooms[message.roomId];
|
||||||
|
|
||||||
|
if (command) {
|
||||||
|
const args = body.split(/\s+/).slice(1);
|
||||||
|
const game = games[command];
|
||||||
|
|
||||||
|
if (['leaderboard', 'lead', 'leader', 'leaders', 'scoreboard', 'best'].includes(subcommand) && games[command]) {
|
||||||
|
getLeaderboard(games[command], { user, room, command });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (['points', 'score'].includes(subcommand) && games[command]) {
|
||||||
|
getPoints(games[command], args[0], { user, room, command });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (game && game.onCommand) {
|
||||||
|
if (user) {
|
||||||
|
user.points = points[game.key]?.[`${user.id}:${user.username}`] || 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
game.onCommand(args, {
|
||||||
|
...game,
|
||||||
|
command,
|
||||||
|
subcommand,
|
||||||
|
bot,
|
||||||
|
message,
|
||||||
|
user,
|
||||||
|
room,
|
||||||
|
points: points[game.key] || {},
|
||||||
|
logger,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.values(games).forEach((game) => game.onMessage?.(message, {
|
||||||
|
...game,
|
||||||
|
bot,
|
||||||
|
message,
|
||||||
|
user: user && {
|
||||||
|
...user,
|
||||||
|
points: points[game.key]?.[`${user.id}:${user.username}`] || 0,
|
||||||
|
},
|
||||||
|
room,
|
||||||
|
logger,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
const messageHandlers = {
|
||||||
|
connect: onConnect,
|
||||||
|
rooms: onRooms,
|
||||||
|
message: onMessage,
|
||||||
|
join: onJoin,
|
||||||
|
leave: onLeave,
|
||||||
|
};
|
||||||
|
|
||||||
|
async function initPoints() {
|
||||||
|
try {
|
||||||
|
const pointsFile = await fs.readFile(`./points-${instance}.json`, 'utf-8');
|
||||||
|
|
||||||
|
Object.assign(points, JSON.parse(pointsFile));
|
||||||
|
} catch (error) {
|
||||||
|
if (error.code === 'ENOENT') {
|
||||||
|
logger.info('Creating new points file');
|
||||||
|
|
||||||
|
await fs.writeFile(`./points-${instance}.json`, '{}');
|
||||||
|
initPoints();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getGames(bot) {
|
||||||
|
const games = config.games.reduce((acc, key) => {
|
||||||
|
const game = require(`./games/${key.game || key}`); // eslint-disable-line global-require, import/no-dynamic-require
|
||||||
|
|
||||||
|
const sendMessage = (body, roomId, options, recipient) => {
|
||||||
|
bot.socket.transmit('message', {
|
||||||
|
roomId,
|
||||||
|
recipient,
|
||||||
|
type: recipient ? 'whisper' : 'message',
|
||||||
|
body: options?.label === false ? body : `[${game.name || key}] ${body}`,
|
||||||
|
style: config.style,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const setGamePoints = (userId, score, options) => setPoints(key, userId, score, options);
|
||||||
|
|
||||||
|
const curatedGame = {
|
||||||
|
...game,
|
||||||
|
...(key.game && key),
|
||||||
|
name: game.name || key,
|
||||||
|
key,
|
||||||
|
sendMessage,
|
||||||
|
setPoints: setGamePoints,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (game.onStart) {
|
||||||
|
game.onStart({ ...curatedGame, bot });
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...acc,
|
||||||
|
[key]: curatedGame,
|
||||||
|
...game.commands?.reduce((commandAcc, command) => ({ ...commandAcc, [command]: curatedGame }), {}),
|
||||||
|
};
|
||||||
|
}, {});
|
||||||
|
|
||||||
|
return games;
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleError(error, socket, domain, data) {
|
||||||
|
logger.error(`${domain} '${JSON.stringify(data)}' triggered error: ${error.message} ${error.stack}`);
|
||||||
|
|
||||||
|
if (data?.roomId) {
|
||||||
|
socket.transmit('message', {
|
||||||
|
body: ':zap::robot::zap: Many fragments! Some large, some small.',
|
||||||
|
type: 'message',
|
||||||
|
roomId: data.roomId,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function connect(bot, games) {
|
||||||
|
const socket = { ws: { readyState: 0 } };
|
||||||
|
|
||||||
|
socket.connect = async () => {
|
||||||
|
try {
|
||||||
|
const { user, httpSession, sessionCookie } = await auth();
|
||||||
|
const wsCreds = await getWsId(httpSession);
|
||||||
|
|
||||||
|
bot.user = user;
|
||||||
|
bot.httpSession = httpSession;
|
||||||
|
|
||||||
|
logger.info(`Attempting to connect to ${config.socket}`);
|
||||||
|
|
||||||
|
socket.ws = new WebSocket(`${config.socket}?${new URLSearchParams({ v: wsCreds.wsId, t: wsCreds.timestamp }).toString()}`, [], {
|
||||||
|
headers: {
|
||||||
|
cookie: sessionCookie,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.ws.on('message', async (msg) => {
|
||||||
|
const [domain, data] = JSON.parse(msg);
|
||||||
|
|
||||||
|
logger.debug(`Received ${domain}: ${JSON.stringify(data)}`);
|
||||||
|
|
||||||
|
if (messageHandlers[domain]) {
|
||||||
|
try {
|
||||||
|
await messageHandlers[domain](data, bot, games);
|
||||||
|
} catch (error) {
|
||||||
|
handleError(error, socket, domain, data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.ws.on('close', async (info) => {
|
||||||
|
logger.error(`WebSocket closed, reconnecting in ${config.reconnectDelay} seconds: ${info}`);
|
||||||
|
|
||||||
|
await delay(config.reconnectDelay * 1000);
|
||||||
|
socket.connect();
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.ws.on('error', async (error) => {
|
||||||
|
logger.error(`WebSocket error: ${error.message}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
logger.info(`Connected to ${config.socket}`);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(`Failed to connect, retrying in ${config.reconnectDelay} seconds: ${error.message}`);
|
||||||
|
|
||||||
|
await delay(config.reconnectDelay * 1000);
|
||||||
|
socket.connect();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
socket.transmit = (domain, data) => {
|
||||||
|
socket.ws.send(JSON.stringify([domain, data]));
|
||||||
|
};
|
||||||
|
|
||||||
|
socket.connect();
|
||||||
|
|
||||||
|
return socket;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function init() {
|
||||||
|
const bot = {
|
||||||
|
rooms: [],
|
||||||
|
users: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
const games = getGames(bot);
|
||||||
|
|
||||||
|
bot.socket = await connect(bot, games);
|
||||||
|
|
||||||
|
await initPoints();
|
||||||
|
}
|
||||||
|
|
||||||
|
init();
|
||||||
|
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
const config = require('config');
|
const config = require('config');
|
||||||
|
|
||||||
const style = require('../utils/style');
|
|
||||||
|
|
||||||
const ducks = new Map();
|
const ducks = new Map();
|
||||||
let shots = new Map();
|
let shots = new Map();
|
||||||
|
|
||||||
|
@ -24,7 +22,7 @@ function launchDuck(context) {
|
||||||
const room = rooms[Math.floor(Math.random() * rooms.length)];
|
const room = rooms[Math.floor(Math.random() * rooms.length)];
|
||||||
|
|
||||||
ducks.set(room.id, new Date());
|
ducks.set(room.id, new Date());
|
||||||
context.sendMessage(`Quack! ${config.duck.duck}`, room.id);
|
context.sendMessage('Quack! :duck:', room.id);
|
||||||
}, interval);
|
}, interval);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,7 +30,7 @@ function onCommand(args, context) {
|
||||||
const duck = ducks.get(context.room.id);
|
const duck = ducks.get(context.room.id);
|
||||||
|
|
||||||
if (!duck) {
|
if (!duck) {
|
||||||
context.sendMessage(`There is no duck, what are you shooting at, ${config.usernamePrefix}${context.user.username}?!`, context.room.id);
|
context.sendMessage(`There is no duck, what are you shooting at, @${context.user.username}?!`, context.room.id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,7 +39,7 @@ function onCommand(args, context) {
|
||||||
|
|
||||||
if (context.command === 'bang') {
|
if (context.command === 'bang') {
|
||||||
if (hit) {
|
if (hit) {
|
||||||
context.sendMessage(`You shot a duck in ${style.bold(`${time} seconds`)}, ${config.usernamePrefix}${context.user.username}`, context.room.id);
|
context.sendMessage(`You shot a duck in **${time} seconds**, @${context.user.username}`, context.room.id);
|
||||||
launchDuck(context);
|
launchDuck(context);
|
||||||
|
|
||||||
context.setPoints(context.user, 1, { key: 'bang' });
|
context.setPoints(context.user, 1, { key: 'bang' });
|
||||||
|
@ -50,10 +48,10 @@ function onCommand(args, context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const messages = [
|
const messages = [
|
||||||
`How could you miss *that*, ${config.usernamePrefix}${context.user.username}...?!`,
|
`How could you miss *that*, @${context.user.username}...?!`,
|
||||||
`That's a miss! Better luck next time, ${config.usernamePrefix}${context.user.username}.`,
|
`That's a miss! Better luck next time, @${context.user.username}.`,
|
||||||
`The duck outsmarted you, ${config.usernamePrefix}${context.user.username}`,
|
`The duck outsmarted you, @${context.user.username}`,
|
||||||
`Channeling Gareth Southgate, ${config.usernamePrefix}${context.user.username}? You missed!`,
|
`Channeling Gareth Southgate, @${context.user.username}? You missed!`,
|
||||||
];
|
];
|
||||||
|
|
||||||
shots.set(context.user.id, new Date());
|
shots.set(context.user.id, new Date());
|
||||||
|
@ -64,7 +62,7 @@ function onCommand(args, context) {
|
||||||
|
|
||||||
if (['bef', 'befriend'].includes(context.command)) {
|
if (['bef', 'befriend'].includes(context.command)) {
|
||||||
if (hit) {
|
if (hit) {
|
||||||
context.sendMessage(`You befriended a duck in ${style.bold(`${time} seconds`)}, ${config.usernamePrefix}${context.user.username}`, context.room.id);
|
context.sendMessage(`You befriended a duck in **${time} seconds**, @${context.user.username}`, context.room.id);
|
||||||
launchDuck(context);
|
launchDuck(context);
|
||||||
|
|
||||||
context.setPoints(context.user, 1, { key: 'befriend' });
|
context.setPoints(context.user, 1, { key: 'befriend' });
|
||||||
|
@ -73,9 +71,9 @@ function onCommand(args, context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const messages = [
|
const messages = [
|
||||||
`The duck does not want to be your friend right now, ${config.usernamePrefix}${context.user.username}`,
|
`The duck does not want to be your friend right now, @${context.user.username}`,
|
||||||
`The duck would like some time for itself, ${config.usernamePrefix}${context.user.username}`,
|
`The duck would like some time for itself, @${context.user.username}`,
|
||||||
`The duck isn't in the mood right now, ${config.usernamePrefix}${context.user.username}`,
|
`The duck isn't in the mood right now, @${context.user.username}`,
|
||||||
];
|
];
|
||||||
|
|
||||||
shots.set(context.user.id, new Date());
|
shots.set(context.user.id, new Date());
|
||||||
|
|
|
@ -2,17 +2,15 @@
|
||||||
|
|
||||||
const config = require('config');
|
const config = require('config');
|
||||||
|
|
||||||
const style = require('../utils/style');
|
|
||||||
const words = require('../../assets/mash-words.json');
|
const words = require('../../assets/mash-words.json');
|
||||||
|
|
||||||
const mashes = new Map();
|
let mash = null;
|
||||||
|
|
||||||
function getWordKey(word) {
|
function getWordKey(word) {
|
||||||
return word.split('').sort().join('');
|
return word.split('').sort().join('');
|
||||||
}
|
}
|
||||||
|
|
||||||
function start(length, context, attempt = 0) {
|
function start(length, context, attempt = 0) {
|
||||||
const mash = mashes.get(context.room.id);
|
|
||||||
const lengthWords = words[length];
|
const lengthWords = words[length];
|
||||||
|
|
||||||
if (!lengthWords) {
|
if (!lengthWords) {
|
||||||
|
@ -22,10 +20,10 @@ function start(length, context, attempt = 0) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mash) {
|
if (mash) {
|
||||||
context.sendMessage(`The mash ${style.bold(mash.anagram)} was not guessed, possible answers: ${mash.answers.map((answer) => style.bold(answer.word)).join(', ')}`, context.room.id);
|
context.sendMessage(`The mash **${mash.anagram}** was not guessed, possible answers: ${mash.answers.map((answer) => `**${answer.word}**`).join(', ')}`, context.room.id);
|
||||||
context.logger.info(`Mash '${mash.anagram}' discarded`);
|
context.logger.info(`Mash '${mash.anagram}' discarded`);
|
||||||
|
|
||||||
mashes.delete(context.room.id);
|
mash = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const wordEntries = Object.entries(lengthWords);
|
const wordEntries = Object.entries(lengthWords);
|
||||||
|
@ -44,48 +42,45 @@ function start(length, context, attempt = 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
mashes.set(context.room.id, { key, anagram, answers });
|
mash = { key, anagram, answers };
|
||||||
|
|
||||||
const newMash = mashes.get(context.room.id);
|
context.sendMessage(`Stomp stomp, here's your mash: **${mash.anagram}**`, context.room.id);
|
||||||
|
|
||||||
context.sendMessage(`Stomp stomp, here's your mash: ${style.bold(style.purple(newMash.anagram))}`, context.room.id);
|
|
||||||
context.logger.info(`Mash started, '${anagram}' with answers ${answers.map((answer) => `'${answer.word}'`).join(', ')}`);
|
context.logger.info(`Mash started, '${anagram}' with answers ${answers.map((answer) => `'${answer.word}'`).join(', ')}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
function play(rawWord, context, shouted) {
|
function play(rawWord, context, shouted) {
|
||||||
const mash = mashes.get(context.room.id);
|
|
||||||
const word = rawWord.toLowerCase();
|
const word = rawWord.toLowerCase();
|
||||||
const key = getWordKey(word);
|
const key = getWordKey(word);
|
||||||
const answer = mash.answers.find((answerX) => answerX.word === word);
|
const answer = mash.answers.find((answerX) => answerX.word === word);
|
||||||
|
|
||||||
if (!shouted) {
|
if (!shouted) {
|
||||||
if (word.length !== mash.key.length) {
|
if (word.length !== mash.key.length) {
|
||||||
context.sendMessage(`Your answer needs to be ${mash.key.length} letters, ${config.usernamePrefix}${context.user.username}`, context.room.id);
|
context.sendMessage(`Your answer needs to be ${mash.key.length} letters, @${context.user.username}`, context.room.id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (key !== mash.key) {
|
if (key !== mash.key) {
|
||||||
context.sendMessage(`You are not using the letters in ${style.bold(mash.anagram)}, ${config.usernamePrefix}${context.user.username}`, context.room.id);
|
context.sendMessage(`You are not using the letters in **${mash.anagram}**, @${context.user.username}`, context.room.id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (word === mash.anagram) {
|
if (word === mash.anagram) {
|
||||||
context.sendMessage(`${config.usernamePrefix}${context.user.username}... :expressionless:`, context.room.id);
|
context.sendMessage(`@${context.user.username}... :expressionless:`, context.room.id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (answer) {
|
if (answer) {
|
||||||
const definition = answer.definitions[0] ? `: ${style.italic(`${answer.definitions[0].slice(0, 100)}${mash.answers[0].definitions[0].length > 100 ? '...' : ''}`)}` : '';
|
const definition = answer.definitions[0] ? `: *${answer.definitions[0].slice(0, 100)}${mash.answers[0].definitions[0].length > 100 ? '...*' : '*'}` : '';
|
||||||
|
|
||||||
context.sendMessage(mash.answers.length === 1
|
context.sendMessage(mash.answers.length === 1
|
||||||
? `${style.bold(style.yellow(word))} is the right answer${definition}, ${style.bold(style.cyan(`${config.usernamePrefix}${context.user.username}`))} now has ${style.bold(`${context.user.points + 1} ${context.user.points === 0 ? 'point' : 'points'}`)}! There were no other options for ${style.bold(mash.anagram)}.`
|
? `**${word}** is the right answer${definition}, @${context.user.username} now has **${context.user.points + 1} ${context.user.points === 0 ? 'point' : 'points'}**! There were no other options for **${mash.anagram}**.`
|
||||||
: `${style.bold(style.yellow(word))} is the right answer${definition}, ${style.bold(style.cyan(context.user.username))} now has ${style.bold(`${context.user.points + 1} ${context.user.points === 0 ? 'point' : 'points'}`)}! Other options for ${style.bold(mash.anagram)}: ${mash.answers.filter((answerX) => answerX.word !== word).map((answerX) => style.italic(answerX.word)).join(', ')}`, context.room.id); // eslint-disable-line max-len
|
: `**${word}** is the right answer${definition}, @${context.user.username} now has **${context.user.points + 1} ${context.user.points === 0 ? 'point' : 'points'}**! Other options for **${mash.anagram}**: ${mash.answers.filter((answerX) => answerX.word !== word).map((answerX) => `*${answerX.word}*`).join(', ')}`, context.room.id);
|
||||||
|
|
||||||
context.logger.info(`Mash '${mash.anagram}' guessed by '${context.user.username}' with '${word}'`);
|
context.logger.info(`Mash '${mash.anagram}' guessed by '${context.user.username}' with '${word}'`);
|
||||||
context.setPoints(context.user, 1);
|
context.setPoints(context.user, 1);
|
||||||
|
|
||||||
mashes.delete(context.room.id);
|
mash = null;
|
||||||
|
|
||||||
setTimeout(() => start(word.length, context), 2000);
|
setTimeout(() => start(word.length, context), 2000);
|
||||||
}
|
}
|
||||||
|
@ -93,7 +88,7 @@ function play(rawWord, context, shouted) {
|
||||||
|
|
||||||
function resolve(word, context) {
|
function resolve(word, context) {
|
||||||
if (!word) {
|
if (!word) {
|
||||||
context.sendMessage(`Please specify an anagram you would like to resolve, ${config.usernamePrefix}${context.user.username}`, context.room.id);
|
context.sendMessage(`Please specify an anagram you would like to resolve, @${context.user.username}`, context.room.id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,26 +96,26 @@ function resolve(word, context) {
|
||||||
const answers = words[word.length]?.[anagram];
|
const answers = words[word.length]?.[anagram];
|
||||||
|
|
||||||
if (answers?.length > 1 && answers.some((answer) => answer.word === word)) {
|
if (answers?.length > 1 && answers.some((answer) => answer.word === word)) {
|
||||||
context.sendMessage(`${style.bold(word)} is a valid word in itself, and has the following anagrams, ${config.usernamePrefix}${context.user.username}: ${answers.filter((answer) => answer.word !== word).map((answer) => style.italic(answer.word)).join(', ')}`, context.room.id);
|
context.sendMessage(`**${word}** is a valid word in itself, and has the following anagrams, @${context.user.username}: ${answers.filter((answer) => answer.word !== word).map((answer) => `*${answer.word}*`).join(', ')}`, context.room.id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (answers?.length === 1 && answers[0].word === word) {
|
if (answers?.length === 1 && answers[0].word === word) {
|
||||||
context.sendMessage(`${style.bold(word)} is a valid word in itself, but has no anagrams, ${config.usernamePrefix}${context.user.username}`, context.room.id);
|
context.sendMessage(`**${word}** is a valid word in itself, but has no anagrams, @${context.user.username}`, context.room.id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (answers?.length > 0) {
|
if (answers?.length > 0) {
|
||||||
context.sendMessage(`Anagrams of ${style.bold(word)}, ${config.usernamePrefix}${context.user.username}: ${answers.map((answer) => style.italic(answer.word)).join(', ')}`, context.room.id);
|
context.sendMessage(`Anagrams of **${word}**, @${context.user.username}: ${answers.map((answer) => `*${answer.word}*`).join(', ')}`, context.room.id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
context.sendMessage(`No anagrams found for ${style.bold(word)}, ${config.usernamePrefix}${context.user.username}`, context.room.id);
|
context.sendMessage(`No anagrams found for **${word}**, @${context.user.username}`, context.room.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
function define(word, context) {
|
function define(word, context) {
|
||||||
if (!word) {
|
if (!word) {
|
||||||
context.sendMessage(`Please specify word you would like to define, ${config.usernamePrefix}${context.user.username}`, context.room.id);
|
context.sendMessage(`Please specify word you would like to define, @${context.user.username}`, context.room.id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,36 +124,33 @@ function define(word, context) {
|
||||||
const answer = answers?.find((answerX) => answerX.word === word);
|
const answer = answers?.find((answerX) => answerX.word === word);
|
||||||
|
|
||||||
if (answer && answer.definitions?.length > 0) {
|
if (answer && answer.definitions?.length > 0) {
|
||||||
context.sendMessage(`${word} can be defined as follows, ${config.usernamePrefix}${context.user.username}: ${style.italic(answer.definitions[0])}`, context.room.id);
|
context.sendMessage(`${word} can be defined as follows, @${context.user.username}: *${answer.definitions[0]}*`, context.room.id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
context.sendMessage(`No definition available for ${style.bold(word)}, ${config.usernamePrefix}${context.user.username}`, context.room.id);
|
context.sendMessage(`No definition available for **${word}**, @${context.user.username}`, context.room.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
function hint(context) {
|
function hint(context) {
|
||||||
const mash = mashes.get(context.room.id);
|
|
||||||
|
|
||||||
if (!mash) {
|
if (!mash) {
|
||||||
context.sendMessage(`There is no mash going on right now, ${config.usernamePrefix}${context.user.username}. Start one with ${config.prefix}mash {length}`, context.room.id);
|
context.sendMessage(`There is no mash going on right now, @${context.user.username}. Start one with ${config.prefix}mash {length}`, context.room.id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mash.anagram.length <= 3) {
|
if (mash.anagram.length <= 3) {
|
||||||
context.sendMessage(`The mash ${style.bold(mash.anagram)} is too short for a hint, ${config.usernamePrefix}${context.user.username}.`, context.room.id);
|
context.sendMessage(`The mash **${mash.anagram}** is too short for a hint, @${context.user.username}.`, context.room.id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mash.anagram.length === 4) {
|
if (mash.anagram.length === 4) {
|
||||||
context.sendMessage(`Hints for ${style.bold(style.purple(mash.anagram))}, ${config.usernamePrefix}${context.user.username}: ${mash.answers.map((answer) => `${style.bold(`${answer.word.slice(0, 1)} ${'_ '.repeat(answer.word.length - 1).trim()}`)} (${answer.definitions[0]})`).join(', ')}`, context.room.id);
|
context.sendMessage(`Hints for **${mash.anagram}**, @${context.user.username}: ${mash.answers.map((answer) => `**${answer.word.slice(0, 1)} ${'_ '.repeat(answer.word.length - 1).trim()}** (${answer.definitions[0]})`).join(', ')}`, context.room.id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
context.sendMessage(`Hints for ${style.bold(mash.anagram)}, ${config.usernamePrefix}${context.user.username}: ${mash.answers.map((answer) => `${style.bold(`${answer.word.slice(0, 1)} ${'_ '.repeat(answer.word.length - 2)}${answer.word.slice(-1)}`)} (${answer.definitions[0]})`).join(', ')}`, context.room.id);
|
context.sendMessage(`Hints for **${mash.anagram}**, @${context.user.username}: ${mash.answers.map((answer) => `**${answer.word.slice(0, 1)} ${'_ '.repeat(answer.word.length - 2)}${answer.word.slice(-1)}** (${answer.definitions[0]})`).join(', ')}`, context.room.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
function onCommand(args, context) {
|
function onCommand(args, context) {
|
||||||
const mash = mashes.get(context.room.id);
|
|
||||||
const word = args[0];
|
const word = args[0];
|
||||||
const length = Number(word);
|
const length = Number(word);
|
||||||
|
|
||||||
|
@ -183,7 +175,7 @@ function onCommand(args, context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!word && mash) {
|
if (!word && mash) {
|
||||||
context.sendMessage(`The current mash is: ${style.bold(style.purple(mash.anagram))}`, context.room.id);
|
context.sendMessage(`The current mash is: **${mash.anagram}**`, context.room.id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -196,9 +188,7 @@ function onCommand(args, context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function onMessage(message, context) {
|
function onMessage(message, context) {
|
||||||
const mash = mashes.get(context.room?.id);
|
if (mash && context.user?.id !== config.user.id) {
|
||||||
|
|
||||||
if (mash && message.type === 'message' && context.user?.id !== config.user.id) {
|
|
||||||
play(message.body, context, true);
|
play(message.body, context, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,33 +7,15 @@ function onCommand(args, context) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.platform === 'irc' && /^#+/.test(args[0])) {
|
const message = context.room ? args.join(' ') : args.slice(1).join(' ');
|
||||||
context.sendMessage(args.slice(1).join(' '), args[0], { label: false });
|
const roomName = args[0].replace(/#+/, '');
|
||||||
|
const room = context.room || Object.values(context.bot.rooms).find((botRoom) => botRoom.name === roomName);
|
||||||
|
|
||||||
|
if (!room) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config.platform === 'irc' && context.room.id === config.user.nick) {
|
context.sendMessage(message, room.id, { label: false });
|
||||||
// if the room ID is the bot's own nickname, it's a PM and we should reply to the sender
|
|
||||||
context.sendMessage(args.join(' '), context.user.id, { label: false });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (config.platform === 'schat' && !context.room) {
|
|
||||||
const roomName = args[0].replace(/#+/, '');
|
|
||||||
const room = Object.values(context.bot.rooms).find((botRoom) => botRoom.name === roomName);
|
|
||||||
|
|
||||||
if (room) {
|
|
||||||
context.sendMessage(args.slice(1).join(' '), room.id, { label: false });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (context.message.recipient === config.user.username) {
|
|
||||||
context.sendMessage(args.join(' '), null, { type: 'message', label: false }, context.user.username);
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
context.sendMessage(args.join(' '), context.room.id, { label: false });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
|
@ -6,7 +6,6 @@ const { decode } = require('html-entities');
|
||||||
|
|
||||||
const questions = require('../../assets/jeopardy.json');
|
const questions = require('../../assets/jeopardy.json');
|
||||||
const shuffle = require('../utils/shuffle');
|
const shuffle = require('../utils/shuffle');
|
||||||
const style = require('../utils/style');
|
|
||||||
|
|
||||||
const settings = { ...config.trivia };
|
const settings = { ...config.trivia };
|
||||||
const help = {
|
const help = {
|
||||||
|
@ -15,12 +14,9 @@ const help = {
|
||||||
timeout: 'seconds as a number',
|
timeout: 'seconds as a number',
|
||||||
};
|
};
|
||||||
|
|
||||||
// let game = null;
|
let game = null;
|
||||||
const games = new Map();
|
|
||||||
|
|
||||||
function scoreRound(context, round) {
|
function scoreRound(context, round) {
|
||||||
const game = games.get(context.room.id);
|
|
||||||
|
|
||||||
if (game.answers.size === 0) {
|
if (game.answers.size === 0) {
|
||||||
return `No one scored in round ${round + 1}, better luck next time!`;
|
return `No one scored in round ${round + 1}, better luck next time!`;
|
||||||
}
|
}
|
||||||
|
@ -30,27 +26,24 @@ function scoreRound(context, round) {
|
||||||
context.setPoints(user, 1);
|
context.setPoints(user, 1);
|
||||||
game.points[user.username] = (game.points[user.username] || 0) + 1;
|
game.points[user.username] = (game.points[user.username] || 0) + 1;
|
||||||
|
|
||||||
return `${style.bold(style.cyan(`${config.usernamePrefix}${user.username}`))} gets a point`;
|
return `**@${user.username}** gets a point`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}).filter(Boolean).join(', ');
|
}).filter(Boolean).join(', ');
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLeaders(context) {
|
function getLeaders() {
|
||||||
const game = games.get(context.room.id);
|
|
||||||
|
|
||||||
return Object.entries(game.points).sort(([, scoreA], [, scoreB]) => scoreB - scoreA).map(([username, score], index) => {
|
return Object.entries(game.points).sort(([, scoreA], [, scoreB]) => scoreB - scoreA).map(([username, score], index) => {
|
||||||
if (index === 0) {
|
if (index === 0) {
|
||||||
return `${style.bold(`${config.usernamePrefix}${username}`)} with ${style.bold(`${score}`)} points`;
|
return `**@${username}** with **${score}** points`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return `${style.bold(style.cyan(`${config.usernamePrefix}${username}`))} with ${style.bold(`${score}`)} points`;
|
return `**@${username}** with **${score}** points`;
|
||||||
}).join(', ');
|
}).join(', ');
|
||||||
}
|
}
|
||||||
|
|
||||||
async function playRound(context, round = 0) {
|
async function playRound(context, round = 0) {
|
||||||
const game = games.get(context.room.id);
|
|
||||||
const ac = new AbortController(); // eslint-disable-line no-undef
|
const ac = new AbortController(); // eslint-disable-line no-undef
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
|
|
||||||
|
@ -60,7 +53,7 @@ async function playRound(context, round = 0) {
|
||||||
|
|
||||||
const question = game.questions[round];
|
const question = game.questions[round];
|
||||||
|
|
||||||
context.sendMessage(`${style.bold(style.purple(`Question ${round + 1}/${game.questions.length}`))} ${style.silver(`(${question.category})`)}: ${question.question}`, context.room.id);
|
context.sendMessage(`**Question ${round + 1}/${game.questions.length}** (${question.category}): ${question.question}`, context.room.id);
|
||||||
context.logger.info(`Trivia asked "${question.question}" with answer: ${question.answer}`);
|
context.logger.info(`Trivia asked "${question.question}" with answer: ${question.answer}`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -69,14 +62,14 @@ async function playRound(context, round = 0) {
|
||||||
});
|
});
|
||||||
|
|
||||||
// replace space with U+2003 Em Space to separate words, since a single space separates the placeholders, and double spaces are removed during Markdown render
|
// replace space with U+2003 Em Space to separate words, since a single space separates the placeholders, and double spaces are removed during Markdown render
|
||||||
context.sendMessage(`${style.bold(style.green(`${Math.floor(game.timeout / 3) * 2} seconds`))} left, first hint for ${style.bold(style.purple(`question ${round + 1}/${game.questions.length}`))}: ${style.bold(`${question.answer.slice(0, 1)} ${question.answer.slice(1).replace(/\s/g, ' ').replace(/[^\s]/g, '_ ').trim()}`)}`, context.room.id);
|
context.sendMessage(`**${Math.floor(game.timeout / 3) * 2} seconds** left, first hint for **question ${round + 1}/${game.questions.length}**: **${question.answer.slice(0, 1)} ${question.answer.slice(1).replace(/\s/g, ' ').replace(/[^\s]/g, '_ ').trim()}**`, context.room.id);
|
||||||
|
|
||||||
await timers.setTimeout((game.timeout / 3) * 1000, null, {
|
await timers.setTimeout((game.timeout / 3) * 1000, null, {
|
||||||
signal: ac.signal,
|
signal: ac.signal,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (question.answer.length > 3) {
|
if (question.answer.length > 3) {
|
||||||
context.sendMessage(`${style.bold(style.green(`${Math.floor(game.timeout / 3)} seconds`))} left, second hint for ${style.bold(style.purple(`question ${round + 1}/${game.questions.length}`))}: ${style.bold(`${question.answer.slice(0, 1)} ${question.answer.slice(1, -1).replace(/\s/g, ' ').replace(/[^\s]/g, '_ ')}${question.answer.slice(-1)}`)}`, context.room.id);
|
context.sendMessage(`**${Math.floor(game.timeout / 3)} seconds** left, second hint for **question ${round + 1}/${game.questions.length}**: **${question.answer.slice(0, 1)} ${question.answer.slice(1, -1).replace(/\s/g, ' ').replace(/[^\s]/g, '_ ')}${question.answer.slice(-1)}**`, context.room.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
await timers.setTimeout((game.timeout / 3) * 1000, null, {
|
await timers.setTimeout((game.timeout / 3) * 1000, null, {
|
||||||
|
@ -91,23 +84,23 @@ async function playRound(context, round = 0) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (game.stopped) {
|
if (game.stopped) {
|
||||||
context.sendMessage(`The game was stopped by ${style.cyan(`${config.usernamePrefix}${game.stopped.username}`)}. The answer to the last question was: ${style.bold(question.answer)}. Best players: ${getLeaders(context)}`, context.room.id);
|
context.sendMessage(`The game was stopped by @${game.stopped.username}. The answer to the last question was: **${question.answer}**. Best players: ${getLeaders()}`, context.room.id);
|
||||||
games.delete(context.room.id);
|
game = null;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (game.answers.size === 0) {
|
if (game.answers.size === 0) {
|
||||||
context.sendMessage(`${style.bold(style.red('TIME\'S UP!'))} No one guessed the answer: ${style.bold(question.answer)}`, context.room.id);
|
context.sendMessage(`**TIME'S UP!** No one guessed the answer: **${question.answer}**`, context.room.id);
|
||||||
} else {
|
} else {
|
||||||
const scores = scoreRound(context, round);
|
const scores = scoreRound(context, round);
|
||||||
|
|
||||||
if (game.mode === 'first') {
|
if (game.mode === 'first') {
|
||||||
context.sendMessage(`${style.bold(style.yellow(question.fullAnswer || question.answer))} is the right answer, played in ${style.bold(style.green(`${((new Date() - now) / 1000).toFixed(3)}s`))}! ${scores}`, context.room.id);
|
context.sendMessage(`**${question.fullAnswer || question.answer}** is the right answer, played in **${((new Date() - now) / 1000).toFixed(3)}s**! ${scores}`, context.room.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (game.mode === 'timeout') {
|
if (game.mode === 'timeout') {
|
||||||
context.sendMessage(`${style.bold(style.red('STOP!'))} The correct answer is ${style.bold(style.green(question.fullAnswer || question.answer))}. ${scores}`, context.room.id);
|
context.sendMessage(`**STOP!** The correct answer is **${question.fullAnswer || question.answer}**. ${scores}`, context.room.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,8 +108,8 @@ async function playRound(context, round = 0) {
|
||||||
await timers.setTimeout(5000);
|
await timers.setTimeout(5000);
|
||||||
|
|
||||||
if (game.stopped) {
|
if (game.stopped) {
|
||||||
context.sendMessage(`The game was stopped by ${config.usernamePrefix}${game.stopped.username}. The answer to the last question was: ${style.bold(question.answer)}. Best players: ${getLeaders(context)}`, context.room.id);
|
context.sendMessage(`The game was stopped by @${game.stopped.username}. The answer to the last question was: **${question.answer}**. Best players: ${getLeaders()}`, context.room.id);
|
||||||
games.delete(context.room.id);
|
game = null;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -127,35 +120,31 @@ async function playRound(context, round = 0) {
|
||||||
|
|
||||||
await timers.setTimeout(2000);
|
await timers.setTimeout(2000);
|
||||||
|
|
||||||
context.sendMessage(`That's the end of the game! Best players: ${getLeaders(context)}`, context.room.id);
|
context.sendMessage(`That's the end of the game! Best players: ${getLeaders()}`, context.room.id);
|
||||||
|
|
||||||
games.delete(context.room.id);
|
game = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function start(context) {
|
async function start(context) {
|
||||||
const roundQuestions = shuffle(questions, settings.rounds);
|
const roundQuestions = shuffle(questions, settings.rounds);
|
||||||
|
|
||||||
games.set(context.room.id, {
|
game = {
|
||||||
round: 0,
|
round: 0,
|
||||||
questions: roundQuestions,
|
questions: roundQuestions,
|
||||||
answers: [],
|
answers: [],
|
||||||
points: {},
|
points: {},
|
||||||
...settings,
|
...settings,
|
||||||
});
|
};
|
||||||
|
|
||||||
playRound(context, 0);
|
playRound(context, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function stop(context) {
|
async function stop(context) {
|
||||||
const game = games.get(context.room.id);
|
|
||||||
|
|
||||||
game.stopped = context.user;
|
game.stopped = context.user;
|
||||||
game.ac.abort();
|
game.ac.abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
function onCommand(args, context) {
|
function onCommand(args, context) {
|
||||||
const game = games.get(context.room.id);
|
|
||||||
|
|
||||||
if (!context.subcommand && !game) {
|
if (!context.subcommand && !game) {
|
||||||
start(context);
|
start(context);
|
||||||
return;
|
return;
|
||||||
|
@ -192,8 +181,6 @@ function onCommand(args, context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onMessage(message, context) {
|
async function onMessage(message, context) {
|
||||||
const game = games.get(context.room?.id);
|
|
||||||
|
|
||||||
if (!game || context.user?.id === config.user?.id) {
|
if (!game || context.user?.id === config.user?.id) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -212,9 +199,9 @@ async function onMessage(message, context) {
|
||||||
|
|
||||||
if (settings.mode === 'timeout' && !game.ac.signal.aborted) {
|
if (settings.mode === 'timeout' && !game.ac.signal.aborted) {
|
||||||
if (message.type === 'message') {
|
if (message.type === 'message') {
|
||||||
context.sendMessage(`${style.bold(fullAnswer || answer)} is the correct answer! You might want to ${style.bold('/whisper')} the answer to me instead, so others can't leech off your impeccable knowledge. You will receive a point at the end of the round.`, context.room.id, null, context.user.username);
|
context.sendMessage(`**${fullAnswer || answer}** is the correct answer! You might want to **/whisper** the answer to me instead, so others can't leech off your impeccable knowledge. You will receive a point at the end of the round.`, context.room.id, null, context.user.username);
|
||||||
} else {
|
} else {
|
||||||
context.sendMessage(`${style.bold(fullAnswer || answer)} is the correct answer! You will receive a point at the end of the round.`, context.room.id, null, context.user.username);
|
context.sendMessage(`**${fullAnswer || answer}** is the correct answer! You will receive a point at the end of the round.`, context.room.id, null, context.user.username);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
64
src/irc.js
64
src/irc.js
|
@ -1,64 +0,0 @@
|
||||||
'use strict';
|
|
||||||
|
|
||||||
const config = require('config');
|
|
||||||
const irc = require('irc-upd');
|
|
||||||
const logger = require('simple-node-logger').createSimpleLogger();
|
|
||||||
const { argv } = require('yargs');
|
|
||||||
// const timers = require('timers/promises');
|
|
||||||
|
|
||||||
const {
|
|
||||||
onMessage,
|
|
||||||
getGames,
|
|
||||||
} = require('./play');
|
|
||||||
|
|
||||||
logger.setLevel(argv.level || 'info');
|
|
||||||
|
|
||||||
const client = new irc.Client(config.server, config.user.nick, {
|
|
||||||
userName: config.user.username,
|
|
||||||
realName: config.user.realName,
|
|
||||||
password: config.user.password,
|
|
||||||
port: config.port,
|
|
||||||
secure: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
async function init() {
|
|
||||||
const bot = {
|
|
||||||
client,
|
|
||||||
rooms: config.channels.map((channel) => ({
|
|
||||||
id: channel,
|
|
||||||
name: channel,
|
|
||||||
})),
|
|
||||||
};
|
|
||||||
|
|
||||||
const games = await getGames(bot, config.user.nick);
|
|
||||||
|
|
||||||
client.addListener('registered', () => {
|
|
||||||
logger.info('Connected!');
|
|
||||||
logger.info('Identifying with NickServ');
|
|
||||||
|
|
||||||
client.say('nickserv', `IDENTIFY ${config.user.username} ${config.user.password}`);
|
|
||||||
|
|
||||||
config.channels.forEach((channel) => {
|
|
||||||
logger.info(`Joining ${channel}`);
|
|
||||||
|
|
||||||
client.join(channel, () => logger.info(`Joined ${channel}`));
|
|
||||||
|
|
||||||
if (config.greeting) {
|
|
||||||
client.say(channel, config.greeting);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
client.addListener('message', (from, to, body) => onMessage({
|
|
||||||
from,
|
|
||||||
to,
|
|
||||||
body,
|
|
||||||
type: 'message',
|
|
||||||
}, bot, games));
|
|
||||||
|
|
||||||
client.addListener('error', (error) => {
|
|
||||||
logger.error(error);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = init;
|
|
225
src/play.js
225
src/play.js
|
@ -1,225 +0,0 @@
|
||||||
'use strict';
|
|
||||||
|
|
||||||
const config = require('config');
|
|
||||||
const fs = require('fs').promises;
|
|
||||||
const logger = require('simple-node-logger').createSimpleLogger();
|
|
||||||
const { argv } = require('yargs');
|
|
||||||
// const timers = require('timers/promises');
|
|
||||||
|
|
||||||
const style = require('./utils/style');
|
|
||||||
|
|
||||||
logger.setLevel(argv.level || 'info');
|
|
||||||
|
|
||||||
const points = {};
|
|
||||||
|
|
||||||
async function initPoints(identifier) {
|
|
||||||
try {
|
|
||||||
const pointsFile = await fs.readFile(`./points-${identifier}.json`, 'utf-8');
|
|
||||||
|
|
||||||
Object.assign(points, JSON.parse(pointsFile));
|
|
||||||
} catch (error) {
|
|
||||||
if (error.code === 'ENOENT') {
|
|
||||||
logger.info('Creating new points file');
|
|
||||||
|
|
||||||
await fs.writeFile(`./points-${identifier}.json`, '{}');
|
|
||||||
initPoints();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function setPoints(identifier, defaultKey, user, value, { mode = 'add', key } = {}) {
|
|
||||||
const gameKey = key || defaultKey;
|
|
||||||
|
|
||||||
if (!user) {
|
|
||||||
logger.warn(`Failed to set ${gameKey} points for missing user`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const userKey = `${user.id}:${user.username}`;
|
|
||||||
|
|
||||||
if (!points[gameKey]) {
|
|
||||||
points[gameKey] = {};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mode === 'add') {
|
|
||||||
points[gameKey][userKey] = (points[gameKey][userKey] || 0) + value;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mode === 'set') {
|
|
||||||
points[gameKey][userKey] = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
await fs.writeFile(`./points-${identifier}.json`, JSON.stringify(points, null, 4));
|
|
||||||
}
|
|
||||||
|
|
||||||
function getPoints(game, rawUsername, { user, room, command }) {
|
|
||||||
const username = rawUsername?.replace(new RegExp(`^${config.usernamePrefix}`), '');
|
|
||||||
const gamePoints = points[command] || points[game.key];
|
|
||||||
|
|
||||||
const userPoints = username
|
|
||||||
? Object.entries(gamePoints || {}).find(([identifier]) => identifier.split(':')[1] === username)?.[1]
|
|
||||||
: gamePoints?.[`${user?.id}:${user?.username}`];
|
|
||||||
|
|
||||||
game.sendMessage(`${username ? `${style.bold(username)} has` : 'You have'} scored ${style.bold(userPoints || 0)} points in ${game.name}, ${config.usernamePrefix}${user.username}`, room.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getLeaderboard(game, { user, room, command }) {
|
|
||||||
const leaderboard = points[command] || points[game.key];
|
|
||||||
|
|
||||||
if (!leaderboard || Object.keys(leaderboard).length === 0) {
|
|
||||||
game.sendMessage(`No points scored in ${game.name} yet!`, room.id);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const curatedLeaderboard = Object.entries(leaderboard)
|
|
||||||
.sort(([, scoreA], [, scoreB]) => scoreB - scoreA)
|
|
||||||
.map(([userKey, score]) => {
|
|
||||||
const username = userKey.split(':')[1];
|
|
||||||
return `${style.bold(`${username === user.username ? config.usernamePrefix : ''}${username}`)} at ${style.bold(score)} points`;
|
|
||||||
})
|
|
||||||
.slice(0, 10)
|
|
||||||
.join(', ');
|
|
||||||
|
|
||||||
game.sendMessage(`The top ${Math.min(Object.keys(leaderboard).length, 10)} ${style.italic(game.name)} players are: ${curatedLeaderboard}`, room.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getGames(bot, identifier) {
|
|
||||||
await initPoints(identifier);
|
|
||||||
|
|
||||||
const games = config.games.reduce((acc, key) => {
|
|
||||||
const game = require(`./games/${key.game || key}`); // eslint-disable-line global-require, import/no-dynamic-require
|
|
||||||
|
|
||||||
const sendMessage = (body, roomId, options, recipient) => {
|
|
||||||
const curatedBody = options?.label === false || config.labels === false ? body : `${style.grey(`[${game.name || key}]`)} ${body}`;
|
|
||||||
|
|
||||||
if (config.platform === 'irc') {
|
|
||||||
bot.client.say(roomId || recipient, curatedBody);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (config.platform === 'schat') {
|
|
||||||
bot.socket.transmit('message', {
|
|
||||||
roomId,
|
|
||||||
recipient,
|
|
||||||
type: recipient && options.type !== 'message' ? 'whisper' : 'message',
|
|
||||||
body: curatedBody,
|
|
||||||
style: config.style,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const setGamePoints = (userId, score, options) => setPoints(identifier, key, userId, score, options);
|
|
||||||
|
|
||||||
const curatedGame = {
|
|
||||||
...game,
|
|
||||||
...(key.game && key),
|
|
||||||
name: game.name || key,
|
|
||||||
key,
|
|
||||||
sendMessage,
|
|
||||||
setPoints: setGamePoints,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (game.onStart) {
|
|
||||||
game.onStart({ ...curatedGame, bot });
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
...acc,
|
|
||||||
[key]: curatedGame,
|
|
||||||
...game.commands?.reduce((commandAcc, command) => ({ ...commandAcc, [command]: curatedGame }), {}),
|
|
||||||
};
|
|
||||||
}, {});
|
|
||||||
|
|
||||||
return games;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getMessageUser(message, bot) {
|
|
||||||
if (config.platform === 'irc') {
|
|
||||||
return {
|
|
||||||
username: message.from,
|
|
||||||
id: message.from,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (config.platform === 'schat') {
|
|
||||||
return bot.users[message.userId] || message.user;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getMessageRoom(message, bot) {
|
|
||||||
if (config.platform === 'irc') {
|
|
||||||
return {
|
|
||||||
id: message.to,
|
|
||||||
name: message.to,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (config.platform === 'schat') {
|
|
||||||
return bot.rooms[message.roomId];
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function onMessage(message, bot, games) {
|
|
||||||
const body = message.originalBody || message.body;
|
|
||||||
const [, command, subcommand] = body?.match(new RegExp(`^${config.prefix}(\\w+)(?:\\:(\\w+))?`)) || [];
|
|
||||||
const user = getMessageUser(message, bot);
|
|
||||||
const room = getMessageRoom(message, bot);
|
|
||||||
|
|
||||||
if (command) {
|
|
||||||
const args = body.split(/\s+/).slice(1);
|
|
||||||
const game = games[command];
|
|
||||||
|
|
||||||
if (['leaderboard', 'lead', 'leader', 'leaders', 'scoreboard', 'best'].includes(subcommand) && games[command]) {
|
|
||||||
getLeaderboard(games[command], { user, room, command });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (['points', 'score'].includes(subcommand) && games[command]) {
|
|
||||||
getPoints(games[command], args[0], { user, room, command });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (game && game.onCommand) {
|
|
||||||
if (user) {
|
|
||||||
user.points = points[game.key]?.[`${user.id}:${user.username}`] || 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
game.onCommand(args, {
|
|
||||||
...game,
|
|
||||||
command,
|
|
||||||
subcommand,
|
|
||||||
bot,
|
|
||||||
message,
|
|
||||||
user,
|
|
||||||
room,
|
|
||||||
points: points[game.key] || {},
|
|
||||||
logger,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Object.values(games).forEach((game) => game.onMessage?.(message, {
|
|
||||||
...game,
|
|
||||||
bot,
|
|
||||||
message,
|
|
||||||
user: user && {
|
|
||||||
...user,
|
|
||||||
points: points[game.key]?.[`${user.id}:${user.username}`] || 0,
|
|
||||||
},
|
|
||||||
room,
|
|
||||||
logger,
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
onMessage,
|
|
||||||
getGames,
|
|
||||||
getLeaderboard,
|
|
||||||
getPoints,
|
|
||||||
setPoints,
|
|
||||||
initPoints,
|
|
||||||
};
|
|
181
src/schat.js
181
src/schat.js
|
@ -1,181 +0,0 @@
|
||||||
'use strict';
|
|
||||||
|
|
||||||
const config = require('config');
|
|
||||||
const { setTimeout: delay } = require('timers/promises');
|
|
||||||
const bhttp = require('bhttp');
|
|
||||||
const WebSocket = require('ws');
|
|
||||||
const logger = require('simple-node-logger').createSimpleLogger();
|
|
||||||
const { argv } = require('yargs');
|
|
||||||
|
|
||||||
const {
|
|
||||||
onMessage,
|
|
||||||
getGames,
|
|
||||||
} = require('./play');
|
|
||||||
|
|
||||||
const instance = process.env.NODE_APP_INSTANCE || 'main';
|
|
||||||
|
|
||||||
logger.setLevel(argv.level || 'info');
|
|
||||||
|
|
||||||
async function auth() {
|
|
||||||
const httpSession = bhttp.session();
|
|
||||||
const username = config.uniqueUsername ? `${config.user.username}-${new Date().getTime().toString().slice(-5)}` : config.user.username;
|
|
||||||
|
|
||||||
const res = await httpSession.post(`${config.api}/session`, {
|
|
||||||
...config.user,
|
|
||||||
username,
|
|
||||||
}, {
|
|
||||||
encodeJSON: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (res.statusCode !== 200) {
|
|
||||||
throw new Error(`Failed to authenticate: ${res.body.toString()}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.info(`Authenticated as '${username}' with ${config.api}`);
|
|
||||||
|
|
||||||
return {
|
|
||||||
user: res.body,
|
|
||||||
httpSession,
|
|
||||||
sessionCookie: res.headers['set-cookie'][0],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getWsId(httpSession) {
|
|
||||||
const res = await httpSession.get(`${config.api}/socket`);
|
|
||||||
|
|
||||||
if (res.statusCode !== 200) {
|
|
||||||
throw new Error(`Failed to retrieve WebSocket ID: ${res.body.toString()}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.body;
|
|
||||||
}
|
|
||||||
|
|
||||||
function onConnect(data, bot) {
|
|
||||||
bot.socket.transmit('joinRooms', { rooms: config.channels });
|
|
||||||
}
|
|
||||||
|
|
||||||
function onRooms({ rooms, users }, bot) {
|
|
||||||
logger.info(`Joined ${rooms.map((room) => room.name).join(', ')}`);
|
|
||||||
|
|
||||||
/* eslint-disable no-param-reassign */
|
|
||||||
bot.rooms = rooms.reduce((acc, room) => ({ ...acc, [room.id]: room }), {});
|
|
||||||
bot.users = { ...bot.users, ...users };
|
|
||||||
/* eslint-enable no-param-reassign */
|
|
||||||
|
|
||||||
rooms.forEach((room) => {
|
|
||||||
bot.socket.transmit('message', {
|
|
||||||
roomId: room.id,
|
|
||||||
body: `Hi, I am ${config.user.username}, your game host!`,
|
|
||||||
style: config.style,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/* eslint-disable no-param-reassign */
|
|
||||||
function onJoin(data, bot) {
|
|
||||||
if (bot.rooms[data.roomId] && !bot.rooms[data.roomId].userIds?.includes(data.user.id)) {
|
|
||||||
bot.users[data.user.id] = data.user;
|
|
||||||
bot.rooms[data.roomId].userIds.push(data.user.id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function onLeave(data, bot) {
|
|
||||||
if (bot.rooms[data.roomId]) {
|
|
||||||
bot.rooms[data.roomId].userIds = bot.rooms[data.roomId].userIds.filter((userId) => userId !== data.userId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const messageHandlers = {
|
|
||||||
connect: onConnect,
|
|
||||||
rooms: onRooms,
|
|
||||||
message: onMessage,
|
|
||||||
join: onJoin,
|
|
||||||
leave: onLeave,
|
|
||||||
};
|
|
||||||
|
|
||||||
function handleError(error, socket, domain, data) {
|
|
||||||
logger.error(`${domain} '${JSON.stringify(data)}' triggered error: ${error.message} ${error.stack}`);
|
|
||||||
|
|
||||||
if (data?.roomId) {
|
|
||||||
socket.transmit('message', {
|
|
||||||
body: ':zap::robot::zap: Many fragments! Some large, some small.',
|
|
||||||
type: 'message',
|
|
||||||
roomId: data.roomId,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function connect(bot, games) {
|
|
||||||
const socket = { ws: { readyState: 0 } };
|
|
||||||
|
|
||||||
socket.connect = async () => {
|
|
||||||
try {
|
|
||||||
const { user, httpSession, sessionCookie } = await auth();
|
|
||||||
const wsCreds = await getWsId(httpSession);
|
|
||||||
|
|
||||||
bot.user = user;
|
|
||||||
bot.httpSession = httpSession;
|
|
||||||
|
|
||||||
logger.info(`Attempting to connect to ${config.socket}`);
|
|
||||||
|
|
||||||
socket.ws = new WebSocket(`${config.socket}?${new URLSearchParams({ v: wsCreds.wsId, t: wsCreds.timestamp }).toString()}`, [], {
|
|
||||||
headers: {
|
|
||||||
cookie: sessionCookie,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.ws.on('message', async (msg) => {
|
|
||||||
const [domain, data] = JSON.parse(msg);
|
|
||||||
|
|
||||||
logger.debug(`Received ${domain}: ${JSON.stringify(data)}`);
|
|
||||||
|
|
||||||
if (messageHandlers[domain]) {
|
|
||||||
try {
|
|
||||||
await messageHandlers[domain](data, bot, games);
|
|
||||||
} catch (error) {
|
|
||||||
handleError(error, socket, domain, data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.ws.on('close', async (info) => {
|
|
||||||
logger.error(`WebSocket closed, reconnecting in ${config.reconnectDelay} seconds: ${info}`);
|
|
||||||
|
|
||||||
await delay(config.reconnectDelay * 1000);
|
|
||||||
socket.connect();
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.ws.on('error', async (error) => {
|
|
||||||
logger.error(`WebSocket error: ${error.message}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
logger.info(`Connected to ${config.socket}`);
|
|
||||||
} catch (error) {
|
|
||||||
logger.error(`Failed to connect, retrying in ${config.reconnectDelay} seconds: ${error.message}`);
|
|
||||||
|
|
||||||
await delay(config.reconnectDelay * 1000);
|
|
||||||
socket.connect();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
socket.transmit = (domain, data) => {
|
|
||||||
socket.ws.send(JSON.stringify([domain, data]));
|
|
||||||
};
|
|
||||||
|
|
||||||
socket.connect();
|
|
||||||
|
|
||||||
return socket;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function init() {
|
|
||||||
const bot = {
|
|
||||||
rooms: [],
|
|
||||||
users: [],
|
|
||||||
};
|
|
||||||
|
|
||||||
const games = await getGames(bot, instance);
|
|
||||||
|
|
||||||
bot.socket = await connect(bot, games);
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = init;
|
|
|
@ -1,39 +0,0 @@
|
||||||
'use strict';
|
|
||||||
|
|
||||||
const config = require('config');
|
|
||||||
const styles = require('irc-colors');
|
|
||||||
|
|
||||||
function schatBold(text) {
|
|
||||||
return `**${text}**`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function schatItalic(text) {
|
|
||||||
return `*${text}*`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function bypass(text) {
|
|
||||||
return text;
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = (() => {
|
|
||||||
if (config.platform === 'irc') {
|
|
||||||
return styles;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (config.platform === 'schat') {
|
|
||||||
const methods = {
|
|
||||||
bold: schatBold,
|
|
||||||
italic: schatItalic,
|
|
||||||
};
|
|
||||||
|
|
||||||
const handler = {
|
|
||||||
get(target, prop) {
|
|
||||||
return target[prop] || bypass;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
return new Proxy(methods, handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
})();
|
|
Loading…
Reference in New Issue