Documentation and variable renaming

Made it easier for other developers to understand the code?
This commit is contained in:
Myx
2023-10-18 01:17:08 +02:00
parent c25bc12896
commit 0ea4cb7bdd
2 changed files with 135 additions and 53 deletions

154
bot.ts
View File

@@ -1,12 +1,20 @@
/**
* This file contains the code for a Discord bot that joins a random voice channel in a random guild and plays a random sound file.
* It uses the @discordjs/voice library for voice connections and the node-schedule library for scheduling the next join.
* The bot also starts a web server using the startServer function from the webserver module.
*
* @packageDocumentation
*/
import {
joinVoiceChannel,
createAudioPlayer,
createAudioResource,
entersState,
AudioPlayerStatus,
VoiceConnectionStatus
VoiceConnectionStatus,
VoiceConnection
} from '@discordjs/voice';
import { ChannelType, Client, GatewayIntentBits } from 'discord.js';
import { ChannelType, Client, GatewayIntentBits, GuildBasedChannel } from 'discord.js';
import * as dotenv from 'dotenv';
import * as fileSystem from 'fs';
import { startServer as startWebServer } from './client/webserver';
@@ -14,14 +22,14 @@ import * as schedule from 'node-schedule';
dotenv.config();
export var nextPlayBackTime: string = '';
const minTime = parseInt(process.env.INTERVALMIN_MINUTES!, 10); // Minimum interval in minutes
const maxTime = convertHoursToMinutes(parseFloat(process.env.INTERVALMAX_HOURS!)); // Maximum interval in minutes
const voiceChannelRetries = parseInt(process.env.VOICECHANNELRETRIES!, 10); // Number of retries to find a voice channel with members in it
export var nextPlayBackTime: string = ''; // Export so it can be used in the webserver module aswell.
const minTimeInterval = parseInt(process.env.INTERVALMIN_MINUTES!, 10); // Minimum interval in minutes.
const maxTimeInterval = convertHoursToMinutes(parseFloat(process.env.INTERVALMAX_HOURS!)); // Maximum interval in minutes.
const voiceChannelRetries = parseInt(process.env.VOICECHANNELRETRIES!, 10); // Number of retries to find a voice channel with members in it.
const token = process.env.TOKEN;
const soundsDir = './sounds/';
const client = new Client({
const discordApplicationToken = process.env.TOKEN; // Discord bot token from .env file (required) More info: https://discordgsm.com/guide/how-to-get-a-discord-bot-token
const soundsDirectory = './sounds/';
const discordClient = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
@@ -32,18 +40,18 @@ const client = new Client({
});
export class LoggerColors {
public static readonly Green = "\x1b[32m%s\x1b[0m";
public static readonly Yellow = "\x1b[33m%s\x1b[0m";
public static readonly Cyan = "\x1b[36m%s\x1b[0m";
public static readonly Red = "\x1b[31m%s\x1b[0m";
public static readonly Teal = "\x1b[35m%s\x1b[0m";
public static readonly Green: string = "\x1b[32m%s\x1b[0m";
public static readonly Yellow: string = "\x1b[33m%s\x1b[0m";
public static readonly Cyan: string = "\x1b[36m%s\x1b[0m";
public static readonly Red: string = "\x1b[31m%s\x1b[0m";
public static readonly Teal: string = "\x1b[35m%s\x1b[0m";
}
client.login(token);
discordClient.login(discordApplicationToken);
client.on('ready', async () => {
console.log(LoggerColors.Green, `Add to server by: https://discord.com/oauth2/authorize?client_id=${client.application?.id}&permissions=70379584&scope=bot`);
console.log(`Logged in as ${client.user?.tag}!`);
discordClient.on('ready', async () => {
console.log(LoggerColors.Green, `Add to server by: https://discord.com/oauth2/authorize?client_id=${discordClient.application?.id}&permissions=70379584&scope=bot`);
console.log(`Logged in as ${discordClient.user?.tag}!`);
joinRandomChannel(voiceChannelRetries);
startWebServer();
@@ -52,22 +60,22 @@ client.on('ready', async () => {
/**
* Joins a random voice channel in a random guild and plays a random sound file.
* @param retries - The number of retries to attempt if no voice channels are found.
* @returns void
* @returns Promise<void>
*/
async function joinRandomChannel(retries = 12) {
if (!client.guilds.cache.size) {
if (!discordClient.guilds.cache.size) {
console.log(LoggerColors.Red, 'No guilds found');
scheduleNextJoin();
return;
}
const guild = client.guilds.cache.random();
const voiceChannels = guild?.channels.cache.filter(channel =>
const randomlyPickedDiscordServer = discordClient.guilds.cache.random();
const accessableVoiceChannels = randomlyPickedDiscordServer?.channels.cache.filter(channel =>
channel.type === ChannelType.GuildVoice &&
channel.members.size > 0
);
if (!voiceChannels?.size) {
if (!accessableVoiceChannels?.size) {
if (retries > 0) {
console.log(LoggerColors.Yellow, `No voice channels found, retrying in 5 seconds... (${retries} retries left)`);
setTimeout(() => joinRandomChannel(retries - 1), 5000); // Wait for 5 seconds before retrying
@@ -80,35 +88,30 @@ async function joinRandomChannel(retries = 12) {
return;
}
const voiceChannel = voiceChannels.random();
const randomlyPickedVoiceChannel = accessableVoiceChannels.random();
try {
const connection = joinVoiceChannel({
channelId: voiceChannel!.id,
guildId: voiceChannel!.guild.id,
adapterCreator: voiceChannel!.guild.voiceAdapterCreator,
// Join the voice channel
const voiceChannelConnection = joinVoiceChannel({
channelId: randomlyPickedVoiceChannel!.id,
guildId: randomlyPickedVoiceChannel!.guild.id,
adapterCreator: randomlyPickedVoiceChannel!.guild.voiceAdapterCreator,
});
await entersState(connection, VoiceConnectionStatus.Ready, 30e3);
const soundFiles = fileSystem.readdirSync(soundsDir).filter(file => file.endsWith('.mp3'));
const soundFile = soundsDir + soundFiles[Math.floor(Math.random() * soundFiles.length)];
await entersState(voiceChannelConnection, VoiceConnectionStatus.Ready, 30e3);
const soundFilePath = getRandomSoundFilePath();
if(!soundFile) {
if(!soundFilePath) {
console.log(LoggerColors.Red, 'No sound files found');
scheduleNextJoin();
return;
}
const resource = createAudioResource(soundFile);
const player = createAudioPlayer();
console.log(LoggerColors.Teal, `Playing ${soundFile} in ${voiceChannel?.name}...`);
await playSoundFile(
soundFilePath,
randomlyPickedVoiceChannel,
voiceChannelConnection);
player.play(resource);
connection.subscribe(player);
await entersState(player, AudioPlayerStatus.Idle, 300000);
connection.destroy();
} catch (error) {
console.error(error);
}
@@ -116,31 +119,78 @@ async function joinRandomChannel(retries = 12) {
scheduleNextJoin();
}
function scheduleNextJoin(){
const randomInterval = Math.floor(Math.random() * (maxTime - minTime + 1)) + minTime;
/**
* Plays a sound file in a voice channel.
* @param soundFilePath - The path to the sound file to play.
* @param randomlyPickedVoiceChannel - The voice channel to play the sound file in.
* @param voiceChannelConnection - The voice channel connection.
* @returns Promise<void>
*/
async function playSoundFile(
soundFilePath: string,
randomlyPickedVoiceChannel: GuildBasedChannel | undefined,
voiceChannelConnection: VoiceConnection
): Promise<void> {
const audioResource = createAudioResource(soundFilePath);
const audioPlayer = createAudioPlayer();
console.log(LoggerColors.Teal, `Playing ${soundFilePath} in ${randomlyPickedVoiceChannel?.name}...`);
audioPlayer.play(audioResource);
voiceChannelConnection.subscribe(audioPlayer);
await entersState(audioPlayer, AudioPlayerStatus.Idle, 300000);
voiceChannelConnection.destroy();
}
/**
* Returns a random sound file from the sounds directory.
* @returns string - The path to a random sound file.
*/
function getRandomSoundFilePath(): string {
const allSoundFilesAsArray = fileSystem.readdirSync(soundsDirectory).filter(file => file.endsWith('.mp3'));
return soundsDirectory + allSoundFilesAsArray[Math.floor(Math.random() * allSoundFilesAsArray.length)];
}
/**
* Schedules the next join to a random channel. Using a random interval between minTime and maxTime.
* It clears the previous schedule before scheduling the next join, to avoid multiple schedules.
* @see minTimeInterval - time in minutes
* @see maxTimeInterval - time in hours
* @see schedule - node-schedule instance
*/
function scheduleNextJoin(): void {
const randomInterval = Math.floor(Math.random() * (maxTimeInterval - minTimeInterval + 1)) + minTimeInterval;
const minutes = randomInterval % 60;
const hours = Math.floor(randomInterval / 60);
schedule.gracefulShutdown().finally(() => {
console.log(`${Math.floor(minutes)} ${Math.floor(hours) == 0 ? '*' : Math.floor(hours) } * * *`)
let jobname = schedule.scheduleJob(`${Math.floor(minutes)} ${Math.floor(hours) == 0 ? '*' : Math.floor(hours) } * * *`, function(){
const jobName = schedule.scheduleJob(`${Math.floor(minutes)} ${Math.floor(hours) == 0 ? '*' : Math.floor(hours) } * * *`, function(){
joinRandomChannel();
}).name;
console.log(schedule.scheduledJobs[jobname].nextInvocation().toLocaleString())
let nextPlaybackDate = schedule.scheduledJobs[jobname].nextInvocation();
let nextPlaybackDate = schedule.scheduledJobs[jobName].nextInvocation();
nextPlayBackTime = dateToString(nextPlaybackDate) ?? '';
log(nextPlaybackDate, hours, minutes);
});
}
function convertHoursToMinutes(hours: number){
function convertHoursToMinutes(hours: number): number {
return hours * 60;
}
function log(waitTime: Date, preHour: number, preMinute: number){
/**
* Logs the wait time, current time, next join time, and cron schedule in the console.
* @param waitTime - The time to wait until the next join.
* @param hour - The hour of the cron schedule.
* @param minute - The minute of the cron schedule.
*/
function log(
waitTime: Date,
hour: number,
minute: number
){
const currentTime = new Date();
console.log(
@@ -148,10 +198,10 @@ function log(waitTime: Date, preHour: number, preMinute: number){
Wait time: ${(waitTime.getTime() - currentTime.getTime()) / 60000} minutes,
Current time: ${dateToString(currentTime)},
Next join time: ${dateToString(waitTime)},
Cron: ${Math.floor(preMinute)} ${Math.floor(preHour) == 0 ? '*' : Math.floor(preHour) } * * *`
Cron: ${Math.floor(minute)} ${Math.floor(hour) == 0 ? '*' : Math.floor(hour) } * * *`
);
}
function dateToString(date: Date){
function dateToString(date: Date): string {
return date.toLocaleString('sv-SE', { timeZone: 'Europe/Stockholm' });
}

View File

@@ -32,14 +32,27 @@ const upload = multer({
}
});
/**
* Returns the index.html file.
* @returns index.html - The index.html file.
*/
app.get('/', (_req, res) => {
res.sendFile(path.join(__dirname, 'web/index.html'));
});
/**
* Uploads a file to the sounds folder.
* @Body myFile - The file to upload.
*/
app.post('/upload', upload.single('myFile'), async (req, res) => {
res.send('File uploaded successfully.');
});
/**
* Uploads a YouTube video as an mp3 file to the sounds folder.
* The video must be shorter than 10 seconds.
* @Body url - The YouTube video url.
*/
app.post('/upload-youtube', async (req, res) => {
const url = req.body.url;
@@ -89,9 +102,17 @@ app.post('/upload-youtube', async (req, res) => {
}
});
// create a enpoint to return a file from the sounds folder (use the file name) with as little code as possible
/**
* Returns a file from the sounds folder by filename
* @param filename - The name of the file to return.
* @returns mp3 - The requested file.
*/
app.use('/sounds', express.static(path.join(__dirname, '../sounds')));
/**
* Returns a list of all sound files in the sounds directory.
* @returns string[] - An array of all sound files in the sounds directory.
*/
app.get('/sounds', (_req, res) => {
const fs = require('fs');
const directoryPath = path.join(__dirname, '../sounds');
@@ -103,10 +124,17 @@ app.get('/sounds', (_req, res) => {
});
});
/**
* Returns the next playback time.
* @returns string - The next playback time.
*/
app.get('/nextplaybacktime', (_req, res) => {
res.send(nextPlayBackTime);
});
/**
* Deletes a file from the sounds folder by filename
*/
app.delete('/sounds/:filename', (req, res) => {
const fs = require('fs');
const directoryPath = path.join(__dirname, '../sounds');
@@ -121,6 +149,10 @@ app.delete('/sounds/:filename', (req, res) => {
app.use(express.static(path.join(__dirname, "web")));
/**
* Starts the web server on either http or https protocol based on the availability of SSL certificates.
* @returns void
*/
export function startServer() {
let port: 80 | 443 = 80;
let server;