I’m hosting a Discord bot for a small private server of mine with the purpose of pulling a random image from Google Images search results. I’ve been using this repository as the basis, and after fixing the outdated intents in index.js it has been working great. However, this bot only supports slash commands, but I also want a message command (otherwise known as a prefix command) to be an alternative option for users.
Here is the relevant code for performing an image search:
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const { commandName } = interaction;
if (commandName === 'search') {
query = interaction.options.getString('input');
google.search(query)
.then(images => {
const random_idx = Math.floor(Math.random() * images.length);
interaction.reply(images[random_idx].url); // random image from result
});
}
});
Essentially, I want this exact same function to occur if the user does .im query
instead of /search query
. I am very new to JavaScript and I’ve come to realize that slash commands are handled completely differently from message commands, so I have no idea where to even begin. If someone could assist I’d greatly appreciate it. Thank you!
JomSpoons is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.