Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
DCC-Server/index.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
115 lines (95 sloc)
3.51 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// DiscordCC | |
const fs = require('fs'); | |
const dotenv = require('dotenv'); | |
const discord = require('discord.js'); | |
const config = require('./config.json'); | |
const { speechToText } = require("./speechToText"); | |
const { toMp3 } = require("./toMp3"); | |
const PubNub = require('pubnub'); | |
const { sendMsg } = require('./sendMsg'); | |
dotenv.config(); | |
const client = new discord.Client(); | |
exports.client = client; | |
const pubnub = new PubNub({ | |
publishKey: process.env.PUBNUB_PUBLISH_KEY, | |
subscribeKey: process.env.PUBNUB_SUBSCRIBE_KEY | |
}); | |
exports.pubnub = pubnub; | |
client.commands = new discord.Collection(); | |
// Load the commands from the /commands folder | |
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); | |
for (const file of commandFiles) { | |
const command = require(`./commands/${file}`); | |
client.commands.set(command.name, command); | |
} | |
// Called as soon as the bot is ready to take commands | |
client.on('ready', () => { | |
// Clear the server cache | |
client.commands.get('clearCache').execute(); | |
pubnub.subscribe({ | |
channels: ['experiment'] | |
}); | |
pubnub.addListener({ | |
message: (log) => { | |
fs.appendFileSync('./logs/experiment.txt', `\n${log.message}`); | |
} | |
}); | |
connect(); | |
}); | |
// Connect to the voice channel specified in the config file | |
async function connect() { | |
try { | |
const connection = await client.channels.cache.get(config.generalChannelID).join(); | |
listen(connection); | |
} catch (err) { | |
console.log(err); | |
} | |
} | |
// Records audio from a single user into an /temp folder file | |
function listen(connection){ | |
connection.on('speaking', async (user, speaking) => { | |
// Get the user's nickname in this server | |
const nickname = await client.guilds.fetch(config.serverID) | |
.then((guild) => { return guild.members.cache.get(user.id).displayName; }); | |
// Notify the clients that the user started or stopped speaking | |
if (speaking.bitfield == 1) | |
sendMsg(nickname, `${config.prefix}voice ${speaking.bitfield}`); | |
// Return if the user has stopped speaking or if it is a bot speaking | |
if (speaking.bitfield == 0 || user.bot) return; | |
let audioStream = connection.receiver.createStream(user, { mode: 'pcm' }) | |
// Pipe the voice input into an mp3 file | |
const path = await toMp3(nickname, audioStream); | |
// Transcribe the audio | |
audioStream.on('end', async () => { | |
if (fs.statSync(path).size < 10) { | |
fs.unlinkSync(path); | |
return; | |
} | |
speechToText(nickname, path); | |
}); | |
audioStream.on('error', (err) => { | |
console.log(err); | |
}); | |
}); | |
} | |
// Called every time someone sends a message to the chat | |
client.on('message', message => { | |
// If it is a message sent by the bot - return | |
if (message.author.bot) return | |
// if it is a valid DCC command - execute it | |
if (message.content.startsWith(config.prefix)) { | |
const args = message.content.slice(config.prefix.length).split(' '); | |
const commandName = args.shift().toLowerCase(); | |
if(!client.commands.has(commandName)) return; | |
const command = client.commands.get(commandName); | |
if (message.member.hasPermission("ADMINISTRATOR")) | |
{ | |
try { | |
command.execute(message, args); | |
} catch (error) { | |
console.error(error); | |
} | |
} | |
} | |
}); | |
client.login(process.env.LISTENER_TOKEN); |