-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
94 lines (78 loc) · 2.41 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
require('dotenv').config();
const objects = require('./list.js');
const { Client, GatewayIntentBits, REST, Routes, SlashCommandBuilder } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
]
});
const commands = [
new SlashCommandBuilder()
.setName('search')
.setDescription('Search for an object')
.addStringOption(option =>
option.setName('query')
.setDescription('The name to search for')
.setRequired(true))
];
const rest = new REST({ version: '10' }).setToken(process.env.TOKEN);
client.once('ready', async () => {
console.log('Bot is ready!');
// Get the ID of the first guild the bot is in
const guild = client.guilds.cache.first();
if (!guild) {
console.log('Bot is not in any guild');
return;
}
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationGuildCommands(client.user.id, guild.id),
{ body: commands },
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
});
// Function to search objects
function searchObjects(query) {
return objects.filter(x => x.name.toLowerCase().includes(query.toLowerCase()));
}
// Handle message creation event (existing functionality)
client.on("messageCreate", (message) => {
if (message.author.bot) return; // Ignore messages from bots
try {
let res = searchObjects(message.content);
if (res.length > 0) {
if (res[0].URL) {
message.channel.send(res[0].URL);
}
} else {
console.log("No matching object found for message:", message.content);
}
} catch (error) {
console.error(`Error processing message: ${error}`);
}
});
// Handle slash command interactions
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const { commandName } = interaction;
if (commandName === 'search') {
const query = interaction.options.getString('query');
let res = searchObjects(query);
if (res.length > 0) {
if (res[0].URL) {
await interaction.reply(res[0].URL);
} else {
await interaction.reply('Found an object, but it has no URL.');
}
} else {
await interaction.reply('No matching object found.');
}
}
});
client.login(process.env.TOKEN);