Larger refactoring and added avoidlist

This commit is contained in:
Myx
2023-10-21 05:09:38 +02:00
parent ccf7a218fd
commit 8fd3eeae03
30 changed files with 753 additions and 213 deletions

7
helpers/converters.ts Normal file
View File

@@ -0,0 +1,7 @@
export function dateToString(date: Date): string {
return date.toLocaleString('sv-SE', { timeZone: 'Europe/Stockholm' });
}
export function convertHoursToMinutes(hours: number): number {
return hours * 60;
}

View File

@@ -0,0 +1,18 @@
/**
* Formats a name into a file name with random generated value at the end.
* @param name - The name to generate a file name for.
* @returns string - The generated file name.
*/
export function generateFileName(name: string): string {
const randomHex = [...Array(3)].map(() => Math.floor(Math.random() * 16).toString(16)).join('');
const formattedName = name
.replace(/\(.*?\)|\[.*?\]/g, '')
.split(' ')
.filter(word => /^[a-zA-Z0-9]/.test(word))
.join(' ')
.replace(/\s+/g, ' ')
.replace('.mp3', '');
return `${formattedName}-${randomHex}.mp3`;
}

View File

@@ -0,0 +1,11 @@
import * as fileSystem from 'fs';
export const soundsDirectory = './sounds/';
/**
* Returns a random sound file from the sounds directory.
* @returns string - The path to a random sound file.
*/
export function getRandomSoundFilePath(): string {
const allSoundFilesAsArray = fileSystem.readdirSync(soundsDirectory).filter(file => file.endsWith('.mp3'));
return soundsDirectory + allSoundFilesAsArray[Math.floor(Math.random() * allSoundFilesAsArray.length)];
}

View File

@@ -0,0 +1,10 @@
import * as fileSystem from 'fs';
import { AvoidList } from '../models/avoid-list';
/**
* Returns a list of users that the bot should avoid being in same channel as.
* @returns avoidList
*/
export function loadAvoidList(): AvoidList {
return JSON.parse(fileSystem.readFileSync("avoid-list.json", 'utf-8'));
}

7
helpers/logger-colors.ts Normal file
View File

@@ -0,0 +1,7 @@
export class LoggerColors {
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";
}

24
helpers/logger.ts Normal file
View File

@@ -0,0 +1,24 @@
import { dateToString } from "./converters";
import { LoggerColors } from "./logger-colors";
/**
* 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.
*/
export function logger(
waitTime: Date,
hour: number,
minute: number
){
const currentTime = new Date();
console.log(
LoggerColors.Cyan, `
Wait time: ${(waitTime.getTime() - currentTime.getTime()) / 60000} minutes,
Current time: ${dateToString(currentTime)},
Next join time: ${dateToString(waitTime)},
Cron: ${Math.floor(minute)} ${Math.floor(hour) == 0 ? '*' : Math.floor(hour) } * * *`
);
}

View File

@@ -0,0 +1,18 @@
import { Client, GatewayIntentBits } from "discord.js";
export function SetupDiscordCLient(): Client{
const discordClient = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildIntegrations,
],
});
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
discordClient.login(discordApplicationToken);
return discordClient;
}