-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.ts
132 lines (112 loc) · 3.26 KB
/
index.ts
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
import fs from 'fs';
import { config } from 'dotenv';
import {
Client,
Collection,
Events,
GatewayIntentBits,
Message,
REST,
Routes
} from 'discord.js';
import path from 'path';
config({ path: './.env' });
if (!process.env.DISCORD_TOKEN) {
console.error('Missing env DISCORD_TOKEN');
process.exit(1);
}
const commands = [];
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.MessageContent
]
});
client.commands = new Collection();
// Grab all the command files
const commandsPath = path.join(__dirname, 'commands');
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.warn(
`The command at ${filePath} is missing a required "data" or "execute" property.`
);
}
}
// Construct and prepare an instance of the REST module
const rest = new REST().setToken(process.env.DISCORD_TOKEN);
// and deploy your commands!
(async () => {
try {
console.log(
`Started refreshing ${commands.length} application (/) commands.`
);
if (!process.env.APP_ID) {
console.error('Missing env APP_ID');
process.exit(1);
}
// The put method is used to fully refresh all commands in the guild with the current set
const data = await rest.put(
Routes.applicationCommands(process.env.APP_ID),
{ body: commands }
);
if (data && typeof data === 'object' && 'length' in data) {
console.log(
`Successfully reloaded ${data.length} application (/) commands.`
);
}
} catch (error) {
console.error(error);
}
})();
client.once(Events.ClientReady, () => {
console.log('Client ready');
});
client.on(Events.MessageCreate, async (message: Message) => {
if (message.author.bot) {
return;
}
// Handle `Hello` sent by User
if (message.content === 'Hello') {
const name = message.author.displayName;
await message.channel.send(`Hello ${name}`);
}
});
// Handle slash commands
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) return;
// Check if the command is support by the client
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
// Try to execute the command
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({
content: 'There was an error while executing this command!',
ephemeral: true
});
} else {
await interaction.reply({
content: 'There was an error while executing this command!',
ephemeral: true
});
}
}
});
client.login(process.env.DISCORD_TOKEN);