Skip to content

Instantly share code, notes, and snippets.

@dylanjamesdev
Last active May 26, 2021 21:03
Show Gist options
  • Save dylanjamesdev/98d0abd5ce7d7bd712491c03210ca9cb to your computer and use it in GitHub Desktop.
Save dylanjamesdev/98d0abd5ce7d7bd712491c03210ca9cb to your computer and use it in GitHub Desktop.
I do need some help, I'm sorry to ask this way lmfao but I for the life of me can't get slash commands to work (javascript)
1. Fetch slash commands from files and place in a collection
```const slashFiles = glob.sync("./handlers/slash/*.js");
for (const file of slashFiles) {
const slashcommand = require(file);
client.slashcommands.set(slashcommand.name, slashcommand);
}```
2. Create slash commands via function when bot is ready(guild based for testing)
3. Respond to interactions event
```module.exports = async (client, interaction) => {
const args = interaction.data.options;
const guild = client.guilds.cache.get(interaction.guild_id);
const author = client.users.cache.get(interaction.member.user.id);
const channel = guild.channels.cache.get(interaction.channel_id);
try {
if (client.slashcommands.get(interaction.data.name))
await client.slashcommands.get(interaction.data.name)(
interaction,
client,
args,
guild,
author,
channel
);
} catch (e) {
console.log(e);
}
};```
4. Simple testing ping command
```const Util = require("../../utils/functions/slash");
module.exports = {
name: "ping",
async function(interaction, client, args, guild, author, channel) {
await Util.PostContent(`${author.username}, Pong!`, client, interaction);
await channel
.send("Calculate...")
.then((msg) =>
msg.edit(
`:envelope: Message Latency: ${
Date.now() - msg.createdTimestamp
}ms.\n:bar_chart: API Latency: ${Math.round(client.ws.ping)}ms.`
)
);
}
};```
For some reason, the interaction fails to go through however the command registers and it can most definitely find the command in the collection set. The commands populate but fail to execute.
This is a list of the functions I'm using:
https://bin.tritan.gg/zecogucovi.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment