- What's your exact discord.js
npm list discord.jsand nodenode -vversion? - Not a discord.js issue? Check out #1081585952654360687.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!
✅Marked as resolved by OP
#Creating system to add and delete users to a channel
99 messages · Page 1 of 1 (latest)
const users = [];
if (interaction.channel.members.permissionsIn(interaction.channel).has(PermissionFlagsBits.ViewChannel)) {
// users.push(interaction.member)
console.log(interaction.member)
}
so something like this?
No why
interaction.channel.members is only the members with permission to view already
So all of that is redundant
Converting a Collection to an array
You only need to convert it to an array if you need the index of an entry:
- Iteration (looping) is possible with
for...ofoverCollection#valuesorforEach - You can transform a Collection to an array with
Collection#map - If you need indices, you can get the array using
[...collection.values()]
Alright so simple that's the value needed to added on a stringselectmenubuilder?
<channel>.members is a collection
yeh so in this example
interaction.channel.members is a collection with all the members that can see the channel
Loop through it and add it
Yes
So just loop throught interaction.channel.members add it to an array users and then add the values of the users array to a StringSelectMenuBuilder()?
Why are you back at using the <Channel>.members collection? As Said before none of this is needed for your use case. You are just making this overly complicated for no reason
because everyone said it's needed to deleting users from the channel 😂
The issue is different this time Qjuh, they now need all the members with view channel perm to add to String Select
And if I don't know how else to do it, I can't do much but follow what others are saying
Then just <Channel>.members.map(member => ({your option for the SelectMenu}))
That's not what we said it is needed for
just can we build the selectmenu in that map loop?
so simple added the member as value
You can add it to .addOptions() method
Like this?
interaction.members.map(member => {
addOptions(member);
})
The other way around. <SelectMenuBuilder>.addOptions(interaction.channel.members.map(…))
Umm no,
.addOptions(
Collection.map(m => ({
label: m.user.username,
value: m.id
}))
)
But with the interaction.members.map(member =>
above?
Yes
Why interaction.members? You want to get all members that can view the channel, right? Not those that are selected in a UserSelectMenu
Oh wait, yeah, <Channel>.members as Qjuh said
Please don’t confuse the members selected in an interaction with the members of a channel
woops it must be interaction.channel.members
But i got this problem
Also your code was still the wrong way around
Also the code you sent is wrong on so many levels
Collection only refers to interaction.channel.members as it is a collection
interaction.channel.members is your Collection🐌
.addOptions doesn't just work like that
We‘re slowly reaching #rules 3 territory
it needed be like:
.addOptions(
interaction.channel.members.map(m => ({
label: m.user.username,
value: m.id
}))
?
Also you need to map inside .addOptions() method, not put it inside the map, just follow my code snippet
Yes but you need to actually call addOptions on something
If you call that on a StringSelectMenuBuilder, yes
^ just replace Collection with interaction.channel.members here
yeh sorry i read it wrong 😂
Also, do you use TS? In that case, why on earth are you making the interaction parameter any?
ChatInputCommandInteraction @14.15.2
Represents a command interaction.
const row = new ActionRowBuilder().addComponents(
new StringSelectMenuBuilder()
.setCustomId("userinhere")
.addOptions(
interaction.channel.members.map(m => ({
label: m.user.username,
value: m.id
}))
)
)
it's must be builded like this right?
Make the type of interaction this
Judging be their code, it doesn't looks like they use ts or they would be needed to use generics for builders
Probably , try it out
yeh it works
Oh yeah true, but the error does say TS. Is it just a vscode thing?
Yes
Alright everything is working with
if (!interaction.member.permissions.has(PermissionFlagsBits.Administrator)) return interaction.reply({ content: "Je hebt hier geen permissies voor" });
const removeUser = new ActionRowBuilder().addComponents(
new StringSelectMenuBuilder()
.setCustomId("removinguser")
.addOptions(
interaction.channel.members.map(m => ({
label: m.user.username,
value: m.id
}))
)
)
await interaction.reply({
content: `Kies welke gebruiker je wilt verwijderen van het kanaal`, components: [removeUser], ephemeral: true
});
How can i fix that bots doesn't appear in the list of members
they don't appear and you want them to? or they do and you don't want that?
Bots doesn't need to add or remove to the channel so i don't want to see bots in the list
like this
then filter the collection by !member.user.bot
Collection#filter @2.1.0
Identical to Array.filter(), but returns a Collection instead of an Array.
collection.filter(user => user.username === 'Bob');
But where
like first do .addOptions interachion.channel.members.map and then filter out the bots?
You would need to modify it with something like
interaction.channel.members.filter(...).map(...)
and then .filter(member => !member.bot).map(m => ({
label: m.user.username,
value: m.id
}))
!member.user.bot*, otherwise yes
works thx
where needs the filter for the UserSelectMenuBuilder()
const row = new ActionRowBuilder().addComponents(
new UserSelectMenuBuilder()
.setCustomId("addinguser")
)
I thought you were using String Select, where does User Select ckme from now?
userselectmenubuilder is using for adding users to the channel
StringSelectMenuBuilder is using for removing users from the channel
And like i said many times, user select is an auto populating menu, you can't choose to show only certain members, it'll show all
Use String Select for both
haha yeh thats a bit easier
You should also keep in mind that the String Select can only have upto 25 options, so you need to make sure it doesn't exceed that
So if the guild has more than 25 members it doesn't going to work to add members to a channel using StringSelectMenuBuilder()?
Well, you are adding only interaction.channel.members meaning members that can view that channel, so if those members exceed that, yes, it'll error
does the UserSelectMenuBuilder an maximum options?
Yeh i only add the interaction.channel.members on the remove part
not on the adding part
for the adding part i needed all the users that are in the guild
Technically no, it still shows 25 members at once, but theres a search bar that you can use to search for other members
Then use UserSelectMenu in that case, but you can't filter out bots then, best you can do is check if selected user is a bot and reply with error response or something
Alright thanks
Now i got this
const members = (interaction.guild.members.fetch(addingUserID));
That's results in a Promise { guildmember { with all the data how to filter on that for checking if its a bot
Idk why are you fetching the member here, but fetch returns a promise, you need to resolve(await) it
If the addingUserId is supposed to from UserSelectMenuInteraction, you don't need to use the id to fetch them, you can just <Interaction>.members.first() (assuming only one member can be selected at once) and that will give you the Member object
UserSelectMenuInteraction#members @14.15.2
Collection of the selected members
if (interaction.members.first().permissions.has(PermissionFlagsBits.Administrator)) return interaction.reply({ content: <@${removingUserID}> kan niet uit dit kanaal verwijderd worden, ephemeral: true }).then(() => {
setTimeout(() => {
interaction.deleteReply();
}, 5000);
})
why interaction.members.first doesn't work in the removinguser part
Why are you checking if the first member they want to remove is an admin?
cause if you trying to deleting an user out of a ticket channel thats a administrator there must generate a interaction.reply("You can't deleted an administrator out of the channel)
But why only the first selected member? Or is it a single-select menu?
And what exactly doesn’t work about it?
it's single select
TypeError: Cannot read properties of undefined (reading 'first')
at Object.execute (D:\DSModding\DSModding_DevelopmentBot\src\events\ticketButtonListener.js:131:37)
at Client.<anonymous> (D:\DSModding\DSModding_DevelopmentBot\src\functions\handelEvents.js:8:58)
at Client.emit (node:events:530:35)
at InteractionCreateAction.handle (D:\DSModding\DSModding_DevelopmentBot\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12)
at module.exports [as INTERACTION_CREATE] (D:\DSModding\DSModding_DevelopmentBot\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
at WebSocketManager.handlePacket (D:\DSModding\DSModding_DevelopmentBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:355:31)
at WebSocketManager.<anonymous> (D:\DSModding\DSModding_DevelopmentBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:239:12)
at WebSocketManager.emit (D:\DSModding\DSModding_DevelopmentBot\node_modules@vladfrangu\async_event_emitter\dist\index.cjs:282:31)
at WebSocketShard.<anonymous> (D:\DSModding\DSModding_DevelopmentBot\node_modules@discordjs\ws\dist\index.js:1173:51)
at WebSocketShard.emit (D:\DSModding\DSModding_DevelopmentBot\node_modules@vladfrangu\async_event_emitter\dist\index.cjs:282:31)
Emitted 'error' event on Client instance at:
at emitUnhandledRejectionOrErr (node:events:401:10)
at process.processTicksAndRejections (node:internal/process/task_queues:84:21)
Node.js v20.12.2
[nodemon] app crashed - waiting for file changes before starting...
interaction is not a UserSelectMenuInteraction then
Which makes sense seeing this is happening in a ButtonListener
But why its working for a userselectmenubuilder?
and not for a stringselectmenubuilder?
Because a UserSelectMenu sends Users/GuildMembers on interaction. A StringSelectMenu sends strings. Not members
Alright