forked from Talon/tardis-bot
Update bot to new discord API and add simple chatgpt module
This commit is contained in:
60
index.js
60
index.js
@@ -1,4 +1,6 @@
|
||||
const Discord = require('discord.js');
|
||||
const Voice = require("@discordjs/voice");
|
||||
const adapterCreator = require("./adapter");
|
||||
require('dotenv').config();
|
||||
const fetch = require('node-fetch');
|
||||
const fs = require('fs');
|
||||
@@ -7,13 +9,28 @@ const sqlite3 = require('sqlite3');
|
||||
const { open } = require('sqlite')
|
||||
|
||||
let joinedVoiceChannels = [];
|
||||
let modules = [];
|
||||
let commandHandlers = new Map();
|
||||
let joinedVoiceChannelConnections = new Map();
|
||||
|
||||
const bot = new Discord.Client();
|
||||
let modules = [];
|
||||
|
||||
let commandHandlers = new Map();
|
||||
const player = Voice.createAudioPlayer();
|
||||
|
||||
const rest = new Discord.REST({ version: '10' }).setToken(process.env["TOKEN"]);
|
||||
const bot = new Discord.Client({
|
||||
intents: [
|
||||
Discord.GatewayIntentBits.GuildMembers,
|
||||
Discord.GatewayIntentBits.GuildMessageReactions,
|
||||
Discord.GatewayIntentBits.GuildMessages,
|
||||
Discord.GatewayIntentBits.GuildPresences,
|
||||
Discord.GatewayIntentBits.GuildVoiceStates,
|
||||
Discord.GatewayIntentBits.Guilds,
|
||||
Discord.GatewayIntentBits.MessageContent
|
||||
]
|
||||
});
|
||||
|
||||
async function initDB() {
|
||||
console.log(__dirname);
|
||||
console.log(__dirname);
|
||||
api.db = await open({
|
||||
filename: process.env["DB_FILE"],
|
||||
driver: sqlite3.Database
|
||||
@@ -21,9 +38,10 @@ console.log(__dirname);
|
||||
}
|
||||
|
||||
const api = {
|
||||
player: player,
|
||||
db: undefined,
|
||||
queue: undefined,
|
||||
strings: require('./strings/'+process.env.STRING_SET+'.json'),
|
||||
strings: require('./strings/' + process.env.STRING_SET + '.json'),
|
||||
ttsEngines: (() => {
|
||||
let engines = {};
|
||||
console.log(`Registering TTS engines...`);
|
||||
@@ -39,6 +57,9 @@ const api = {
|
||||
announcementVoice: process.env.ANNOUNCEMENT_VOICE,
|
||||
announcementEngine: undefined,
|
||||
|
||||
play: (file) => {
|
||||
return player.play(Voice.createAudioResource(file));
|
||||
},
|
||||
respond: (message, text, voiceText) => {
|
||||
let toSend = message.member.displayName + ", " + (voiceText ? voiceText : text);
|
||||
if (message.member.voice.channel) {
|
||||
@@ -49,12 +70,14 @@ const api = {
|
||||
}
|
||||
},
|
||||
|
||||
getActiveVoiceChannel: () => joinedVoiceChannels[0],
|
||||
|
||||
isInVoiceChannel: (channel) => {
|
||||
return joinedVoiceChannels.includes(channel);
|
||||
},
|
||||
|
||||
getConnectionForVoiceChannel: (channel) => {
|
||||
return bot.voice.connections.find((conn) => conn.channel === channel);
|
||||
return joinedVoiceChannelConnections.get(channel);
|
||||
},
|
||||
|
||||
generateVoice: async (string, engine, voice, params) => {
|
||||
@@ -68,15 +91,24 @@ const api = {
|
||||
|
||||
joinChannel: async (channel) => {
|
||||
if (!api.isInVoiceChannel(channel)) {
|
||||
const res = await channel.join();
|
||||
|
||||
const res = Voice.joinVoiceChannel({
|
||||
channelId: channel.id,
|
||||
guildId: channel.guild.id,
|
||||
adapterCreator: adapterCreator(channel)
|
||||
});
|
||||
res.subscribe(player);
|
||||
joinedVoiceChannels.push(channel);
|
||||
joinedVoiceChannelConnections.set(channel, res);
|
||||
}
|
||||
},
|
||||
|
||||
leaveChannel: async (channel) => {
|
||||
if (joinedVoiceChannels.includes(channel)) {
|
||||
let con = joinedVoiceChannelConnections.get(channel);
|
||||
joinedVoiceChannels = joinedVoiceChannels.filter((chan) => chan !== channel);
|
||||
await channel.leave();
|
||||
con.disconnect();
|
||||
joinedVoiceChannelConnections.delete(channel);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -95,13 +127,14 @@ function registerModules() {
|
||||
const moduleDirectories = fs.readdirSync('./modules');
|
||||
moduleDirectories.forEach((dir) => {
|
||||
if (dir.startsWith('.')) return;
|
||||
modules.push(require(`./modules/${dir}/index.js`));
|
||||
modules.push(require(`./modules/${dir}`));
|
||||
console.log(`Loading ./modules/${dir}/index.js`)
|
||||
})
|
||||
modules.forEach((mod) => mod(bot, api));
|
||||
}
|
||||
|
||||
function handleMessage(message) {
|
||||
console.log(`I got message`);
|
||||
if (message.content.startsWith(process.env.PREFIX)) {
|
||||
const args = message.content.split(" ");
|
||||
const command = args[0].substr(1, args[0].length);
|
||||
@@ -114,9 +147,12 @@ function handleMessage(message) {
|
||||
|
||||
|
||||
api.announcementEngine = api.ttsEngines[process.env.ANNOUNCEMENT_ENGINE];
|
||||
(async () => {
|
||||
|
||||
async function start() {
|
||||
await initDB();
|
||||
registerModules();
|
||||
})()
|
||||
}
|
||||
bot.login(process.env.TOKEN);
|
||||
bot.on('message', handleMessage);
|
||||
bot.on('messageCreate', handleMessage);
|
||||
|
||||
start();
|
||||
Reference in New Issue
Block a user