forked from muncheruniverse/munch-cord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
157 lines (138 loc) · 5.48 KB
/
index.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const fs = require('node:fs')
const path = require('node:path')
require('dotenv-flow').config()
const sequelize = require('./db/db-connect')
const { Client, Collection, Events, GatewayIntentBits, ActivityType } = require('discord.js')
// Import required model files
const ManageChannels = require('./db/manage-channels')
const UserInscriptions = require('./db/user-inscriptions')
const BipMessages = require('./db/bip-messages')
const Brc20s = require('./db/brc20s')
const UserBrc20s = require('./db/user-brc20s')
const { UserAddresses } = require('./db/user-addresses')
const { Collections, Inscriptions } = require('./db/collections-inscriptions')
// Import required modal interactions
const addCollectionModal = require('./modal/add-collection')
const verifyNft = require('./modal/verify-nft')
// Import required selector interactions
const roleSelector = require('./selector/role-selector')
const verifySelector = require('./selector/verify-selector')
const removeCollectionSelector = require('./selector/remove-collection-selector')
const removeBrc20Selector = require('./selector/remove-brc20-selector')
// Import required button action interactions
const verify = require('./button/verify')
// Import required api service
const apiService = require('./api/api-service')
// Create a new client
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers],
})
// Create a new map to store all of the bot's commands
client.commands = new Collection()
// Find all files in the commands directory that end in .js
const commandsPath = path.join(__dirname, 'commands')
const commandFilePaths = []
fs.readdirSync(commandsPath).forEach((dirName) => {
fs.readdirSync(path.join(commandsPath, dirName)).forEach((file) => {
if (file.endsWith('.js') && !file.endsWith('.test.js')) {
commandFilePaths.push(path.join(commandsPath, dirName, file))
}
})
})
// Loop through each command file and add it to the bot's commands map
for (const filePath of commandFilePaths) {
const command = require(filePath)
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command)
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`)
}
}
client.on(Events.InteractionCreate, async (interaction) => {
try {
if (interaction.isModalSubmit()) {
// Modal interactions
if (interaction.customId === addCollectionModal.data) {
await addCollectionModal.execute(interaction)
} else if (interaction.customId === verifyNft.data) {
await verifyNft.execute(interaction)
}
} else if (interaction.isStringSelectMenu()) {
// Selector interactions
if (interaction.customId === roleSelector.data) {
roleSelector.execute(interaction)
} else if (interaction.customId === removeCollectionSelector.data) {
removeCollectionSelector.execute(interaction)
} else if (interaction.customId === removeBrc20Selector.data) {
removeBrc20Selector.execute(interaction)
} else if (interaction.customId === verifySelector.data) {
verifySelector.execute(interaction)
}
} else if (interaction.isChatInputCommand()) {
// Slash command interactions
const command = interaction.client.commands.get(interaction.commandName)
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`)
return
}
await command.execute(interaction)
} else if (interaction.isButton()) {
// Button interactions
if (interaction.customId === 'verifyNFT') await verify.execute(interaction)
}
} catch (error) {
if (error.code === 10062 || error.code === 40060) {
console.warn('Interaction has already been acknowledged. Are multiple bots running using the same app/token?')
} else {
console.error(error)
if (interaction.replied || interaction.deferred) {
await interaction.followUp({
content: 'There was an error while executing this interaction!',
ephemeral: true,
})
} else {
await interaction.reply({
content: 'There was an error while executing this interaction!',
ephemeral: true,
})
}
}
}
})
// Once the client is ready, perform initial setup and output a message indicating that the client is ready
client.once(Events.ClientReady, (client) => {
// Connect to the database
try {
sequelize.authenticate()
console.log('Database connection has been established successfully.')
} catch (error) {
console.error('Unable to connect to the database:', error)
}
// Sync all database tables
Collections.sync().then(() => {
Inscriptions.sync().then(() => {
UserAddresses.sync().then(() => {
UserInscriptions.sync()
Brc20s.sync().then(() => {
UserBrc20s.sync()
})
})
})
})
BipMessages.sync()
ManageChannels.sync()
// Api Service for health and verify
const app = apiService(client)
const port = process.env.PORT || 3000
// Set the server to listen for requests
app.listen(port, () => {
console.log(`Server is running on port ${port}`)
})
// Activity status for discord
client.user.setActivity('Monster Mash 👹', { type: ActivityType.Listening })
// Output a message indicating that the client is ready
console.log(`Ready! Logged in as ${client.user.tag}`)
})
// Log in to Discord with the bot token specified in the .env file
client.login(process.env.TOKEN)
module.exports = client