-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
130 lines (109 loc) · 4.88 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const fs = require('node:fs')
const path = require('node:path')
const { Client, Collection, GatewayIntentBits, Routes, REST } = require('discord.js')
const discordAppToken = process.env.discordAppToken,
generalChannelId = process.env.generalChannelId,
botTestingChannelId = process.env.botTestingChannelId,
discordServerId = process.env.discordServerId,
defaultMemberRoleId = process.env.defaultMemberRoleId,
welcomeChannelId = process.env.welcomeChannelId,
suggestionsChannelId = process.env.suggestionsChannelId,
jellybotUserId = process.env.jellybotUserId,
deleteCommands = process.env.deleteCommands,
clearSuggestionsFrequencyHours = process.env.clearSuggestionsFrequencyHours,
clearSuggestionsOlderThanDays = process.env.clearSuggestionsOlderThanDays
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessageReactions,
],
})
const utils = require('./utils.js')
// // Creating a collection for commands in client
client.commands = new Collection()
const commands = []
const foldersPath = path.join(__dirname, 'commands')
const commandFolders = fs.readdirSync(foldersPath)
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder)
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'))
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file)
const command = require(filePath)
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command)
commands.push(command.data.toJSON())
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`)
}
}
}
const guildMemberAdd_Handler = async (member) => {
try {
const userId = member.user.id
const guildObj = client.guilds.cache.get(discordServerId)
const memberObj = await guildObj.members.fetch(userId)
const roleObj = await guildObj.roles.fetch(defaultMemberRoleId)
memberObj.roles.add(roleObj)
client.channels.cache.get(generalChannelId).send(`Welcome to the party, <@${userId}>! You've been assigned the <@&${roleObj.id}> role. Please take a moment and read the <#${welcomeChannelId}> channel. I try to be useful so if you want to know how I can help, hit the / key to see what commands I have available. Be kind and enjoy your stay!`)
} catch (error) {
console.log(error)
}
}
const messageCreate_Handler = async (msg) => {
// Remove messages from Suggestions Channel, only allowing Jellybot to post threads for suggestions. This is to keep the channel clean and organized.
if (msg.channelId === suggestionsChannelId){
if (msg.author.id != jellybotUserId){
msg.delete()
}
}
}
const ready_Handler = async () => {
const CLIENT_ID = client.user.id
const rest = new REST().setToken(discordAppToken)
if (deleteCommands === "true") {
// Delete all commands
await rest.put(Routes.applicationGuildCommands(CLIENT_ID, discordServerId), { body: [] })
.then(() => console.log('Successfully deleted all guild commands.'))
.catch(console.error)
await rest.put(Routes.applicationCommands(CLIENT_ID), { body: [] })
.then(() => console.log('Successfully deleted all global commands.'))
.catch(console.error)
process.exit(1) // Exit application
}
// Register all commands.
const data = await rest.put(Routes.applicationGuildCommands(CLIENT_ID, discordServerId), { body: commands })
.then(() => console.log(`Successfully created application (/) commands.`))
.catch(console.error)
await client.channels.cache.get(botTestingChannelId).send(`I'm here and ready to work!`).then(() => {console.log('Jellybot is online!')}).catch(console.error)
utils.messageCleanup(client, suggestionsChannelId, clearSuggestionsFrequencyHours, clearSuggestionsOlderThanDays)
}
const begin = () => {
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return
const command = client.commands.get(interaction.commandName)
if (!command) return
try {
await command.execute(interaction)
} catch (error) {
if (error) console.error(error)
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true })
}
})
client.on('messageCreate', (msg) => {
messageCreate_Handler(msg)
})
client.on('guildMemberAdd', async (member) => {
guildMemberAdd_Handler(member)
})
client.once('ready', () => {
ready_Handler()
})
}
begin()
// Keep at the end of the file.
client.login(discordAppToken)