FemScoreboard/discord/bot.ts

373 lines
13 KiB
TypeScript
Raw Permalink Normal View History

2023-10-07 22:46:02 -07:00
/**
* bot.ts
* Scans the chat for reactions and updates the leaderboard database.
*/
2023-10-08 18:59:04 -07:00
import {
2024-03-31 14:36:09 -07:00
Attachment,
AttachmentBuilder,
2023-10-08 18:59:04 -07:00
Client,
2024-02-06 16:45:55 -08:00
Collection,
2023-10-08 18:59:04 -07:00
Events,
GatewayIntentBits,
Interaction,
2024-03-31 14:36:09 -07:00
Message,
MessageFlags,
2023-10-08 18:59:04 -07:00
MessageReaction,
2024-03-31 14:36:09 -07:00
MessageType,
2023-10-08 18:59:04 -07:00
PartialMessageReaction,
Partials, SlashCommandBuilder,
2023-10-08 18:59:04 -07:00
TextChannel,
User
} from 'discord.js';
import fs = require('node:fs');
import path = require('node:path');
2024-02-06 15:23:26 -08:00
import fetch from 'node-fetch';
2024-03-31 14:36:09 -07:00
import FormData = require('form-data');
import tmp = require('tmp');
2024-02-06 15:23:26 -08:00
import { JSDOM } from 'jsdom';
2024-03-31 14:36:09 -07:00
import { logError, logInfo, logWarn } from '../logging';
2023-10-08 19:10:47 -07:00
import {
db,
openDb,
reactionEmojis,
recordReaction,
2024-05-09 16:20:14 -07:00
requestTTSResponse,
2023-10-08 19:10:47 -07:00
sync
} from './util';
2024-03-31 14:36:09 -07:00
import 'dotenv/config';
2023-10-08 19:10:47 -07:00
const KNOWN_USERNAMES = ['vinso1445', 'bapazheng', 'f0oby', 'shibe.mp4', '1thinker', 'bapabakshi', 'keliande27', 'gnuwu', 'scoliono', 'adam28405'];
2024-03-31 14:36:09 -07:00
const config = {};
2023-10-07 22:46:02 -07:00
interface CommandClient extends Client {
commands?: Collection<string, { data: SlashCommandBuilder, execute: (interaction: Interaction) => Promise<void> }>
}
const client: CommandClient = new Client({
2024-03-31 14:36:09 -07:00
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.MessageContent],
2023-10-07 22:46:02 -07:00
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
});
client.commands = new Collection();
2024-02-06 16:45:55 -08:00
2023-10-07 22:46:02 -07:00
client.once(Events.ClientReady, async () => {
2023-10-08 19:10:47 -07:00
logInfo('[bot] Ready.');
2023-10-07 22:46:02 -07:00
for (let i = 0; i < reactionEmojis.length; ++i)
2023-10-08 19:10:47 -07:00
logInfo(`[bot] config: reaction_${i + 1} = ${reactionEmojis[i]}`);
2023-10-07 22:46:02 -07:00
});
2023-10-08 19:10:47 -07:00
2023-10-07 22:46:02 -07:00
async function onMessageReactionChanged(reaction: MessageReaction | PartialMessageReaction, user: User)
{
// When a reaction is received, check if the structure is partial
if (reaction.partial) {
// If the message this reaction belongs to was removed, the fetching might result in an API error which should be handled
try {
await reaction.fetch();
} catch (error) {
2023-10-08 19:10:47 -07:00
logError('[bot] Something went wrong when fetching the reaction:', error);
2023-10-07 22:46:02 -07:00
// Return as `reaction.message.author` may be undefined/null
return;
}
}
if (reaction.message.partial) {
// If the message this reaction belongs to was removed, the fetching might result in an API error which should be handled
try {
await reaction.message.fetch();
} catch (error) {
2023-10-08 19:10:47 -07:00
logError('[bot] Something went wrong when fetching the message:', error);
2023-10-07 22:46:02 -07:00
// Return as `reaction.message.author` may be undefined/null
return;
}
}
// Now the message has been cached and is fully available
2023-10-08 19:10:47 -07:00
logInfo(`[bot] ${reaction.message.author.id}'s message reaction count changed: ${reaction.emoji.name}x${reaction.count}`);
2023-10-07 22:46:02 -07:00
await recordReaction(<MessageReaction> reaction);
}
2024-03-31 14:36:09 -07:00
function textOnlyMessages(message: Message)
{
return message.cleanContent.length > 0 &&
(message.type === MessageType.Default || message.type === MessageType.Reply);
}
function isGoodResponse(response: string)
{
return response.length > 0 && !(response in [
'@Today Man-San(1990)🍁🍂',
'@1981 Celical Man🍁🍂',
'@Exiled Sammy 🔒🏝⏱'
]);
}
async function onNewMessage(message: Message)
{
if (message.author.bot) {
return;
}
/** First, handle audio messages */
if (message.flags.has(MessageFlags.IsVoiceMessage)) {
try {
const audio = await requestRVCResponse(message.attachments.first());
const audioBuf = await audio.arrayBuffer();
const audioFile = new AttachmentBuilder(Buffer.from(audioBuf)).setName('mikuified.wav');
await message.reply({
files: [audioFile]
});
} catch (err) {
logError(`[bot] Failed to generate audio message reply: ${err}`);
}
}
/** Text messages */
if (!textOnlyMessages(message)) {
return;
}
// Miku must reply when spoken to
const mustReply = message.mentions.has(process.env.CLIENT) || message.cleanContent.toLowerCase().includes('miku');
const history = await message.channel.messages.fetch({
2024-05-10 19:55:23 -07:00
limit: config["llmconf"].llmSettings.msg_context-1,
2024-03-31 14:36:09 -07:00
before: message.id
});
// change Miku's message probability depending on current message frequency
const historyMessages = [...history.values()].reverse();
//const historyTimes = historyMessages.map((m: Message) => m.createdAt.getTime());
//const historyAvgDelayMins = (historyTimes[historyTimes.length - 1] - historyTimes[0]) / 60000;
const replyChance = Math.floor(Math.random() * 1/Number(process.env.REPLY_CHANCE)) === 0;
const willReply = mustReply || replyChance;
if (!willReply) {
return;
}
const cleanHistory = historyMessages.filter(textOnlyMessages);
const cleanHistoryList = [
...cleanHistory,
message
];
try {
await message.channel.sendTyping();
2024-03-31 14:36:09 -07:00
const response = await requestLLMResponse(cleanHistoryList);
// evaluate response
if (!isGoodResponse(response)) {
logWarn(`[bot] Burning bad response: "${response}"`);
return;
}
await message.reply(response);
} catch (err) {
logError(`[bot] Error while generating LLM response: ${err}`);
}
}
2023-10-08 18:59:04 -07:00
async function fetchMotd()
{
2024-05-09 16:20:14 -07:00
try {
const res = await fetch(process.env.MOTD_HREF);
const xml = await res.text();
const parser = new JSDOM(xml);
const doc = parser.window.document;
const el = doc.querySelector(process.env.MOTD_QUERY);
return el ? el.textContent : null;
} catch (err) {
logWarn('[bot] Failed to fetch MOTD; is the booru down?');
}
2023-10-08 18:59:04 -07:00
}
2024-03-31 14:36:09 -07:00
async function requestRVCResponse(src: Attachment): Promise<Blob>
{
logInfo(`[bot] Downloading audio message ${src.url}`);
const srcres = await fetch(src.url);
const srcbuf = await srcres.arrayBuffer();
const tmpFile = tmp.fileSync();
const tmpFileName = tmpFile.name;
fs.writeFileSync(tmpFileName, Buffer.from(srcbuf));
logInfo(`[bot] Got audio file: ${srcbuf.size} bytes`);
const queryParams = new URLSearchParams();
queryParams.append("token", process.env.LLM_TOKEN);
const fd = new FormData();
fd.append('file', fs.readFileSync(tmpFileName), 'voice-message.ogg');
2024-05-09 16:20:14 -07:00
const rvcEndpoint = `${process.env.LLM_HOST}/rvc?${queryParams.toString()}`;
2024-03-31 14:36:09 -07:00
logInfo(`[bot] Requesting RVC response for ${src.id}`);
const res = await fetch(rvcEndpoint, {
method: 'POST',
body: fd
});
const resContents = await res.blob();
return resContents;
}
async function requestLLMResponse(messages)
{
const queryParams = new URLSearchParams();
queryParams.append("token", process.env.LLM_TOKEN);
for (const field of Object.keys(config["llmconf"].llmSettings)) {
queryParams.append(field, config["llmconf"].llmSettings[field]);
}
2024-05-09 16:20:14 -07:00
const llmEndpoint = `${process.env.LLM_HOST}/?${queryParams.toString()}`;
let messageList = await Promise.all(
messages.map(async (m: Message) => {
let role = 'user';
if (m.author.id === process.env.CLIENT) {
role = 'assistant';
} else if (m.author.bot) {
return null;
/* } else if (KNOWN_USERNAMES.includes(m.author.username)) {
role = m.author.username; */
}
// fetch replied-to message, if there is one, and prompt it as such
let cleanContent = m.cleanContent;
if (m.type == MessageType.Reply && m.reference) {
// what about deeply nested replies? could possibly be recursive?
const repliedToMsg = await m.fetchReference();
if (repliedToMsg) {
const repliedToMsgLines = repliedToMsg.cleanContent.split('\n');
cleanContent = `> ${repliedToMsgLines.join('\n> ')}\n${cleanContent}`;
}
}
return { role, content: cleanContent };
})
);
messageList = messageList.filter(x => !!x);
// at the beginning, inject the system prompt
// at the end, start our text generation as a reply to the most recent msg from history
const replyContext = `> ${messageList[messageList.length - 1].content.split('\n').join('\n> ')}\n`;
2024-03-31 14:36:09 -07:00
const reqBody = [
{
"role": "system",
"content": config["llmconf"].sys_prompt
},
...messageList,
{
"role": "assistant",
"content": replyContext
}
2024-03-31 14:36:09 -07:00
];
logInfo("[bot] Requesting LLM response with message list: " + reqBody.map(m => m.content));
const res = await fetch(llmEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(reqBody)
});
const txt = await res.json();
const txtRaw: string = txt["raw"][0];
2024-05-28 11:34:09 -07:00
// Depends on chat template used
const prefix = "<|start_header_id|>assistant<|end_header_id|>\n\n";
const suffix = "<|eot_id|>";
2024-03-31 14:36:09 -07:00
const txtStart = txtRaw.lastIndexOf(prefix);
2024-05-28 11:34:09 -07:00
const txtEnd = txtRaw.slice(txtStart + prefix.length);
const txtStop = txtEnd.indexOf(suffix) !== -1 ? txtEnd.indexOf(suffix) : txtEnd.length;
return txtEnd.slice(0, txtStop);
2024-03-31 14:36:09 -07:00
}
2023-10-08 18:59:04 -07:00
async function scheduleRandomMessage(firstTime = false)
{
if (!firstTime) {
const channel = <TextChannel> await client.channels.fetch(process.env.MOTD_CHANNEL);
if (!channel) {
logWarn(`[bot] Channel ${process.env.MOTD_CHANNEL} not found, disabling MOTD.`);
return;
}
2023-10-08 18:59:04 -07:00
const randomMessage = await fetchMotd();
if (randomMessage) {
try {
const audio = await requestTTSResponse(randomMessage);
const audioBuf = await audio.arrayBuffer();
const audioFile = new AttachmentBuilder(Buffer.from(audioBuf)).setName('mikuified.wav');
await channel.send({
content: randomMessage,
files: [audioFile]
});
logInfo(`[bot] Sent MOTD + TTS: ${randomMessage}`);
} catch (err) {
await channel.send(randomMessage);
logWarn(`[bot] Could not fetch MOTD TTS: ${err}`);
logInfo(`[bot] Send text MOTD: ${randomMessage}`);
}
} else {
logWarn(`[bot] Could not fetch MOTD.`);
}
2023-10-08 18:59:04 -07:00
}
// wait between 2-8 hours
const timeoutMins = Math.random() * 360 + 120;
const scheduledTime = new Date();
scheduledTime.setMinutes(scheduledTime.getMinutes() + timeoutMins);
2023-10-08 19:15:00 -07:00
logInfo(`[bot] Next MOTD: ${scheduledTime.toLocaleTimeString()}`);
2023-10-08 18:59:04 -07:00
setTimeout(scheduleRandomMessage, timeoutMins * 60 * 1000);
}
2024-02-06 16:45:55 -08:00
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
});
2024-03-31 14:36:09 -07:00
client.on(Events.MessageCreate, onNewMessage);
2023-10-07 22:46:02 -07:00
client.on(Events.MessageReactionAdd, onMessageReactionChanged);
client.on(Events.MessageReactionRemove, onMessageReactionChanged);
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;
2023-10-07 22:46:02 -07:00
const client: CommandClient = interaction.client;
const command = client.commands.get(interaction.commandName);
if (!command) {
logError(`[bot] No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command.execute(interaction);
} catch (error) {
logError(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
} else {
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
}
});
// startup
(async () => {
2024-03-31 14:36:09 -07:00
tmp.setGracefulCleanup();
2023-10-08 19:10:47 -07:00
logInfo("[db] Opening...");
2023-10-07 22:46:02 -07:00
await openDb();
2023-10-08 19:10:47 -07:00
logInfo("[db] Migrating...");
2023-10-07 22:46:02 -07:00
await db.migrate();
2023-10-08 19:10:47 -07:00
logInfo("[db] Ready.");
logInfo("[bot] Loading commands...");
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
client.commands.set(command.data.name, command);
2024-03-31 14:36:09 -07:00
config[command.data.name] = command.config;
logInfo(`[bot] Found command: /${command.data.name}`);
}
}
2023-10-08 19:10:47 -07:00
logInfo("[bot] Logging in...");
2023-10-07 22:46:02 -07:00
await client.login(process.env.TOKEN);
await sync(client.guilds);
2023-10-08 18:59:04 -07:00
if (process.env.ENABLE_MOTD) {
await scheduleRandomMessage(true);
}
})();