Grabbing ID of the user that reacted to a message - discord

I got this part of my code:
message.awaitReactions((reaction, user) => user.id != message.author.id && (reaction.emoji.name == 'βœ…' || reaction.emoji.name == 'πŸ’£'),
{ max: 1, time: 2147483647 }).then(collected => {
if (collected.first().emoji.name == 'βœ…') {
message.delete()
client.channels.cache.get("700489735352746045").send(BufferClear);
What it does: bot sends an embed, then if user reacts with βœ… it sends BufferClear, question is, how do I get the ID of the user that reacted to the message?

In awaitReactions() you have 2 parameters, one of which is user, so you can use user.id to get that user's ID.

Related

How do i keep taking inputs from multiple users until a specific user says a particular keyword (discord.js)?

So I've been trying to make an auction bot on discord. I've figured out how to handle a single item, but I'm having issues regarding bundles.
My goal - Let the user info any/all items when auctioning a bundle till he says the keyword 'done'. The bot stores the url of the first message (embed sent by the bot) and then does nothing till the user says done. The two users are 1. the user himself, 2. the bot who's item(s) he wants to auction.
I tried using the message collector but the bot straight up ignores it. After searching around on the web and trying to adapt to my situation, the closest thing i found is using async/await with awaitMessages while inside a for loop. After that failed too, i tried to use a chain of then() with the awaitMessages. Here's my code snippet. Any help is appreciated.
let flg =0
if(arguments[3] === 'bundle'){
message.channel.send('Say `done` when you have info-ed all your pokemons')
await message.channel.awaitMessages(m => m.author.id === '666956518511345684' || m.author.id === message.author.id, {time : 30000, errors : ['time']}).then(col1 => {
if (col1.first().author.id === '666956518511345684')
details.poke = col1.first().url
}).then(col2 => {
if (col2.first().author.id === message.author.id)
while(flg = 0){
if (col2.first().content.toLowerCase() === 'done')
flg = 1
}
})
Have the collector ignore bot messages, take multiple inputs and check for message content. Once time runs out, or the max input has been reached, the collection of messages will be returned.
if(arguments[3] === 'bundle'){
message.channel.send('Say `done` when you have info-ed all your pokemons')
const filter = m => {
return !m.user.bot && ['666956518511345684', message.author.id].includes(m.author.id) && m.content.toLowerCase() === 'done';
}
message.channel.awaitMessages(filter, {
// Take a max of 5 inputs for example
max: 5
time: 30000,
errors: ['time']
}).then(collected => {
// Your code
})
}
Documentation on TextChannel#awaitMessages()
I tried message collector once again and i made it work somehow. Here's how :
let i = 0
let filter = m => {m.author.id === '<Bot's id>' || m.author.id === message.author.id}
let collector = message.channel.createMessageCollector(filter, {time : 30000})
collector.on('collect', m => {
if (m.author.id === '<bot's id>' && i === 0){
details.poke = m.url
i = 1
}
if (m.author.id === message.author.id && m.content.toLowerCase() === 'done'){
collector.stop()
}
})
collector.on('end', col => { //code here })
The variable i is a flag, since i only want the url of the first message sent by the bot. It can be removed/more flags can be added up to your needs/requirements.
Lemme know if you need anymore help regarding this.

How to ban with Discord.js?

I have created a command to ban a user and I want it to prompt the user for a confirmation on whether or not they would like to ban the user, and then once they confirm, execute the ban.
I've tried to log where the problem is, but it gives me a blank error.
My code for the command:
const Discord = require('discord.js')
let target = arguments.shift()
let reason1 = arguments.join(" ");
const reason = reason1 || "Ohne Grund"
const banEmbed = new Discord.MessageEmbed()
.setTitle('🚫Ban')
.setDescription(`Bist du dir sicher, dass ${message.mentions.users.first()} gebannt werden soll?`)
.setColor('RED')
.addFields(
{ name: 'Reason', value: `${reason}`}
)
.setTimestamp()
.setFooter(message.guild.name, message.guild.iconURL())
message.channel.send(banEmbed).then(sentEmbed =>{
sentEmbed.react("βœ…")
sentEmbed.react("❌")
sentEmbed.awaitReactions((reaction, user) => user.id == message.author.id && (reaction.emoji.name == 'βœ…' || reaction.emoji.name == '❌'),
{ max: 1, time: 30000 }).then(collected => {
console.log(collected.first().emoji.name)
if (collected.first().emoji.name == 'βœ…') {
console.log(target)
console.log(reason)
target.ban({ days: 0, reason: reason })
message.reply('Member wird gebannt.');
}
if (collected.first().emoji.name == '❌') {
message.reply('Member wird nicht gebannt.');
}
}).catch(() => {
message.reply('timeout');
console.error();
});
});
and this is the part where it fails, everything else is okay:
sentEmbed.awaitReactions((reaction, user) => user.id == message.author.id && (reaction.emoji.name == 'βœ…' || reaction.emoji.name == '❌'),
{ max: 1, time: 30000 }).then(collected => {
console.log(collected.first().emoji.name)
if (collected.first().emoji.name == 'βœ…') {
console.log(target)
console.log(reason)
target.ban({ reason: reason })
message.reply('Member wird gebannt.');
}
Console Output:
The client is ready!
βœ…
<#!664493064336965634>
test
Please help!
What you are doing here won't work. Why? You are getting the first element of the array which is a string even if you mention someone it will look like <#4242424242424> which doesn't have the method .ban
let target = arguments.shift()
You should find that member in the guild either you can get it by the mentions property
let target = message.mentions.members.first() || message.guild.members.cache.get(arguments.shift());

How do I make a message collector in discord.js to have multiple responses according to the message the user sent?

I'm trying to make a start command for a currency bot. I'm trying to make a message collector, which asks for collecting for this certain user. And depending on the user's response, it would have different responses.
If the user replies with "yes", the bot will send "cool".
If the user replies with "no", the bot will send "not cool".
If the user doesn't reply in 15 seconds or replies something else than "yes" and "no", the bot will send "bruh".
message.channel.send("Yes or no?")
.then(function (message) {
const filter = m => m.author.id === message.author.id,;
const collector = message.channel.createMessageCollector(filter, { max: 1, time: 15000, errors: ['time'] })
collector.on('collect', m => {
if (m == "yes") {
message.channel.send("cool")
} else if (m == "no") {
message.channel.send("not cool")
} else {
message.channel.send("bruh")
}
})
})
}
But whatever I say, the bot doesn't respond at all, even in the console. Thanks for any help!
The message parameter of the MessageCollector returns a message object. What you are currently attempting to do is comparing an object (The parameter m that represents the object) with a string - Something that can obviously not be done.
If you would like to compare the collected message to a certain string, you would have to compare its content property, that obviously returns the content of the collected message.
Final Code:
message.channel.send("Yes or no?")
.then(function (message) {
const filter = m => m.author.id === message.author.id,;
const collector = message.channel.createMessageCollector(filter, { max: 1, time: 15000, errors: ['time'] })
collector.on('collect', m => {
if (m.content == "yes") {
message.channel.send("cool")
} else if (m.content == "no") {
message.channel.send("not cool")
} else {
message.channel.send("bruh")
}
})
})
}

I'm trying to make a second input into a bot

I'm trying to make a command where you input the command and the bot says: Are you sure? Then you type yes or no but I can't figure out how I can make it so the user can reply. Can someone help please?
have some ways to do that, the easier is to use MessageCollector to collect the user response.
Example:
message.channel.send("Are you sure?") // Ask the user
const filter = (m) => m.author.id === message.author.id && (m.content.toLowerCase() === "yes" || m.content.toLowerCase() === "no") // Create a filter, only accept messages from the user that used the command and the message includes "yes" or "no"
const collector = message.channel.createMessageCollector(filter, {time: 30000})
collector.once("collect", msg => {
if(msg.content.toLowerCase() === "yes") {
// User sent yes
} else {
// User sent "no"
}
})
collector.once("stop", (collected, reason) => {
if(reason === "time") {
// User took so long to anwser
}
})
You can use TextChannel.awaitMessages too, it returns a Promise with messages.
Example:
message.channel.send("Are you sure?")
const filter = (m) => m.author.id === message.author.id && (m.content.toLowerCase() === "yes" || m.content.toLowerCase() === "no") // Create a filter, only accept messages from the user that used the command and the message includes "yes" or "no"
message.channel.awaitMessages(filter, {max: 1, time: 30000})
.then(collected => {
const msg = collected.first()
if(msg.content.toLowerCase() === "yes") {
// User sent yes
} else {
// User sent "no"
}
})

Creating channel and setChannel troubles

I'm having trouble creating a discord bot, what it should do is detect whenever someone jkoins a specific voice Chat, and if a user does, the bot would have to create a new channel with, as the name, the nickname of the user who joined, then move that user to the new created channel and set that channel to private so no one can join.
Currently my problems are:
-I can't set the name of the new channel as the nickname of the user
-I can't move the user to that channel
-And I think the rest may work
Here is the part of my code:
client.on('voiceStateUpdate', (oldMember, newMember) => {
if(newMember.channelID != '693086073244483614') return;
const nick = newMember.nickname;
newMember.guild.channels.create('test', { type: 'voice' })
.then(() => {
newMember.setChannel('test');
console.log(`Hello from ${newMember.channel}!`);
const nChannel = newMember.channel;
nChannel.setParent('690292158636360032');
nChannel.overwritePermissions([
{
parent: '#690292158636360032',
id: '532236212967047169',
deny: ['CONNECT'],
},
]);
});
});
Any help would be appriciated, I'm new to both discord bots and javascript, so thanks a lot!
The Client#voiceStateUpdate event does not return a member, it returns a VoiceState. https://discord.js.org/#/docs/main/stable/class/Client?scrollTo=e-voiceStateUpdate
From the VoiceState, you can get a member with <VoiceState>.member.
So to fix your error, replace const nick = newMember.nickname to const nick = newMember.member.nickname.
client.on('voiceStateUpdate', (oldMember, newMember) => {
var oldUserChannel = oldMember.voiceChannel;
var newUserChannel = newMember.voiceChannel;
if(oldUserChannel === undefined && newUserChannel !== undefined) { //User joined the channel.
if(newMember.voiceChannel.id != '693086073244483614') return; //Check if the user join the right voice channel.
let nick = newMember.nickname;
if(nick == null){
nick = newMember.user.username; //If the member doesn't have a nickname.
}
newMember.guild.createChannel(nick, { type: 'voice' })
.then((nChannel) => {
console.log(`Hello from ${nChannel}!`);
nChannel.setParent('690292158636360032'); //Your category ID
nChannel.overwritePermissions(nChannel.guild.defaultRole.id, {
VIEW_CHANNEL: false
});
newMember.setVoiceChannel(nChannel)
});
} else if(newUserChannel === undefined){ //User left the channel.
if(oldMember.voiceChannel.parent == null) return;
if(oldMember.voiceChannel.parent.id != '690292158636360032') return; //Check if the voice channel is under the specified category.
if(oldMember.voiceChannel.members.first() == null){ //There is no more users in this channel.
oldMember.voiceChannel.delete(); //Delete the channel.
}
}
});
Here is the final result, fully working. I also add a system to automatically remove the channel if there is nobody in.
I have added some annotations, tell me if you have any questions.

Resources