1
0
mirror of https://github.com/toast-ts/Daggerbot-TS.git synced 2024-11-18 04:40:59 -05:00
Daggerbot-TS/src/index.ts

69 lines
5.3 KiB
TypeScript
Raw Normal View History

import Discord from 'discord.js';
2023-04-14 06:47:58 -04:00
import TClient from './client.js';
2022-11-11 19:58:11 -05:00
const client = new TClient;
client.init();
2023-09-01 00:32:11 -04:00
import YTModule from './funcs/YTModule.js';
import MPModule from './funcs/MPModule.js';
2023-06-24 13:47:42 -04:00
import {Player} from 'discord-player';
const player = Player.singleton(client);
2023-08-29 20:25:44 -04:00
import MessageTool from './helpers/MessageTool.js';
2023-05-23 01:14:17 -04:00
import {writeFileSync, readFileSync} from 'node:fs';
2022-11-11 19:58:11 -05:00
2023-06-24 13:47:42 -04:00
// Error handler
function DZ(error:Error, type:string){// Yes, I may have shiternet but I don't need to wake up to like a hundred messages or so.
2023-08-30 04:34:59 -04:00
if (JSON.parse(readFileSync('src/errorBlocklist.json', 'utf8')).includes(error.message)) return;// I wonder if my idea works, if not then please run me over with a bulldozer.
2023-08-11 08:19:13 -04:00
console.error(error);
(client.channels.resolve(client.config.mainServer.channels.errors) as Discord.TextChannel | null)?.send({embeds: [new client.embed().setColor('#560000').setTitle('Error caught!').setFooter({text: 'Error type: ' + type}).setDescription(`**Error:**\n\`\`\`${error.message}\`\`\`**Stack:**\n\`\`\`${`${error.stack}`.slice(0, 2500)}\`\`\``)]})
2023-01-11 03:33:38 -05:00
}
2023-03-04 05:04:40 -05:00
process.on('unhandledRejection', (error: Error)=>DZ(error, 'unhandledRejection'));
process.on('uncaughtException', (error: Error)=>DZ(error, 'uncaughtException'));
2023-08-12 12:40:18 -04:00
process.on('error', (error: Error)=>DZ(error, 'nodeError'));
2023-08-17 13:06:00 -04:00
client.on('error', (error: Error)=>DZ(error, 'clientError'));
2022-11-11 19:58:11 -05:00
2023-06-24 13:47:42 -04:00
// Audio Player event handling
2023-06-24 22:17:35 -04:00
if (client.config.botSwitches.music){
2023-08-29 20:34:22 -04:00
player.events.on('playerStart', (queue,track)=>queue.channel.send({embeds:[MessageTool.embedMusic(client.config.embedColor, `Next up: ${track.raw.title} - ${track.raw.author}`,track.raw.thumbnail)]}));
player.events.on('audioTrackAdd', (queue,track)=>queue.channel.send({embeds:[MessageTool.embedMusic(client.config.embedColorGreen, `Added: ${track.raw.title} - ${track.raw.author}`,track.raw.thumbnail)]}));
player.events.on('audioTrackRemove', (queue, track)=>queue.channel.send({embeds:[MessageTool.embedMusic(client.config.embedColor, `Removed: ${track.raw.title} - ${track.raw.author}`,track.raw.thumbnail)]}));
2023-06-26 00:13:31 -04:00
player.events.on('emptyQueue', queue=>{
2023-06-24 22:17:35 -04:00
if (queue.tracks.size < 1) return queue.channel.send('There\'s no songs left in the queue, leaving voice channel in 15 seconds.').then(()=>setTimeout(()=>queue.connection.disconnect(), 15000))
2023-06-26 00:13:31 -04:00
});
2023-08-29 20:34:22 -04:00
player.events.on('playerPause', queue=>queue.channel.send({embeds:[MessageTool.embedMusic(client.config.embedColor, 'Player has been paused.\nRun the command to unpause it')]}));
2023-06-26 00:13:31 -04:00
player.events.on('playerError', (queue, error)=>DZ(error, 'playerError')); // I don't know if both of these actually works, because most
player.events.on('error', (queue, error)=>DZ(error, 'playerInternalError')); // errors from the player is coming from unhandledRejection
2023-06-24 22:17:35 -04:00
}
2023-06-24 13:47:42 -04:00
2023-08-26 10:47:06 -04:00
// YouTube Upload notification and MP loop
2023-08-19 08:50:05 -04:00
if (client.config.botSwitches.mpstats) setInterval(async()=>{
const serverlake = (await client.MPServer._content.findById(client.config.mainServer.id));
2023-09-01 00:32:11 -04:00
for await (const [locName, locArea] of Object.entries(client.config.MPStatsLocation)) await MPModule(client, locArea.channel, locArea.message, serverlake[locName], locName)
2023-08-28 00:22:47 -04:00
}, 35000);
2023-08-30 04:34:59 -04:00
setInterval(async()=>{// Ping notification is currently WIP, it might be active in production but I want to see how it goes with role mentions first so I can make any further changes.
2023-09-25 03:01:48 -04:00
YTModule(client, 'UCQ8k8yTDLITldfWYKDs3xFg', 'Daggerwin', '528967918772551702', '1155760735612305408'); // 528967918772551702 = #videos-and-streams; 1155760735612305408 = YT Upload Ping;
YTModule(client, 'UCguI73--UraJpso4NizXNzA', 'Machinery Restorer', '767444045520961567', '1155760735612305408') // 767444045520961567 = #machinery-restorer; ^^
2023-08-15 06:47:31 -04:00
}, 300000)
2022-11-11 19:58:11 -05:00
// Event loop for punishments and daily msgs
setInterval(async()=>{
2023-03-05 05:04:10 -05:00
const now = Date.now();
2023-05-23 01:14:17 -04:00
2023-09-01 00:32:11 -04:00
const punishments = await client.punishments._content.find();
2023-03-05 05:04:10 -05:00
punishments.filter(x=>x.endTime && x.endTime<= now && !x.expired).forEach(async punishment=>{
console.log(client.logTime(), `${punishment.member}\'s ${punishment.type} should expire now`);
console.log(client.logTime(), await client.punishments.removePunishment(punishment._id, client.user.id, 'Time\'s up!'));
2023-03-05 05:04:10 -05:00
});
2023-05-23 01:14:17 -04:00
const formattedDate = Math.floor((now - client.config.LRSstart)/1000/60/60/24);
2023-08-24 12:06:39 -04:00
const dailyMsgs = JSON.parse(readFileSync('./src/database/dailyMsgs.json', 'utf8'))
2023-08-14 04:33:49 -04:00
if (client.config.botSwitches.dailyMsgsBackup && !dailyMsgs.some((x:Array<number>)=>x[0] === formattedDate)){
2023-03-05 05:04:10 -05:00
let total = (await client.userLevels._content.find({})).reduce((a,b)=>a + b.messages, 0); // sum of all users
const yesterday = dailyMsgs.find((x:Array<number>)=>x[0] === formattedDate - 1);
if (total < yesterday) total = yesterday // messages went down.
dailyMsgs.push([formattedDate, total]);
2023-05-23 01:14:17 -04:00
writeFileSync('./src/database/dailyMsgs.json', JSON.stringify(dailyMsgs))
2023-03-05 05:04:10 -05:00
console.log(client.logTime(), `Pushed [${formattedDate}, ${total}] to dailyMsgs`);
2023-08-13 03:16:39 -04:00
client.guilds.cache.get(client.config.mainServer.id).commands.fetch().then(commands=>(client.channels.resolve(client.config.mainServer.channels.logs) as Discord.TextChannel).send(`:pencil: Pushed \`[${formattedDate}, ${total}]\` to </rank leaderboard:${commands.find(x=>x.name === 'rank').id}>`));
(client.channels.resolve(client.config.mainServer.channels.thismeanswar) as Discord.TextChannel).send({files:['./src/database/dailyMsgs.json']}).catch(fileErr=>console.log(fileErr))
2023-03-05 05:04:10 -05:00
}
2022-11-22 16:29:02 -05:00
}, 5000)