11// Check if all the required environment variables are set
2- import { env } from ' ./config.ts' ;
2+ import { env } from " ./config.ts" ;
33
4- if ( ! env . discordToken || env . discordToken === ' YOUR_DISCORD_TOKEN' ) {
5- throw new Error ( ' You MUST provide a discord token in .env!' ) ;
4+ if ( ! env . discordToken || env . discordToken === " YOUR_DISCORD_TOKEN" ) {
5+ throw new Error ( " You MUST provide a discord token in .env!" ) ;
66}
77
8- if ( ! env . youtubeApiKey || env . youtubeApiKey === ' YOUR_YOUTUBE_API_KEY' ) {
9- throw new Error ( ' You MUST provide a YouTube API key in .env!' ) ;
8+ if ( ! env . youtubeApiKey || env . youtubeApiKey === " YOUR_YOUTUBE_API_KEY" ) {
9+ throw new Error ( " You MUST provide a YouTube API key in .env!" ) ;
1010}
1111
12- if ( ! env . mysqlAddress || env . mysqlAddress === 'YOUR_MYSQL_SERVER_ADDRESS' ) {
13- throw new Error ( ' You MUST provide a MySQL server address in .env!' ) ;
12+ if ( ! env . twitchClientId || env . twitchClientId === "YOUR_TWITCH_CLIENT_ID" ) {
13+ throw new Error ( " You MUST provide a Twitch client ID in .env!" ) ;
1414}
1515
16- if ( ! env . mysqlPort || env . mysqlPort === 'YOUR_MYSQL_SERVER_PORT' ) {
17- throw new Error ( 'You MUST provide a MySQL server port in .env!' ) ;
16+ if (
17+ ! env . twitchClientSecret ||
18+ env . twitchClientSecret === "YOUR_TWITCH_CLIENT_SECRET"
19+ ) {
20+ throw new Error ( "You MUST provide a Twitch client secret in .env!" ) ;
1821}
1922
20- if ( ! env . mysqlUser || env . mysqlUser === 'YOUR_MYSQL_USER' ) {
21- throw new Error ( 'You MUST provide a MySQL user in .env!' ) ;
22- }
23+ // If everything is set up correctly, continue with the bot
24+ import {
25+ Client ,
26+ GatewayIntentBits ,
27+ REST ,
28+ Routes ,
29+ type APIApplicationCommand ,
30+ } from "discord.js" ;
2331
24- if ( ! env . mysqlPassword || env . mysqlPassword === 'YOUR_MYSQL_PASSWORD' ) {
25- throw new Error ( 'You MUST provide a MySQL password in .env!' ) ;
26- }
32+ import commandsMap from "./commands.ts" ;
2733
28- if ( ! env . mysqlDatabase || env . mysqlDatabase === 'YOUR_DATABASE_NAME' ) {
29- throw new Error ( 'You MUST provide a database name in .env!' ) ;
30- }
34+ import fs from "fs/promises" ;
35+ import path from "path" ;
3136
32- if ( ! env . twitchClientId || env . twitchClientId === 'YOUR_TWITCH_CLIENT_ID' ) {
33- throw new Error ( 'You MUST provide a Twitch client ID in .env!' ) ;
34- }
37+ import { initTables } from "./utils/database.ts" ;
38+ import { getTwitchToken } from "./utils/twitch/auth.ts" ;
3539
36- if ( ! env . twitchClientSecret || env . twitchClientSecret === 'YOUR_TWITCH_CLIENT_SECRET' ) {
37- throw new Error ( 'You MUST provide a Twitch client secret in .env!' ) ;
38- }
40+ import { CronJob } from "cron" ;
3941
40- // Import API
41- import './api.ts'
42+ import backup from "./utils/backup.ts" ;
4243
43- // If everything is set up correctly, continue with the bot
44- import { Client , GatewayIntentBits , REST , Routes , type APIApplicationCommand } from 'discord.js' ;
45- import commandsMap from './commands.ts' ;
46- import fs from 'fs/promises' ;
47- import path from 'path' ;
48- import { initTables } from './utils/database.ts' ;
49- import { getTwitchToken } from './utils/twitch/auth.ts' ;
44+ // Start the cron jobs
45+ await fs . mkdir ( path . resolve ( process . cwd ( ) , "backups" ) , { recursive : true } ) ;
46+ new CronJob ( "0 0 * * *" , async ( ) => {
47+ await backup (
48+ path . resolve ( process . cwd ( ) , "db.sqlite3" ) ,
49+ `./backups/db-${ new Date ( ) . toISOString ( ) . replace ( / [: .] / g, "-" ) } .sqlite3` ,
50+ ) ;
51+ } ) . start ( ) ;
5052
5153const client = new Client ( {
52- intents : [
53- GatewayIntentBits . Guilds ,
54- GatewayIntentBits . GuildMessages ,
55- ]
54+ intents : [ GatewayIntentBits . Guilds , GatewayIntentBits . GuildMessages ] ,
5655} ) ;
5756
5857// Update the commands
@@ -61,6 +60,7 @@ const rest = new REST().setToken(env.discordToken);
6160const getAppId : { id ?: string | null } = ( await rest . get (
6261 Routes . currentApplication ( ) ,
6362) ) || { id : null } ;
63+
6464if ( ! getAppId ?. id )
6565 throw "No application ID was able to be found with this token" ;
6666
@@ -73,22 +73,25 @@ const data = (await rest.put(Routes.applicationCommands(getAppId.id), {
7373console . log ( `Successfully reloaded ${ data . length } application (/) commands.` ) ;
7474
7575// Check if MySQL is set up properly and its working
76- if ( ! await initTables ( ) ) {
77- throw new Error ( ' Error initializing tables' ) ;
76+ if ( ! ( await initTables ( ) ) ) {
77+ throw new Error ( " Error initializing tables" ) ;
7878}
7979
8080// Get Twitch token
81- if ( ! await getTwitchToken ( ) ) {
82- throw new Error ( ' Error getting Twitch token' ) ;
81+ if ( ! ( await getTwitchToken ( ) ) ) {
82+ throw new Error ( " Error getting Twitch token" ) ;
8383}
8484
8585// Login to Discord
8686client . login ( env . discordToken ) ;
8787
88- export default client
88+ export default client ;
8989
9090// Import events
91- const getEvents = await fs . readdir ( path . resolve ( __dirname , './events' ) ) ;
92- await Promise . all ( getEvents . map ( async ( file ) => {
93- await import ( './events/' + file ) ;
94- } ) ) ;
91+ const getEvents = await fs . readdir ( path . resolve ( __dirname , "./events" ) ) ;
92+
93+ await Promise . all (
94+ getEvents . map ( async ( file ) => {
95+ await import ( "./events/" + file ) ;
96+ } ) ,
97+ ) ;
0 commit comments