Kicking a user

if(button === 'kick')
{
const member = interaction.options.getMember('');
member.kick();
}

if(button === 'kick')
{
const member = interaction.options.getMember('');
member.kick();
}

75 Replies
d.js toolkit
d.js toolkit12mo ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
SuperPEKKA336
SuperPEKKA33612mo ago
this is in my interaction.js file how would i get the value of the user chosen in the command? kick.js
const {SlashCommandBuilder, PermissionFlagsBits, ButtonBuilder, ButtonStyle, ActionRowBuilder} = require('discord.js');

const {EMBEDS, KICK} = require('../../constants.json');

module.exports =
{
data: new SlashCommandBuilder()
.setName('kick')
.setDescription('Kicks a user')
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
.addUserOption(option =>
option.setName('user')
.setDescription('User to kick')
.setRequired(true))
.addStringOption(option =>
option.setName('reason')
.setDescription('Reason for kicking the user')
.setRequired(true)),

async execute(interaction)
{
const embed = EMBEDS.KICK;
const member = interaction.options.getMember('user');

embed.description = embed.description.replaceAll("%user", member.id);
embed.description = embed.description.replaceAll("%reason", interaction.options.getString('reason'));

const kick = new ButtonBuilder()
.setCustomId('confirm')
.setLabel('Kick')
.setStyle(ButtonStyle.Danger);

const cancel = new ButtonBuilder()
.setCustomId('cancel')
.setLabel('Cancel')
.setStyle(ButtonStyle.Secondary);

const row = new ActionRowBuilder()
.addComponents(kick)
.addComponents(cancel)

await interaction.reply(
{
embeds: [embed],
components: [row],
ephemeral: true
});
}
};
const {SlashCommandBuilder, PermissionFlagsBits, ButtonBuilder, ButtonStyle, ActionRowBuilder} = require('discord.js');

const {EMBEDS, KICK} = require('../../constants.json');

module.exports =
{
data: new SlashCommandBuilder()
.setName('kick')
.setDescription('Kicks a user')
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
.addUserOption(option =>
option.setName('user')
.setDescription('User to kick')
.setRequired(true))
.addStringOption(option =>
option.setName('reason')
.setDescription('Reason for kicking the user')
.setRequired(true)),

async execute(interaction)
{
const embed = EMBEDS.KICK;
const member = interaction.options.getMember('user');

embed.description = embed.description.replaceAll("%user", member.id);
embed.description = embed.description.replaceAll("%reason", interaction.options.getString('reason'));

const kick = new ButtonBuilder()
.setCustomId('confirm')
.setLabel('Kick')
.setStyle(ButtonStyle.Danger);

const cancel = new ButtonBuilder()
.setCustomId('cancel')
.setLabel('Cancel')
.setStyle(ButtonStyle.Secondary);

const row = new ActionRowBuilder()
.addComponents(kick)
.addComponents(cancel)

await interaction.reply(
{
embeds: [embed],
components: [row],
ephemeral: true
});
}
};
the 'user'
d.js docs
d.js docs12mo ago
Suggestion for @superpekka336:guide Popular Topics: Interaction collectors read more
Squid
Squid12mo ago
you should use this instead of listening to interactionCreate for the confirmation buttons
SuperPEKKA336
SuperPEKKA33612mo ago
ah
Squid
Squid12mo ago
else, you could store the member's id in a button's custom id
SuperPEKKA336
SuperPEKKA33612mo ago
hm so
if(button === 'kick')
{
member.kick(interaction.customId);
}
if(button === 'kick')
{
member.kick(interaction.customId);
}
const kick = new ButtonBuilder()
.setCustomId(member)
.setLabel('Kick')
.setStyle(ButtonStyle.Danger);
const kick = new ButtonBuilder()
.setCustomId(member)
.setLabel('Kick')
.setStyle(ButtonStyle.Danger);
Squid
Squid12mo ago
The custom id must be a string
SuperPEKKA336
SuperPEKKA33612mo ago
yeah i figured
Squid
Squid12mo ago
I suggest that you use a collector
SuperPEKKA336
SuperPEKKA33612mo ago
ok ty wait i dont get it should i make a new file?
Squid
Squid12mo ago
No; create the collector in your command file
SuperPEKKA336
SuperPEKKA33612mo ago
ah so wait what
Squid
Squid12mo ago
The Collector must be created in the file of your command.
SuperPEKKA336
SuperPEKKA33612mo ago
yes
Squid
Squid12mo ago
Glad we're on the same page
SuperPEKKA336
SuperPEKKA33612mo ago
ur lecturing a gebril where in it? like the beggining?
Squid
Squid12mo ago
the collector relies on you to have access to the message containing buttons, first
SuperPEKKA336
SuperPEKKA33612mo ago
k what do i put where
const { ComponentType } = require('discord.js');

