Added chat token reset. WIP numbers game.

This commit is contained in:
Niels Simenon 2023-04-10 01:23:19 +02:00
parent 7321037f4f
commit 74342ab07b
3 changed files with 110 additions and 9 deletions

View File

@ -76,10 +76,10 @@ module.exports = {
userTokenLimit: 20000, // daily, roughly 100+ messages or $0.04 per user userTokenLimit: 20000, // daily, roughly 100+ messages or $0.04 per user
userTokenPeriod: 24, // hours userTokenPeriod: 24, // hours
replyTokenLimit: 1000, replyTokenLimit: 1000,
replyWordLimit: 100, replyWordLimit: 70,
replyWordLimitPublic: true, replyWordLimitPublic: true,
temperature: 1, temperature: 1,
history: 2, history: 3,
rule: 'a tired game host', rule: 'a tired game host',
rulePublic: true, rulePublic: true,
}, },

View File

@ -55,7 +55,7 @@ function setHistory(value, context) {
return; return;
} }
context.logger.info(`Chat history must be a valid number between 0 and 10, ${context.user.prefixedUsername}`); context.sendMessage(`Chat history must be a valid number between 0 and 10, ${context.user.prefixedUsername}`, context.room.id, { label: false });
} }
function setReplyWordLimit(value, context) { function setReplyWordLimit(value, context) {
@ -66,7 +66,7 @@ function setReplyWordLimit(value, context) {
const newReplyWordLimit = Number(value); const newReplyWordLimit = Number(value);
if (!Number.isNaN(newReplyWordLimit) && newReplyWordLimit > 3 && newReplyWordLimit <= 200) { if (!Number.isNaN(newReplyWordLimit) && newReplyWordLimit >= 3 && newReplyWordLimit <= 200) {
settings.replyWordLimit = newReplyWordLimit; settings.replyWordLimit = newReplyWordLimit;
context.logger.info(`Chat reply word limit set to ${newReplyWordLimit} by ${context.user.username}`); context.logger.info(`Chat reply word limit set to ${newReplyWordLimit} by ${context.user.username}`);
@ -75,7 +75,7 @@ function setReplyWordLimit(value, context) {
return; return;
} }
context.logger.info(`Chat reply word limit must be a valid number between 3 and 200, ${context.user.prefixedUsername}`); context.sendMessage(`Chat reply word limit must be a valid number between 3 and 200, ${context.user.prefixedUsername}`, context.room.id, { label: false });
} }
function setTemperature(value, context) { function setTemperature(value, context) {
@ -95,7 +95,7 @@ function setTemperature(value, context) {
return; return;
} }
context.logger.info(`Chat temperature must be a valid number between 0 and 2, ${context.user.prefixedUsername}`); context.sendMessage(`Chat temperature must be a valid number between 0 and 2, ${context.user.prefixedUsername}`, context.room.id, { label: false });
} }
function setModel(model, context) { function setModel(model, context) {
@ -113,7 +113,7 @@ function setModel(model, context) {
return; return;
} }
context.logger.info(`Model '${model}' is not supported right now, ${context.user.prefixedUsername}`); context.sendMessage(`Model '${model}' is not supported right now, ${context.user.prefixedUsername}`, context.room.id, { label: false });
} }
function setRule(rule, context) { function setRule(rule, context) {
@ -153,6 +153,16 @@ async function getTokens(username) {
return usedTokens || 0; return usedTokens || 0;
} }
async function resetTokens(username) {
if (!username) {
return;
}
await knex('chat_tokens')
.where('user_id', username)
.delete();
}
async function onCommand(args, context) { async function onCommand(args, context) {
if (context.subcommand === 'history' && config.operators.includes(context.user.username)) { if (context.subcommand === 'history' && config.operators.includes(context.user.username)) {
setHistory(args[0], context); setHistory(args[0], context);
@ -169,7 +179,7 @@ async function onCommand(args, context) {
return; return;
} }
if (['rule', 'is', 'be', 'ur', 'reset'].includes(context.subcommand) && (config.chat.rulePublic || config.operators.includes(context.user.username))) { if (['rule', 'is', 'be', 'ur'].includes(context.subcommand) && (config.chat.rulePublic || config.operators.includes(context.user.username))) {
setRule(context.subcommand === 'reset' ? 'reset' : args.join(' '), context); setRule(context.subcommand === 'reset' ? 'reset' : args.join(' '), context);
return; return;
} }
@ -188,6 +198,16 @@ async function onCommand(args, context) {
return; return;
} }
if (context.subcommand === 'reset' && config.operators.includes(context.user.username)) {
const username = args[0] ? args[0].replace(new RegExp(`^${config.usernamePrefix}`), '') : context.user.username;
await resetTokens(username);
context.sendMessage(args[0] ? `Chat tokens reset for ${style.bold(username)}` : 'Chat tokens reset', context.room.id, { label: false });
return;
}
const prompt = args.join(' '); const prompt = args.join(' ');
try { try {
@ -202,7 +222,9 @@ async function onCommand(args, context) {
const message = { const message = {
role: 'user', role: 'user',
content: `Using ${settings.replyWordLimit} words or fewer, answer as if you're ${settings.rule}. ${prompt}`, content: settings.replyWordLimit
? `Using ${settings.replyWordLimit} words or fewer, answer as if you're ${settings.rule}. ${prompt}`
: `Answer as if you're ${settings.rule}. ${prompt}`,
}; };
const userHistory = (history.get(context.user.username) || []).concat(message); const userHistory = (history.get(context.user.username) || []).concat(message);

79
src/games/numbers.js Normal file
View File

@ -0,0 +1,79 @@
'use strict';
const config = require('config');
const { evaluate } = require('mathjs');
const games = new Map();
function getBoard(context) {
const game = games.get(context.room.id);
return game;
}
function pickNumbers(type, context) {
const game = games.get(context.room.id);
if (!game) {
return;
}
console.log('pick', type);
}
function playSolution(solution, context) {
}
function start(context) {
if (games.has(context.room.id)) {
context.sendMessage(`${getBoard(context)} This is the current board. Use ${config.prefix}numbers:stop to reset.`);
return;
}
games.set(context.room.id, {
state: 'pick',
});
context.sendMessage('Let\'s play the numbers! Would you like a big number or a small one?', context.room.id);
}
function onCommand(args, context) {
if (!args.subcommand) {
start(context);
}
}
function onMessage(message, context) {
console.log('message', message.body);
const game = games.get(context.room.id);
if (game?.state === 'pick') {
const multi = message.body.match(/\b[bs]{2,}\b/i)?.[0];
if (multi) {
pickNumbers(multi.toLowerCase(), context);
return;
}
if (/big/i.test(message.body)) {
pickNumbers('big', context);
return;
}
if (/small/i.test(message.body)) {
pickNumbers('small', context);
return;
}
}
if (game?.state === 'solutions') {
playSolution(message.body);
}
}
module.exports = {
onCommand,
onMessage,
};