Update bot to new discord API and add simple chatgpt module

This commit is contained in:
2023-03-21 13:35:28 +01:00
parent 136173f4de
commit b6008e0ad3
11 changed files with 6383 additions and 4183 deletions

View File

@@ -1,49 +1,49 @@
const printf=require('printf');
const fs = require('fs');
const path = require('path');
module.exports = async (bot, api) => {
bot.on('message', async (message) => {
if (!message.content.startsWith(process.env.PREFIX)) {
if (message.channel.id == process.env.TTS_CHANNEL) {
let chan=message.member.voice.channel;
let userRow = await api.db.get('select * from TTSPreferences where user_id=?', message.author.id);
if (!userRow) {
await api.db.run('insert into TTSPreferences (user_id,engine,voice) values (?,?,?)', [message.author.id, api.announcementEngine.shortName, api.announcementVoice]);
userRow = await api.db.get('select * from TTSPreferences where user_id=?', message.author.id);
}
if (api.ttsEngines[userRow.engine]) {
api.speak(chan,message.content, api.ttsEngines[userRow.engine], userRow.voice)
} else {
}
}
}
});
api.registerCommand('myvoice', async (args, message) => {
let userEngine, userVoice;
if (args.length > 3) {
return api.respond(message, printf(api.strings.TOO_MANY_ARGUMENTS));
}
if (api.ttsEngines[args[1]]) {
userEngine = args[1];
if (api.ttsEngines[userEngine].validateVoice(args[2].toLowerCase())) {
userVoice = args[2].toLowerCase();
api.respond(message, printf(api.strings.USER_VOICE_CHANGED, userVoice, api.ttsEngines[userEngine].longName));
} else {
userVoice = api.ttsEngines[userEngine].getDefaultVoice();
api.respond(message, printf(api.strings.INVALID_VOICE, userVoice, api.ttsEngines[userEngine].longName));
}
await api.db.run('update TTSPreferences set engine=?, voice=? where user_id=?', userEngine, userVoice, message.author.id);
} else {
api.respond(message, printf(api.strings.INVALID_ENGINE, args[1]));
}
});
api.registerCommand('random', async (args, message) => {
const files = fs.readdirSync(process.env["VOICE_TMP_PATH"]);
const rnd = files[Math.floor(Math.random()*files.length)];
console.log(rnd);
api.queue.add(__dirname + "/../../sysmsg.wav");
api.queue.add(process.env["VOICE_TMP_PATH"] + "/" + rnd);
});
const printf=require('printf');
const fs = require('fs');
const path = require('path');
module.exports = async (bot, api) => {
bot.on('messageCreate', async (message) => {
if (!message.content.startsWith(process.env.PREFIX)) {
if (message.channel.id == process.env.TTS_CHANNEL) {
let chan=message.member.voice.channel;
let userRow = await api.db.get('select * from TTSPreferences where user_id=?', message.author.id);
if (!userRow) {
await api.db.run('insert into TTSPreferences (user_id,engine,voice) values (?,?,?)', [message.author.id, api.announcementEngine.shortName, api.announcementVoice]);
userRow = await api.db.get('select * from TTSPreferences where user_id=?', message.author.id);
}
if (api.ttsEngines[userRow.engine]) {
api.speak(chan,message.content, api.ttsEngines[userRow.engine], userRow.voice)
} else {
}
}
}
});
api.registerCommand('myvoice', async (args, message) => {
let userEngine, userVoice;
if (args.length > 3) {
return api.respond(message, printf(api.strings.TOO_MANY_ARGUMENTS));
}
if (api.ttsEngines[args[1]]) {
userEngine = args[1];
if (api.ttsEngines[userEngine].validateVoice(args[2].toLowerCase())) {
userVoice = args[2].toLowerCase();
api.respond(message, printf(api.strings.USER_VOICE_CHANGED, userVoice, api.ttsEngines[userEngine].longName));
} else {
userVoice = api.ttsEngines[userEngine].getDefaultVoice();
api.respond(message, printf(api.strings.INVALID_VOICE, userVoice, api.ttsEngines[userEngine].longName));
}
await api.db.run('update TTSPreferences set engine=?, voice=? where user_id=?', userEngine, userVoice, message.author.id);
} else {
api.respond(message, printf(api.strings.INVALID_ENGINE, args[1]));
}
});
api.registerCommand('random', async (args, message) => {
const files = fs.readdirSync(process.env["VOICE_TMP_PATH"]);
const rnd = files[Math.floor(Math.random()*files.length)];
console.log(rnd);
api.queue.add(__dirname + "/../../sysmsg.wav");
api.queue.add(process.env["VOICE_TMP_PATH"] + "/" + rnd);
});
}

View File

@@ -1,32 +1,33 @@
const printf=require('printf');
const AudioQueue=require('../../AudioQueue.js')
module.exports = function (bot, api) {
bot.on('voiceStateUpdate', async (oldState, newState) => {
if (newState.member.user.bot) return;
if (oldState.channel && newState.channel) return;
const channel = oldState.channel || newState.channel;
if (!channel) return;
if (channel.members.array().length < 2) {
return await api.leaveChannel(channel);
}
await api.joinChannel(channel);
let joined = false;
if (!oldState.channel) {
joined = true;
let conn=api.getConnectionForVoiceChannel(channel);
api.queue=new AudioQueue(conn);
}
let username = newState.member.displayName;
let str = "";
if (!joined) {
str = printf(api.strings.USER_LEFT, username);
} else {
str = printf(api.strings.USER_JOINED, username);
}
const filepath = await api.generateVoice(str, api.announcementEngine, api.announcementVoice);
api.queue.add(__dirname + "/sysmsg.wav");
api.queue.add(filepath);
})
}
const printf=require('printf');
const AudioQueue=require('../../AudioQueue.js')
module.exports = function (bot, api) {
bot.on('voiceStateUpdate', async (oldState, newState) => {
console.log("Voice state update");
if (newState.member.user.bot) return;
if (oldState.channel && newState.channel) return;
const channel = oldState.channel || newState.channel;
if (!channel) return;
if (channel.members.size < 1) {
return await api.leaveChannel(channel);
}
await api.joinChannel(channel);
let joined = false;
if (!oldState.channel) {
joined = true;
let conn=api.getConnectionForVoiceChannel(channel);
api.queue=new AudioQueue(conn, api);
}
let username = newState.member.displayName;
let str = "";
if (!joined) {
str = printf(api.strings.USER_LEFT, username);
} else {
str = printf(api.strings.USER_JOINED, username);
}
const filepath = await api.generateVoice(str, api.announcementEngine, api.announcementVoice);
api.queue.add(__dirname + "/sysmsg.wav");
api.queue.add(filepath);
})
}

20
modules/chatgpt/index.js Normal file
View File

@@ -0,0 +1,20 @@
let ChatGPTAPI = null;
module.exports = function (bot, api) {
import("chatgpt").then((mod) => {
ChatGPTAPI = mod.ChatGPTAPI;
});
api.registerCommand('chat', async (args, message) => {
const response = await getChatGPTResponse(message.content.slice(6).trim());
api.respond(message, response);
});
}
async function getChatGPTResponse(prompt) {
const api = new ChatGPTAPI({
apiKey: process.env.OPENAI_API_KEY
})
const res = await api.sendMessage(prompt);
return res.text;
}

View File

@@ -1,16 +1,17 @@
const fetch = require('node-fetch');
module.exports = function (bot, api) {
api.registerCommand('randomquote', async (args, message) => {
const data = await fetch(process.env["QDB_URL"], {
headers: {
Authorization: 'Basic ' + Buffer.from(`${process.env["QDB_USER"]}:${process.env["QDB_PASS"]}`).toString('base64')
}
});
const quotes = await data.json();
const quote = quotes[Math.floor(Math.random()*quotes.length)];
let chan=message.member.voice.channel;
api.queue.add(__dirname + "/sysmsg.wav");
api.speak(chan, `${quote.author}, on ${quote.medium}: ${quote.quote}`);
})
const fetch = require('node-fetch');
module.exports = function (bot, api) {
api.registerCommand('randomquote', async (args, message) => {
const data = await fetch(process.env["QDB_URL"], {
headers: {
Authorization: 'Basic ' + Buffer.from(`${process.env["QDB_USER"]}:${process.env["QDB_PASS"]}`).toString('base64')
}
});
const quotes = await data.json();
const quote = quotes[Math.floor(Math.random()*quotes.length)];
let chan=message.member.voice.channel;
// api.queue.add(__dirname + "/sysmsg.wav");
// api.speak(chan, `${quote.author}, on ${quote.medium}: ${quote.quote}`);
api.respond(message, `Here's your quote: ${quote.author}, on ${author.medium}: ${quote.quote}`);
})
}

View File

@@ -1,23 +1,23 @@
const printf=require('printf');
module.exports = function (bot, api) {
api.registerCommand('announcevoice', (args, message) => {
let channel = bot.voice.connections.first().channel;
if (args.length > 3) {
return api.respond(message, printf(api.strings.TOO_MANY_ARGUMENTS));
}
if (api.ttsEngines[args[1]]) {
api.announcementEngine = api.ttsEngines[args[1]];
if (api.announcementEngine.validateVoice(args[2])) {
api.announcementVoice = args[2];
api.respond(message, printf(api.strings.SYSTEM_VOICE_CHANGED, api.announcementVoice, api.announcementEngine.longName));
} else {
api.announcementVoice = api.announcementEngine.getDefaultVoice();
api.respond(message, printf(api.strings.INVALID_VOICE, api.announcementVoice, api.announcementEngine.longName));
}
} else {
api.respond(message, printf(api.strings.INVALID_ENGINE, args[1]));
}
});
api.registerCommand('flush',()=>api.queue.flush());
const printf=require('printf');
module.exports = function (bot, api) {
api.registerCommand('announcevoice', (args, message) => {
let channel = api.getActiveVoiceChannel();
if (args.length > 3) {
return api.respond(message, printf(api.strings.TOO_MANY_ARGUMENTS));
}
if (api.ttsEngines[args[1]]) {
api.announcementEngine = api.ttsEngines[args[1]];
if (api.announcementEngine.validateVoice(args[2])) {
api.announcementVoice = args[2];
api.respond(message, printf(api.strings.SYSTEM_VOICE_CHANGED, api.announcementVoice, api.announcementEngine.longName));
} else {
api.announcementVoice = api.announcementEngine.getDefaultVoice();
api.respond(message, printf(api.strings.INVALID_VOICE, api.announcementVoice, api.announcementEngine.longName));
}
} else {
api.respond(message, printf(api.strings.INVALID_ENGINE, args[1]));
}
});
api.registerCommand('flush',()=>api.queue.flush());
}

View File

@@ -1,14 +1,14 @@
const AudioQueue=require('../../AudioQueue.js')
module.exports = function (bot, api) {
bot.on('ready', async () => {
console.log("Bot initialized and listening");
const guild = await bot.guilds.fetch(process.env.GUILD);
const channel = await bot.channels.fetch(process.env.CHANNEL);
await api.joinChannel(channel);
let conn=api.getConnectionForVoiceChannel(channel);
api.queue=new AudioQueue(conn);
api.queue.add(__dirname + "/../../sysstart.wav");
api.speak(channel, api.strings.WELCOME);
})
const AudioQueue=require('../../AudioQueue.js')
module.exports = function (bot, api) {
bot.on('ready', async () => {
console.log("Bot initialized and listening");
const guild = await bot.guilds.fetch(process.env.GUILD);
const channel = await bot.channels.fetch(process.env.CHANNEL);
await api.joinChannel(channel);
let conn=api.getConnectionForVoiceChannel(channel);
api.queue=new AudioQueue(conn, api);
api.queue.add(__dirname + "/../../sysstart.wav");
api.speak(channel, api.strings.WELCOME);
})
}