Simple http server

This commit is contained in:
Andrew Lee 2022-07-10 13:58:09 -04:00
parent 626cef8516
commit 10e93faf89
Signed by: andrew
GPG key ID: 4DCE67C47836D125
5 changed files with 40 additions and 10 deletions

View file

@ -11,7 +11,7 @@ Make a new file called `config.json`.
```
{
"token": "token_here",
"txtFile": true/false
"txtFile": true/false,
"statusChannel": "channel_id",
"voiceChannel": "voice_channel_id"
"guildID": "guild_id",

35
bot.js
View file

@ -22,15 +22,46 @@ import { Client, MessageEmbed, Collection, version } from 'discord.js';
import { voiceInit } from './AudioBackend.js';
import { readdirSync, readFileSync } from 'node:fs';
// import config from './config.json' assert { type: 'json' } Not supported by ESLint yet
import { createServer } from 'node:http';
const config = JSON.parse(readFileSync('./config.json'));
const bot = new Client({ intents: ['GUILDS', 'GUILD_MESSAGES', 'GUILD_VOICE_STATES'] });
bot.login(config.token);
const port = 1337;
const server = createServer((req, res) => {
let body = '{"test": "test"}';
// Get the data as utf8 strings.
// If an encoding is not set, Buffer objects will be received.
req.setEncoding('utf8');
// Readable streams emit 'data' events once a listener is added.
req.on('data', (chunk) => {
body += chunk;
});
// The 'end' event indicates that the entire body has been received.
req.on('end', () => {
try {
const data = JSON.parse(body);
// Write back something interesting to the user:
res.write(typeof data);
res.end();
} catch (er) {
// uh oh! bad json!
res.statusCode = 400;
return res.end(`error: ${er.message}`);
}
});
});
server.listen(port);
console.log(`Web server started! Port: ${port}`);
// bot.login(config.token);
/**
* Project Ideas:
* Shuffle or "Play by order" mode
* Audio streaming
*/

View file

@ -33,9 +33,7 @@ export default {
const beats = readdirSync(musicFolder).join('\n');
readdir(musicFolder, async(err, files) => {
await interaction.reply(
`Listing ${files.length} audio tracks...\n\`\`\`\n${beats}\n\`\`\``
);
await interaction.reply(`Listing ${files.length} audio tracks...\n\`\`\`\n${beats}\n\`\`\``);
if (err) {
console.error(err);
}

View file

@ -21,7 +21,7 @@
import { SlashCommandBuilder } from '@discordjs/builders';
import { MessageEmbed } from 'discord.js';
import { audio, audioArray, currentTrack, playerState } from '../AudioBackend.js';
import { audio, audioArray, currentTrack, files, playerState } from '../AudioBackend.js';
export default {
data: new SlashCommandBuilder()
@ -37,6 +37,7 @@ export default {
const controlEmbed = new MessageEmbed()
.setAuthor({ name: `${bot.user.username} Status`, iconURL: bot.user.avatarURL() })
.addField('State', playerState)
.addField('Tracks', `${audioID}/${files.length}`)
.addField('Currently Playing', audio)
.addField('Up Next', audioName)
.setColor('#0066ff');

View file

@ -1,6 +1,6 @@
{
"name": "dlap",
"version": "1.3.0",
"version": "1.4.0",
"type": "module",
"main": "bot.js",
"license": "GPL-3.0",
@ -15,7 +15,7 @@
"discord-api-types": "^0.36.1",
"discord.js": "^13.8.1",
"ffmpeg-static": "^5.0.0",
"libsodium-wrappers": "^0.7.10"
"sodium": "^3.0.2"
},
"devDependencies": {
"eslint": "^8.0.1",