const collectorFilter = i =>
{
i.deferUpdate();
return i.user.id === interaction.user.id;
};

message.awaitMessageComponent(
{
filter: collectorFilter,
componentType: ComponentType.StringSelect,
time: 60000
})
.then(interaction => interaction.editReply(`You selected ${interaction.values.join(', ')}!`))
.catch(err => console.log('No interactions were collected.'));
const { ComponentType } = require('discord.js');

const collectorFilter = i =>
{
i.deferUpdate();
return i.user.id === interaction.user.id;
};

message.awaitMessageComponent(
{
filter: collectorFilter,
componentType: ComponentType.StringSelect,
time: 60000
})
.then(interaction => interaction.editReply(`You selected ${interaction.values.join(', ')}!`))
.catch(err => console.log('No interactions were collected.'));
Squid
Squid12mo ago
you should have enough javascript and critical thinking skills to know which variables need to be defined before/after other variables
SuperPEKKA336
SuperPEKKA33612mo ago
i mean whats message
Squid
Squid12mo ago
The return value of interaction.reply(...) once you resolve the promise
SuperPEKKA336
SuperPEKKA33612mo ago
ah so i get message after async execute(interaction) right?
Squid
Squid12mo ago
this is how it should be defined as
SuperPEKKA336
SuperPEKKA33612mo ago
await interaction.reply( { embeds: [embed], components: [row], ephemeral: true }); like that
Squid
Squid12mo ago
Yes, that function returns an object with a .awaitMessageComponent() method
SuperPEKKA336
SuperPEKKA33612mo ago
after thats executed? after it sends teh embeds?
Squid
Squid12mo ago
Well you can't collect interactions on a message that hasn't been sent yet
SuperPEKKA336
SuperPEKKA33612mo ago
fair so awaitMessageComponent is returned to interacts.js?
Squid
Squid12mo ago
I don't understand what you mean
SuperPEKKA336
SuperPEKKA33612mo ago
like nvm so basically i add
message.awaitMessageComponent(
{
filter: collectorFilter,
componentType: ComponentType.UserSelect,
time: 60000
})
.then(interaction => interaction.editReply(`You selected ${interaction.values.join(', ')}!`))
.catch(err => console.log('No interactions were collected.'));
message.awaitMessageComponent(
{
filter: collectorFilter,
componentType: ComponentType.UserSelect,
time: 60000
})
.then(interaction => interaction.editReply(`You selected ${interaction.values.join(', ')}!`))
.catch(err => console.log('No interactions were collected.'));
Squid
Squid12mo ago
componentType: ComponentType.UserSelect
you're collecting buttons, so your component type should be ComponentType.Button which also means interaction.values won't exist
SuperPEKKA336
SuperPEKKA33612mo ago
but im trying to get the user to kick also why is tehre a 60 second deffered responce?
Squid
Squid12mo ago
Then please show me where you create a new UserSelectMenuBuilder()
SuperPEKKA336
SuperPEKKA33612mo ago
not a menu its a subcommand
Squid
Squid12mo ago
the collector is for collection message component interactions message components are components sent on messages. they are buttons and select menus.
SuperPEKKA336
SuperPEKKA33612mo ago
Squid
Squid12mo ago
you don't need to collect which user was selected because that was handled by your slash command (chat input command), which is not a select menu you are only collecting the button that tells you if the command user confirms the kick
SuperPEKKA336
SuperPEKKA33612mo ago
correct
Squid
Squid12mo ago
the componentType option when using collectors tells djs which type of component interactions to collect
SuperPEKKA336
SuperPEKKA33612mo ago
Squid
Squid12mo ago
ComponentType.UserSelect is the type for user slsect menus
SuperPEKKA336
SuperPEKKA33612mo ago
wait no ive done all this already
Squid
Squid12mo ago
remind that all of this should be in your command file
SuperPEKKA336
SuperPEKKA33612mo ago
right now wait what
SuperPEKKA336
SuperPEKKA33612mo ago
SuperPEKKA336
SuperPEKKA33612mo ago
shouldnt i have a single file taht handles ALL interactions? *that
Squid
Squid12mo ago
this file. your command's file
SuperPEKKA336
SuperPEKKA33612mo ago
yes
Squid
Squid12mo ago
the confirmation buttons are entirely dependent on your command being used.
SuperPEKKA336
SuperPEKKA33612mo ago
correct
Squid
Squid12mo ago
Glad we're on the same page
SuperPEKKA336
SuperPEKKA33612mo ago
likewise ok new question: How do I get the member selected in the subcommand to the interactions.js file?
Squid
Squid12mo ago
the entire course of this thread has been me telling you that you don't need to go back-and-forth, and you can handle everything -- the subcommand and the confirmation buttons -- in the command file
SuperPEKKA336
SuperPEKKA33612mo ago
ok so then i use this so i dont need interactions.js at all?
Squid
Squid12mo ago
i don't know, look at interactions.js and ask yourself if it's necessary for your bot
SuperPEKKA336
SuperPEKKA33612mo ago
right now, yes but i should move all the interactions to their respective commands? so each command handles their own interactions? oh a fellow Fortnite enjoyer
Squid
Squid12mo ago
if you send a message component as a response to a command, and if that message component is meant to be used only temporarily (like a simple confirmation button), then I believe that it should be handled in the command's file
SuperPEKKA336
SuperPEKKA33612mo ago
ah ok bc i have reaction roels in interactions.js
SuperPEKKA336
SuperPEKKA33612mo ago
SuperPEKKA336
SuperPEKKA33612mo ago
this correct?
Squid
Squid12mo ago
Does it work
SuperPEKKA336
SuperPEKKA33612mo ago
well no
Squid
Squid12mo ago
Then it's incorrect
SuperPEKKA336
SuperPEKKA33612mo ago
well where do i kick?
Squid
Squid12mo ago
The world is your oyster. You can kick wherever you'd like.
SuperPEKKA336
SuperPEKKA33612mo ago
.... "message isnt defined"
Squid
Squid12mo ago
Don't worry, I told you how to define it
SuperPEKKA336
SuperPEKKA33612mo ago
where
Squid
Squid12mo ago
:3
SuperPEKKA336
SuperPEKKA33612mo ago
Squid
Squid12mo ago
The current function chain doesn't resolve the promise that interaction.reply() returns
SuperPEKKA336
SuperPEKKA33612mo ago
how would i bc yeah how would i resolve it?
SuperPEKKA336
SuperPEKKA33612mo ago
how do i fix this then?
Shuttle
Shuttle12mo ago
just follow the guide https://discordjs.guide/message-components/interactions.html#awaiting-components
const response = await interaction.reply({
content: `Are you sure you want to ban ${target.username} for reason: ${reason}?`,
components: [row],
});

const collectorFilter = i => i.user.id === interaction.user.id;

try {
const confirmation = await response.awaitMessageComponent({ filter: collectorFilter, time: 60000 });
} catch (e) {
await interaction.editReply({ content: 'Confirmation not received within 1 minute, cancelling', components: [] });
}
const response = await interaction.reply({
content: `Are you sure you want to ban ${target.username} for reason: ${reason}?`,
components: [row],
});

const collectorFilter = i => i.user.id === interaction.user.id;

try {
const confirmation = await response.awaitMessageComponent({ filter: collectorFilter, time: 60000 });
} catch (e) {
await interaction.editReply({ content: 'Confirmation not received within 1 minute, cancelling', components: [] });
}