-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
92 lines (82 loc) · 2.79 KB
/
bot.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
require('dotenv').config()
const { BOT_TOKEN, BOT_PREFIX } = process.env
const { Client, Collection, GatewayIntentBits, ChannelType } = require('discord.js')
const fs = require('fs').promises
const sheets = require('./sheets.js')
// Creates bot client
const bot = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent]
})
// Loads commands into collection
bot.commands = new Collection()
;(async () => {
try {
const files = await fs.readdir('./commands')
const jsfiles = files.filter(f => f.endsWith('js'))
jsfiles.forEach((f, i) => {
const props = require(`./commands/${f}`)
bot.commands.set(props.help.name.toLowerCase(), props)
})
} catch (e) {
return console.log(e)
}
console.log(`[DISCORD] Loaded ${bot.commands.size} commands`)
})()
// Loads slash commands into collection
bot.slashCommands = new Collection()
;(async () => {
try {
const files = await fs.readdir('./slashCommands')
const jsfiles = files.filter(f => f.endsWith('js'))
jsfiles.forEach((f, i) => {
const props = require(`./slashCommands/${f}`)
bot.slashCommands.set(props.info.name.toLowerCase(), props)
})
} catch (e) {
return console.log(e)
}
console.log(`[DISCORD] Loaded ${bot.slashCommands.size} slash commands`)
})()
// Initializes Google Sheets once the bot logs into Discord
bot.once('ready', async () => {
console.info(`[DISCORD] Connected as ${bot.user.username}`)
await sheets.getSheets(bot) // Initial fetching of the Google Sheets
setInterval(async () => {
await sheets.getSheets(bot)
}, 30 * 60_000) // Fetches the Sheets again every 30 minutes
})
// Discord command handler
bot.on('messageCreate', async msg => {
if (msg.author.id === bot.user.id || msg.author.bot) return
console.log(msg.channel.type)
const allowedChannelTypes = [
ChannelType.GuildText,
ChannelType.PublicThread,
ChannelType.PrivateThread
]
if (!allowedChannelTypes.includes(msg.channel.type)) return
if (!msg.content.startsWith(BOT_PREFIX)) return
const prefixless = msg.content
.slice(BOT_PREFIX.length)
.trim()
.split(' ')
const cmd = prefixless[0].toLowerCase()
const args = prefixless.slice(1)
const cmdFile = bot.commands.get(cmd)
if (cmdFile) return cmdFile.run(bot, msg, args)
else {
bot.commands.forEach(c => {
if (c.help.aliases.includes(cmd)) return c.run(bot, msg, args)
})
}
})
// Discord slash command handler
bot.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return
const cmd = interaction.commandName
const cmdFile = bot.slashCommands.get(cmd)
if (cmdFile) return cmdFile.run(bot, interaction)
})
bot.on('error', console.error)
// Log into Discord
bot.login(BOT_TOKEN).catch(e => console.error('[DISCORD] Login failed'))