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 toolkit2y 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
SuperPEKKA336OP2y 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 docs2y ago
Suggestion for @superpekka336:guide Popular Topics: Interaction collectors read more
Squid
Squid2y ago
you should use this instead of listening to interactionCreate for the confirmation buttons
SuperPEKKA336
SuperPEKKA336OP2y ago
ah
Squid
Squid2y ago
else, you could store the member's id in a button's custom id
SuperPEKKA336
SuperPEKKA336OP2y 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
Squid2y ago
The custom id must be a string
SuperPEKKA336
SuperPEKKA336OP2y ago
yeah i figured
Squid
Squid2y ago
I suggest that you use a collector
SuperPEKKA336
SuperPEKKA336OP2y ago
ok ty wait i dont get it should i make a new file?
Squid
Squid2y ago
No; create the collector in your command file
SuperPEKKA336
SuperPEKKA336OP2y ago
ah so wait what
Squid
Squid2y ago
The Collector must be created in the file of your command.
SuperPEKKA336
SuperPEKKA336OP2y ago
yes
Squid
Squid2y ago
Glad we're on the same page
SuperPEKKA336
SuperPEKKA336OP2y ago
ur lecturing a gebril where in it? like the beggining?
Squid
Squid2y ago
the collector relies on you to have access to the message containing buttons, first
SuperPEKKA336
SuperPEKKA336OP2y 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
Squid2y ago
you should have enough javascript and critical thinking skills to know which variables need to be defined before/after other variables
SuperPEKKA336
SuperPEKKA336OP2y ago
i mean whats message
Squid
Squid2y ago
The return value of interaction.reply(...) once you resolve the promise
SuperPEKKA336
SuperPEKKA336OP2y ago
ah so i get message after async execute(interaction) right?
Squid
Squid2y ago
this is how it should be defined as
SuperPEKKA336
SuperPEKKA336OP2y ago
await interaction.reply( { embeds: [embed], components: [row], ephemeral: true }); like that
Squid
Squid2y ago
Yes, that function returns an object with a .awaitMessageComponent() method
SuperPEKKA336
SuperPEKKA336OP2y ago
after thats executed? after it sends teh embeds?
Squid
Squid2y ago
Well you can't collect interactions on a message that hasn't been sent yet
SuperPEKKA336
SuperPEKKA336OP2y ago
fair so awaitMessageComponent is returned to interacts.js?
Squid
Squid2y ago
I don't understand what you mean
SuperPEKKA336
SuperPEKKA336OP2y 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
Squid2y 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
SuperPEKKA336OP2y ago
but im trying to get the user to kick also why is tehre a 60 second deffered responce?
Squid
Squid2y ago
Then please show me where you create a new UserSelectMenuBuilder()
SuperPEKKA336
SuperPEKKA336OP2y ago
not a menu its a subcommand
Squid
Squid2y ago
the collector is for collection message component interactions message components are components sent on messages. they are buttons and select menus.
SuperPEKKA336
SuperPEKKA336OP2y ago
Squid
Squid2y 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
SuperPEKKA336OP2y ago
correct
Squid
Squid2y ago
the componentType option when using collectors tells djs which type of component interactions to collect
SuperPEKKA336
SuperPEKKA336OP2y ago
Squid
Squid2y ago
ComponentType.UserSelect is the type for user slsect menus
SuperPEKKA336
SuperPEKKA336OP2y ago
wait no ive done all this already
Squid
Squid2y ago
remind that all of this should be in your command file
SuperPEKKA336
SuperPEKKA336OP2y ago
right now wait what
SuperPEKKA336
SuperPEKKA336OP2y ago
SuperPEKKA336
SuperPEKKA336OP2y ago
shouldnt i have a single file taht handles ALL interactions? *that
Squid
Squid2y ago
this file. your command's file
SuperPEKKA336
SuperPEKKA336OP2y ago
yes
Squid
Squid2y ago
the confirmation buttons are entirely dependent on your command being used.
SuperPEKKA336
SuperPEKKA336OP2y ago
correct
Squid
Squid2y ago
Glad we're on the same page
SuperPEKKA336
SuperPEKKA336OP2y ago
likewise ok new question: How do I get the member selected in the subcommand to the interactions.js file?
Squid
Squid2y 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
SuperPEKKA336OP2y ago
ok so then i use this so i dont need interactions.js at all?
Squid
Squid2y ago
i don't know, look at interactions.js and ask yourself if it's necessary for your bot
SuperPEKKA336
SuperPEKKA336OP2y 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
Squid2y 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
SuperPEKKA336OP2y ago
ah ok bc i have reaction roels in interactions.js
SuperPEKKA336
SuperPEKKA336OP2y ago
SuperPEKKA336
SuperPEKKA336OP2y ago
this correct?
Squid
Squid2y ago
Does it work
SuperPEKKA336
SuperPEKKA336OP2y ago
well no
Squid
Squid2y ago
Then it's incorrect
SuperPEKKA336
SuperPEKKA336OP2y ago
well where do i kick?
Squid
Squid2y ago
The world is your oyster. You can kick wherever you'd like.
SuperPEKKA336
SuperPEKKA336OP2y ago
.... "message isnt defined"
Squid
Squid2y ago
Don't worry, I told you how to define it
SuperPEKKA336
SuperPEKKA336OP2y ago
where
Squid
Squid2y ago
:3
SuperPEKKA336
SuperPEKKA336OP2y ago
Squid
Squid2y ago
The current function chain doesn't resolve the promise that interaction.reply() returns
SuperPEKKA336
SuperPEKKA336OP2y ago
how would i bc yeah how would i resolve it?
SuperPEKKA336
SuperPEKKA336OP2y ago
how do i fix this then?
Shuttle
Shuttle2y 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: [] });
}
Want results from more Discord servers?
Add your server