-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
92 lines (76 loc) · 2.24 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import LemmyBot from 'lemmy-bot'
import { LogCategory, log } from './src/common/log.js'
import 'dotenv/config'
import * as path from 'path'
import { readdirSync } from 'fs'
import { fileURLToPath } from 'url'
import { dirname } from 'path'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
log('LOADING', 'Loading Bot', LogCategory.SUCCESS)
// -----------------------------------------------------------------------------
// Load Handlers
const handlers = {}
const handlersPath = path.join(__dirname, 'src/handlers')
const handlerFiles = readdirSync(handlersPath).filter((file) =>
file.endsWith('.js')
)
for (const file of handlerFiles) {
const filePath = path.join(handlersPath, file)
const { default: command } = await import('file://' + filePath)
handlers[file.split('.').shift()] = command
}
log(
'LOADED',
`Loaded ${handlerFiles.length} handlers${
handlerFiles.length != 1 ? 's' : ''
}`,
LogCategory.SUCCESS
)
// -----------------------------------------------------------------------------
// Load Schedules
const schedules = []
const schedulesPath = path.join(__dirname, 'src/schedules')
const scheduleFiles = readdirSync(schedulesPath).filter((file) =>
file.endsWith('.js')
)
for (const file of scheduleFiles) {
const filePath = path.join(schedulesPath, file)
const { default: command } = await import('file://' + filePath)
schedules.push(command)
}
log(
'LOADED',
`Loaded ${scheduleFiles.length} schedule${
scheduleFiles.length != 1 ? 's' : ''
}`,
LogCategory.SUCCESS
)
// -----------------------------------------------------------------------------
// Main Bot Code
if (
process.env.LEMMY_INSTANCE &&
process.env.LEMMY_USERNAME &&
process.env.LEMMY_PASSWORD
) {
const bot = new LemmyBot.LemmyBot({
instance: process.env.LEMMY_INSTANCE,
credentials: {
username: process.env.LEMMY_USERNAME,
password: process.env.LEMMY_PASSWORD,
},
dbFile: 'db.sqlite3',
federation: 'all',
markAsBot: true,
handlers: handlers,
schedule: schedules,
})
bot.start()
log('STARTED', 'Started Bot', LogCategory.SUCCESS)
} else {
log(
'COULD NOT START',
'Your environment file is missing or not formatted correctly.',
LogCategory.DANGER
)
}