-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
102 lines (85 loc) · 2.37 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
import { Client, Collection, Events, GatewayIntentBits } from 'discord.js';
import commands from './src/commands/index.js';
import constants from './src/utils/constants.js';
/**
*
* @param {import('discord.js').Interaction} interaction
* @returns
*/
const filterAllowedChannels = (interaction) => {
if (process.env.ENVIRONMENT === 'dev') {
return constants.ALLOWED_CHANNELS.dev === interaction.channelId ? 0 : -1;
}
if (process.env.ENVIRONMENT === 'prod') {
return constants.ALLOWED_CHANNELS.dev === interaction.channelId ? -1 : 0;
}
return 0;
};
const clientOptions = {
allowedMentions: { parse: ['users', 'roles'] },
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
};
if (process.env.ENVIRONMENT === 'dev') {
clientOptions.presence = {
status: 'invisible'
};
}
const client = new Client(clientOptions);
client.commands = new Collection();
Object.entries(commands).forEach((command) => {
command[1].forEach((c) => {
client.commands.set(c.data.name, c);
});
});
client.once('ready', (c) => {
console.log('Bot is ready');
});
client.on(Events.InteractionCreate, async (interaction) => {
try {
if (interaction.isChatInputCommand()) {
const command = interaction.client.commands.get(interaction.commandName);
if (!command) return;
switch (filterAllowedChannels(interaction)) {
case 0:
break;
case 1:
interaction.reply({
content: 'This command is not allowed in this channel',
ephemeral: true
});
return;
case -1:
case -2:
return;
}
try {
await command.execute(interaction, client);
} catch (err) {
console.error(err);
}
} else if (interaction.isButton()) {
if (filterAllowedChannels(interaction) !== 0) return;
const id = interaction.customId.split(':');
const name = id[0];
const command = client.commands.get(name);
if (!command) return;
try {
if (id.length > 1) {
const lobbyId = id[1];
await command.execute(interaction, lobbyId);
} else {
await command.execute(interaction);
}
} catch (err) {
console.error(err);
}
}
} catch (e) {
console.log(e);
}
});
client.login(process.env.DISCORD_TOKEN);