This commit is contained in:
Dragon Fire
2024-05-01 23:35:34 -04:00
parent 447557622c
commit 78aed1eca0
14 changed files with 75 additions and 75 deletions
+30 -30
View File
@@ -73,36 +73,6 @@ client.on('ready', async () => {
const decTalkFolderExists = await checkFileExists(path.join(__dirname, 'tmp', 'dec-talk')); const decTalkFolderExists = await checkFileExists(path.join(__dirname, 'tmp', 'dec-talk'));
if (!decTalkFolderExists) await mkdir(path.join(__dirname, 'tmp', 'dec-talk')); if (!decTalkFolderExists) await mkdir(path.join(__dirname, 'tmp', 'dec-talk'));
// Interval to change activity every minute
setInterval(() => {
const activity = client.activities[Math.floor(Math.random() * client.activities.length)];
const text = typeof activity.text === 'function' ? activity.text(client) : activity.text;
client.user.setActivity(text, { type: activity.type });
}, 60000);
// Import command-leaderboard.json
try {
const results = client.importCommandLeaderboard();
if (results) {
client.logger.info('[LEADERBOARD] command-leaderboard.json successfully loaded.');
} else {
client.logger.error('[LEADERBOARD] command-leaderboard.json is not formatted correctly.');
}
} catch (err) {
client.logger.error(`[LEADERBOARD] Could not parse command-leaderboard.json:\n${err.stack}`);
}
// Set up disabled commands
try {
const disabled = await client.redis.hgetall('disabled');
for (const command of Object.keys(disabled)) {
client.registry.commands.get(command).disable();
client.logger.info(`[DISABLED] Disabled the ${command} command.`);
}
} catch (err) {
client.logger.error(`[DISABLED] Error while disabling commands:\n${err.stack}`);
}
// Check for API keys and disable commands that need them if not present // Check for API keys and disable commands that need them if not present
if (!process.env.REDIS_HOST || !process.env.REDIS_PASS) { if (!process.env.REDIS_HOST || !process.env.REDIS_PASS) {
client.logger.error('[REDIS] No REDIS_HOST or REDIS_PASS in env. Exiting process.'); client.logger.error('[REDIS] No REDIS_HOST or REDIS_PASS in env. Exiting process.');
@@ -145,6 +115,36 @@ client.on('ready', async () => {
client.logger.info('[DISABLED] No WEBSTER_KEY in env. word-of-the-day, word-chain, and define have been disabled.'); client.logger.info('[DISABLED] No WEBSTER_KEY in env. word-of-the-day, word-chain, and define have been disabled.');
} }
// Interval to change activity every minute
setInterval(() => {
const activity = client.activities[Math.floor(Math.random() * client.activities.length)];
const text = typeof activity.text === 'function' ? activity.text(client) : activity.text;
client.user.setActivity(text, { type: activity.type });
}, 60000);
// Set up disabled commands
try {
const disabled = await client.redis.db.hgetall('disabled');
for (const command of Object.keys(disabled)) {
client.registry.commands.get(command).disable();
client.logger.info(`[DISABLED] Disabled the ${command} command.`);
}
} catch (err) {
client.logger.error(`[DISABLED] Error while disabling commands:\n${err.stack}`);
}
// Import command-leaderboard.json
try {
const results = client.importCommandLeaderboard();
if (results) {
client.logger.info('[LEADERBOARD] command-leaderboard.json successfully loaded.');
} else {
client.logger.error('[LEADERBOARD] command-leaderboard.json is not formatted correctly.');
}
} catch (err) {
client.logger.error(`[LEADERBOARD] Could not parse command-leaderboard.json:\n${err.stack}`);
}
// Import command-last-run.json // Import command-last-run.json
try { try {
const results = client.importLastRun(); const results = client.importLastRun();
+2 -2
View File
@@ -12,9 +12,9 @@ module.exports = class ChessDeleteCommand extends Command {
} }
async run(msg) { async run(msg) {
const data = await this.client.redis.exists(`chess-${msg.author.id}`); const data = await this.client.redis.db.exists(`chess-${msg.author.id}`);
if (!data) return msg.reply('You do not have a saved Chess game.'); if (!data) return msg.reply('You do not have a saved Chess game.');
await this.client.redis.del(`chess-${msg.author.id}`); await this.client.redis.db.del(`chess-${msg.author.id}`);
return msg.say('Your saved game has been deleted.'); return msg.say('Your saved game has been deleted.');
} }
}; };
+5 -5
View File
@@ -65,7 +65,7 @@ module.exports = class ChessCommand extends Command {
const verification = await verify(msg.channel, opponent); const verification = await verify(msg.channel, opponent);
if (!verification) return msg.say('Looks like they declined...'); if (!verification) return msg.say('Looks like they declined...');
} }
const resumeGame = await this.client.redis.get(`chess-${msg.author.id}`); const resumeGame = await this.client.redis.db.get(`chess-${msg.author.id}`);
let game; let game;
let whiteTime = time === 0 ? Infinity : time * 60000; let whiteTime = time === 0 ? Infinity : time * 60000;
let blackTime = time === 0 ? Infinity : time * 60000; let blackTime = time === 0 ? Infinity : time * 60000;
@@ -85,9 +85,9 @@ module.exports = class ChessCommand extends Command {
blackTime = data.blackTime === -1 ? Infinity : data.blackTime; blackTime = data.blackTime === -1 ? Infinity : data.blackTime;
whitePlayer = data.color === 'white' ? msg.author : opponent; whitePlayer = data.color === 'white' ? msg.author : opponent;
blackPlayer = data.color === 'black' ? msg.author : opponent; blackPlayer = data.color === 'black' ? msg.author : opponent;
await this.client.redis.del(`chess-${msg.author.id}`); await this.client.redis.db.del(`chess-${msg.author.id}`);
} catch { } catch {
await this.client.redis.del(`chess-${msg.author.id}`); await this.client.redis.db.del(`chess-${msg.author.id}`);
return msg.reply('An error occurred reading your saved game. It will be deleted.'); return msg.reply('An error occurred reading your saved game. It will be deleted.');
} }
} else { } else {
@@ -154,7 +154,7 @@ module.exports = class ChessCommand extends Command {
if (turn.first().content.toLowerCase() === 'end') break; if (turn.first().content.toLowerCase() === 'end') break;
if (turn.first().content.toLowerCase() === 'save') { if (turn.first().content.toLowerCase() === 'save') {
const { author } = turn.first(); const { author } = turn.first();
const alreadySaved = await this.client.redis.get(`chess-${author.id}`); const alreadySaved = await this.client.redis.db.get(`chess-${author.id}`);
if (alreadySaved) { if (alreadySaved) {
await msg.say('You already have a saved game, do you want to overwrite it?'); await msg.say('You already have a saved game, do you want to overwrite it?');
const verification = await verify(msg.channel, author); const verification = await verify(msg.channel, author);
@@ -162,7 +162,7 @@ module.exports = class ChessCommand extends Command {
} }
if (gameState.turn === 'black') blackTime -= new Date() - now; if (gameState.turn === 'black') blackTime -= new Date() - now;
if (gameState.turn === 'white') whiteTime -= new Date() - now; if (gameState.turn === 'white') whiteTime -= new Date() - now;
await this.client.redis.set( await this.client.redis.db.set(
`chess-${author.id}`, `chess-${author.id}`,
this.exportGame( this.exportGame(
game, game,
+4 -4
View File
@@ -41,14 +41,14 @@ module.exports = class GunfightCommand extends Command {
time: 30000 time: 30000
}); });
const newScore = Date.now() - now; const newScore = Date.now() - now;
const highScoreGet = await this.client.redis.get('reaction-time'); const highScoreGet = await this.client.redis.db.get('reaction-time');
const highScore = highScoreGet ? Number.parseInt(highScoreGet, 10) : null; const highScore = highScoreGet ? Number.parseInt(highScoreGet, 10) : null;
const highScoreUser = await this.client.redis.get('reaction-time-user'); const highScoreUser = await this.client.redis.db.get('reaction-time-user');
const scoreBeat = !highScore || highScore > newScore; const scoreBeat = !highScore || highScore > newScore;
const user = await fetchHSUserDisplay(this.client, highScoreUser); const user = await fetchHSUserDisplay(this.client, highScoreUser);
if (scoreBeat) { if (scoreBeat) {
await this.client.redis.set('reaction-time', newScore); await this.client.redis.db.set('reaction-time', newScore);
await this.client.redis.set('reaction-time-user', winner.first().author.id); await this.client.redis.db.set('reaction-time-user', winner.first().author.id);
} }
if (!winner.size) return msg.say('Oh... No one won.'); if (!winner.size) return msg.say('Oh... No one won.');
return msg.say(stripIndents` return msg.say(stripIndents`
+4 -4
View File
@@ -43,14 +43,14 @@ module.exports = class TypingRaceCommand extends Command {
time: 30000 time: 30000
}); });
const newScore = Date.now() - now; const newScore = Date.now() - now;
const highScoreGet = await this.client.redis.get('typing-test'); const highScoreGet = await this.client.redis.db.get('typing-test');
const highScore = highScoreGet ? Number.parseInt(highScoreGet, 10) : null; const highScore = highScoreGet ? Number.parseInt(highScoreGet, 10) : null;
const highScoreUser = await this.client.redis.get('typing-test-user'); const highScoreUser = await this.client.redis.db.get('typing-test-user');
const scoreBeat = winner.size && (!highScore || highScore > newScore); const scoreBeat = winner.size && (!highScore || highScore > newScore);
const user = await fetchHSUserDisplay(this.client, highScoreUser); const user = await fetchHSUserDisplay(this.client, highScoreUser);
if (scoreBeat) { if (scoreBeat) {
await this.client.redis.set('typing-test', newScore); await this.client.redis.db.set('typing-test', newScore);
await this.client.redis.set('typing-test-user', winner.first().author.id); await this.client.redis.db.set('typing-test-user', winner.first().author.id);
} }
if (!winner.size) return msg.say('Oh... No one won.'); if (!winner.size) return msg.say('Oh... No one won.');
const wpm = (sentence.length / 5) / ((newScore / 1000) / 60); const wpm = (sentence.length / 5) / ((newScore / 1000) / 60);
+4 -4
View File
@@ -70,14 +70,14 @@ module.exports = class AnagramicaCommand extends Command {
filter, filter,
time: time * 1000 time: time * 1000
}); });
const highScoreGet = await this.client.redis.get('anagramica'); const highScoreGet = await this.client.redis.db.get('anagramica');
const highScore = highScoreGet ? Number.parseInt(highScoreGet, 10) : null; const highScore = highScoreGet ? Number.parseInt(highScoreGet, 10) : null;
const highScoreUser = await this.client.redis.get('anagramica-user'); const highScoreUser = await this.client.redis.db.get('anagramica-user');
const scoreBeat = !highScore || highScore < points; const scoreBeat = !highScore || highScore < points;
const user = await fetchHSUserDisplay(this.client, highScoreUser); const user = await fetchHSUserDisplay(this.client, highScoreUser);
if (scoreBeat) { if (scoreBeat) {
await this.client.redis.set('anagramica', points); await this.client.redis.db.set('anagramica', points);
await this.client.redis.set('anagramica-user', msg.author.id); await this.client.redis.db.set('anagramica-user', msg.author.id);
} }
const moreWords = shuffle(valid.filter(word => !picked.includes(word))).slice(0, 5); const moreWords = shuffle(valid.filter(word => !picked.includes(word))).slice(0, 5);
if (!msgs.size) { if (!msgs.size) {
+4 -4
View File
@@ -127,14 +127,14 @@ module.exports = class MinesweeperCommand extends Command {
} }
} }
const newScore = Date.now() - startTime; const newScore = Date.now() - startTime;
const highScoreGet = await this.client.redis.get(`minesweeper-${size}`); const highScoreGet = await this.client.redis.db.get(`minesweeper-${size}`);
const highScore = highScoreGet ? Number.parseInt(highScoreGet, 10) : null; const highScore = highScoreGet ? Number.parseInt(highScoreGet, 10) : null;
const highScoreUser = await this.client.redis.get(`minesweeper-${size}-user`); const highScoreUser = await this.client.redis.db.get(`minesweeper-${size}-user`);
const scoreBeat = !cheatMode && win && (!highScore || highScore > newScore); const scoreBeat = !cheatMode && win && (!highScore || highScore > newScore);
const user = await fetchHSUserDisplay(this.client, highScoreUser); const user = await fetchHSUserDisplay(this.client, highScoreUser);
if (scoreBeat) { if (scoreBeat) {
await this.client.redis.set(`minesweeper-${size}`, newScore); await this.client.redis.db.set(`minesweeper-${size}`, newScore);
await this.client.redis.set(`minesweeper-${size}-user`, msg.author.id); await this.client.redis.db.set(`minesweeper-${size}-user`, msg.author.id);
} }
if (win === null) return gameMsg.edit('Game ended due to inactivity.'); if (win === null) return gameMsg.edit('Game ended due to inactivity.');
const newDisplayTime = moment.duration(newScore).format('mm:ss'); const newDisplayTime = moment.duration(newScore).format('mm:ss');
+4 -4
View File
@@ -28,14 +28,14 @@ module.exports = class ReactionTimeCommand extends Command {
time: 30000 time: 30000
}); });
const newScore = Date.now() - now; const newScore = Date.now() - now;
const highScoreGet = await this.client.redis.get('reaction-time'); const highScoreGet = await this.client.redis.db.get('reaction-time');
const highScore = highScoreGet ? Number.parseInt(highScoreGet, 10) : null; const highScore = highScoreGet ? Number.parseInt(highScoreGet, 10) : null;
const highScoreUser = await this.client.redis.get('reaction-time-user'); const highScoreUser = await this.client.redis.db.get('reaction-time-user');
const scoreBeat = !highScore || highScore > newScore; const scoreBeat = !highScore || highScore > newScore;
const user = await fetchHSUserDisplay(this.client, highScoreUser); const user = await fetchHSUserDisplay(this.client, highScoreUser);
if (scoreBeat) { if (scoreBeat) {
await this.client.redis.set('reaction-time', newScore); await this.client.redis.db.set('reaction-time', newScore);
await this.client.redis.set('reaction-time-user', msg.author.id); await this.client.redis.db.set('reaction-time-user', msg.author.id);
} }
if (!msgs.size) return msg.say('Failed to answer within 30 seconds.'); if (!msgs.size) return msg.say('Failed to answer within 30 seconds.');
return msg.say(stripIndents` return msg.say(stripIndents`
+4 -4
View File
@@ -29,14 +29,14 @@ module.exports = class TypingTestCommand extends Command {
}); });
const win = msgs.size && msgs.first().content.toLowerCase() === sentence; const win = msgs.size && msgs.first().content.toLowerCase() === sentence;
const newScore = Date.now() - now; const newScore = Date.now() - now;
const highScoreGet = await this.client.redis.get('typing-test'); const highScoreGet = await this.client.redis.db.get('typing-test');
const highScore = highScoreGet ? Number.parseInt(highScoreGet, 10) : null; const highScore = highScoreGet ? Number.parseInt(highScoreGet, 10) : null;
const highScoreUser = await this.client.redis.get('typing-test-user'); const highScoreUser = await this.client.redis.db.get('typing-test-user');
const scoreBeat = win && (!highScore || highScore > newScore); const scoreBeat = win && (!highScore || highScore > newScore);
const user = await fetchHSUserDisplay(this.client, highScoreUser); const user = await fetchHSUserDisplay(this.client, highScoreUser);
if (scoreBeat) { if (scoreBeat) {
await this.client.redis.set('typing-test', newScore); await this.client.redis.db.set('typing-test', newScore);
await this.client.redis.set('typing-test-user', msg.author.id); await this.client.redis.db.set('typing-test-user', msg.author.id);
} }
if (!msgs.size) return msg.reply('Sorry! You lose!'); if (!msgs.size) return msg.reply('Sorry! You lose!');
if (msgs.first().content.toLowerCase() !== sentence) { if (msgs.first().content.toLowerCase() !== sentence) {
+8 -8
View File
@@ -18,26 +18,26 @@ module.exports = class HighScoresCommand extends Command {
} }
async run(msg) { async run(msg) {
const typingRaceGet = await this.client.redis.get('typing-test'); const typingRaceGet = await this.client.redis.db.get('typing-test');
const typingRace = typingRaceGet ? Number.parseInt(typingRaceGet, 10) : null; const typingRace = typingRaceGet ? Number.parseInt(typingRaceGet, 10) : null;
const typingRaceUser = await this.client.redis.get('typing-test-user'); const typingRaceUser = await this.client.redis.db.get('typing-test-user');
const typingRaceUserDisplay = await fetchHSUserDisplay(this.client, typingRaceUser); const typingRaceUserDisplay = await fetchHSUserDisplay(this.client, typingRaceUser);
const anagramsGet = await this.client.redis.get('anagramica'); const anagramsGet = await this.client.redis.db.get('anagramica');
const anagrams = anagramsGet ? Number.parseInt(anagramsGet, 10) : null; const anagrams = anagramsGet ? Number.parseInt(anagramsGet, 10) : null;
const anagramsUser = await this.client.redis.get('anagramica-user'); const anagramsUser = await this.client.redis.db.get('anagramica-user');
const anagramsUserDisplay = await fetchHSUserDisplay(this.client, anagramsUser); const anagramsUserDisplay = await fetchHSUserDisplay(this.client, anagramsUser);
const minesweeperScores = {}; const minesweeperScores = {};
const minesweeperUsers = {}; const minesweeperUsers = {};
for (const size of minesweeperSizes) { for (const size of minesweeperSizes) {
const minesweeperGet = await this.client.redis.get(`minesweeper-${size}`); const minesweeperGet = await this.client.redis.db.get(`minesweeper-${size}`);
const minesweeper = minesweeperGet ? Number.parseInt(minesweeperGet, 10) : null; const minesweeper = minesweeperGet ? Number.parseInt(minesweeperGet, 10) : null;
const minesweeperUser = await this.client.redis.get(`minesweeper-${size}-user`); const minesweeperUser = await this.client.redis.db.get(`minesweeper-${size}-user`);
minesweeperScores[size] = moment.duration(minesweeper).format('mm:ss'); minesweeperScores[size] = moment.duration(minesweeper).format('mm:ss');
minesweeperUsers[size] = await fetchHSUserDisplay(this.client, minesweeperUser); minesweeperUsers[size] = await fetchHSUserDisplay(this.client, minesweeperUser);
} }
const reactionTimeGet = await this.client.redis.get('reaction-time'); const reactionTimeGet = await this.client.redis.db.get('reaction-time');
const reactionTime = reactionTimeGet ? Number.parseInt(reactionTimeGet, 10) : null; const reactionTime = reactionTimeGet ? Number.parseInt(reactionTimeGet, 10) : null;
const reactionTimeUser = await this.client.redis.get('reaction-time-user'); const reactionTimeUser = await this.client.redis.db.get('reaction-time-user');
const reactionTimeUserDisplay = await fetchHSUserDisplay(this.client, reactionTimeUser); const reactionTimeUserDisplay = await fetchHSUserDisplay(this.client, reactionTimeUser);
const minesweeperDisplay = Object.entries(minesweeperScores) const minesweeperDisplay = Object.entries(minesweeperScores)
.map(([size, score]) => `\`${size}x${size}\`: ${score} (Held by ${minesweeperUsers[size]})`) .map(([size, score]) => `\`${size}x${size}\`: ${score} (Held by ${minesweeperUsers[size]})`)
+1 -1
View File
@@ -24,7 +24,7 @@ module.exports = class DisableCommand extends Command {
if (!command._enabled) return msg.say(`The \`${command.name}\` command is already disabled.`); if (!command._enabled) return msg.say(`The \`${command.name}\` command is already disabled.`);
if (command.guarded) return msg.say(`The \`${command.name}\` command cannot be disabled.`); if (command.guarded) return msg.say(`The \`${command.name}\` command cannot be disabled.`);
command.disable(); command.disable();
await this.client.redis.hset('disabled', { [command.name]: true }); await this.client.redis.db.hset('disabled', { [command.name]: true });
return msg.say(`Disabled the \`${command.name}\` command.`); return msg.say(`Disabled the \`${command.name}\` command.`);
} }
}; };
+1 -1
View File
@@ -23,7 +23,7 @@ module.exports = class EnableCommand extends Command {
async run(msg, { command }) { async run(msg, { command }) {
if (command._enabled) return msg.say(`The \`${command.name}\` command is already enabled.`); if (command._enabled) return msg.say(`The \`${command.name}\` command is already enabled.`);
command.enable(); command.enable();
await this.client.redis.hdel('disabled', command.name); await this.client.redis.db.hdel('disabled', command.name);
return msg.say(`Enabled the \`${command.name}\` command.`); return msg.say(`Enabled the \`${command.name}\` command.`);
} }
}; };
+2 -2
View File
@@ -29,7 +29,7 @@ module.exports = class Timer {
await channel.send(`🕰️ <@${this.userID}>, you wanted me to remind you of: **"${this.title}"**.`); await channel.send(`🕰️ <@${this.userID}>, you wanted me to remind you of: **"${this.title}"**.`);
} finally { } finally {
this.client.timers.delete(this.id); this.client.timers.delete(this.id);
await this.client.redis.hdel('timer', this.id); await this.client.redis.db.hdel('timer', this.id);
} }
}, time); }, time);
} }
@@ -37,6 +37,6 @@ module.exports = class Timer {
delete() { delete() {
clearTimeout(this.timeout); clearTimeout(this.timeout);
this.client.timers.delete(this.id); this.client.timers.delete(this.id);
return this.client.redis.hdel('timer', this.id); return this.client.redis.db.hdel('timer', this.id);
} }
}; };
+2 -2
View File
@@ -9,7 +9,7 @@ module.exports = class TimerManager extends Collection {
} }
async fetchAll() { async fetchAll() {
const timers = await this.client.redis.hgetall('timer'); const timers = await this.client.redis.db.hgetall('timer');
for (let data of Object.values(timers)) { for (let data of Object.values(timers)) {
data = JSON.parse(data); data = JSON.parse(data);
await this.setTimer(data.id, data.channelID, new Date(data.time) - new Date(), data.userID, data.title, false); await this.setTimer(data.id, data.channelID, new Date(data.time) - new Date(), data.userID, data.title, false);
@@ -19,7 +19,7 @@ module.exports = class TimerManager extends Collection {
async setTimer(id, channelID, time, userID, title, updateRedis = true) { async setTimer(id, channelID, time, userID, title, updateRedis = true) {
const timer = new Timer(this.client, id, channelID, userID, time, title); const timer = new Timer(this.client, id, channelID, userID, time, title);
if (updateRedis) await this.client.redis.hset('timer', { [timer.id]: timer.stringify() }); if (updateRedis) await this.client.redis.db.hset('timer', { [timer.id]: timer.stringify() });
this.set(timer.id, timer); this.set(timer.id, timer);
return timer; return timer;
} }