mirror of
https://github.com/Myxelium/RandomMemerBot.git
synced 2026-07-09 06:25:08 +00:00
Added cron job, updated webserver, added retry functionality
This commit is contained in:
76
bot.ts
76
bot.ts
@@ -9,10 +9,15 @@ import {
|
||||
import { ChannelType, Client, GatewayIntentBits } from 'discord.js';
|
||||
import * as dotenv from 'dotenv';
|
||||
import * as fileSystem from 'fs';
|
||||
import { startServer } from './client/webserver';
|
||||
import { startServer as startWebServer } from './client/webserver';
|
||||
import * as schedule from 'node-schedule';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const minTime = parseInt(process.env.INTERVALMIN_MINUTES!, 10); // Minimum interval in minutes
|
||||
const maxTime = convertHoursToMinutes(parseInt(process.env.INTERVALMAX_HOURS!, 10)); // 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({
|
||||
@@ -29,35 +34,55 @@ 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";
|
||||
}
|
||||
|
||||
client.login(token);
|
||||
|
||||
client.on('ready', () => {
|
||||
|
||||
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`);
|
||||
startServer();
|
||||
console.log(`Logged in as ${client.user?.tag}!`);
|
||||
joinRandomChannel();
|
||||
|
||||
joinRandomChannel(voiceChannelRetries);
|
||||
startWebServer();
|
||||
});
|
||||
|
||||
async function joinRandomChannel() {
|
||||
if (!client.guilds.cache.size)
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
async function joinRandomChannel(retries = 12) {
|
||||
if (!client.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 =>
|
||||
channel.type === ChannelType.GuildVoice &&
|
||||
channel.members.size > 0
|
||||
);
|
||||
|
||||
if (!voiceChannels?.size)
|
||||
if (!voiceChannels?.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
|
||||
}
|
||||
|
||||
if(retries === 0) {
|
||||
console.log(LoggerColors.Red, 'No voice channels found');
|
||||
scheduleNextJoin();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const voiceChannel = voiceChannels.random();
|
||||
|
||||
|
||||
try {
|
||||
const connection = await joinVoiceChannel({
|
||||
const connection = joinVoiceChannel({
|
||||
channelId: voiceChannel!.id,
|
||||
guildId: voiceChannel!.guild.id,
|
||||
adapterCreator: voiceChannel!.guild.voiceAdapterCreator,
|
||||
@@ -69,7 +94,7 @@ async function joinRandomChannel() {
|
||||
const resource = createAudioResource(soundFile);
|
||||
const player = createAudioPlayer();
|
||||
|
||||
console.log(LoggerColors.Yellow, `Playing ${soundFile} in ${voiceChannel?.name}...`);
|
||||
console.log(LoggerColors.Teal, `Playing ${soundFile} in ${voiceChannel?.name}...`);
|
||||
|
||||
player.play(resource);
|
||||
connection.subscribe(player);
|
||||
@@ -80,17 +105,30 @@ async function joinRandomChannel() {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
const waitTime = Math.floor(Math.random() * (43200000 - 10000 + 1)) + 10000;
|
||||
log(waitTime);
|
||||
setTimeout(joinRandomChannel, waitTime);
|
||||
scheduleNextJoin();
|
||||
}
|
||||
|
||||
function scheduleNextJoin(){
|
||||
const randomInterval = Math.floor(Math.random() * (maxTime - minTime + 1)) + minTime;
|
||||
log(randomInterval);
|
||||
|
||||
schedule.scheduleJob(`*/${randomInterval} * * * *`, function(){
|
||||
joinRandomChannel();
|
||||
});
|
||||
}
|
||||
|
||||
function convertHoursToMinutes(hours: number){
|
||||
return hours * 60;
|
||||
}
|
||||
|
||||
function log(waitTime: number){
|
||||
const currentTime = new Date();
|
||||
const nextJoinTime = new Date(currentTime.getTime() + waitTime * 60 * 1000); // Convert waitTime from minutes to milliseconds
|
||||
|
||||
console.log(
|
||||
LoggerColors.Cyan, `
|
||||
Wait time: ${(waitTime / 1000 / 60 > 60) ? (waitTime / 1000 / 60 / 60 + ' hours') : (waitTime / 1000 / 60 + ' minutes')},
|
||||
Current time: ${new Date().toLocaleTimeString()},
|
||||
Next join time: ${new Date(Date.now() + waitTime)
|
||||
.toLocaleTimeString()}`
|
||||
Wait time: ${waitTime} minutes,
|
||||
Current time: ${currentTime.toLocaleTimeString()},
|
||||
Next join time: ${nextJoinTime.toLocaleTimeString()}`
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user