'How to get the ID of the user who sent the slash command in Discord.js V13
Just had a simple question, I have some moderation slash commands on discord bot running on Discord.js V13, and I need to get the user ID of the person who runs the slash command. So I can then check for the permissions of the user on the guild to make sure he has the Ban_Members permission per example. And also how do I get the guild ID of where the slash command was executed
Thanks! If I wasn't clear feel free to ask me to clarify something
The code I tried
client.ws.on("INTERACTION_CREATE", async (interaction) => {
if (interaction.data.component_type) return;
const command = interaction.data.name.toLowerCase();
const args = interaction.data.options;
if (command == "mute") {
const guildId = interaction.guildId;
console.log(guildId);
// client.api.interactions(interaction.id, interaction.token).callback.post({
// data: {
// type: 4,
// data: {
// embeds: [muteembed],
// ephemeral: "false",
// },
// },
// });
}
});
And I just receive undefined in the console log
Something else I tried
client.on("interactionCreate", async (interaction) => {
if (interaction.data.component_type) return;
const command = interaction.data.name.toLowerCase();
const args = interaction.data.options;
if (command == "mute") {
const guildId = interaction.guildId;
console.log(guildId);
// client.api.interactions(interaction.id, interaction.token).callback.post({
// data: {
// type: 4,
// data: {
// embeds: [muteembed],
// ephemeral: "false",
// },
// },
// });
}
});
And I get this error:
if (interaction.data.component_type) return;
^
TypeError: Cannot read properties of undefined (reading 'component_type')
Solution 1:[1]
its usually
interaction.user.id
interaction.member.id
Solution 2:[2]
The two things you need is a user and a guild. Make sure you check there is a guild, otherwise things will go wrong.
if(!interaction.guild) return; // Returns as there is no guild
var guild = interaction.guild.id;
var userID = interaction.user.id;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DogePlayz |
| Solution 2 | Jeffplays2005 |
