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"
}
})
Related
I have been having some problems with my image cooldown script recently. It's intent, when it works, is to let users post images in between a set time limit, in order to prevent spamming. In Discord.JS v12, it was working perfectly. However, after installing v13.6, it does not do its job anymore. Here's the code.
On line 6, there is a line that excludes some channels from the cooldown. The rest of the channels in the server are subject to it though. I would appreciate some help with this code, as it is pretty important.
FYI: All intents have been enabled and listed in the index.js file. Everything else works, but not this script.
Here's the raw paste code, if you want to test the code out:
const talkedRecently = new Set();
client.on('messageCreate', async message => {
if (!message.guild) return;
if (message.author.bot) return;
if (!message.channel.type === 'dm') return;
if (!message.channel.id === 'channel1' || 'channel2' || 'channel3' || 'channel4' || 'channel5' || 'channel6' || 'channel7' || 'channel8') {
if (message.attachments.size > 0) {
if (talkedRecently.has(message.author.id)) {
setTimeout(()=> message.delete(), 5000).catch(err => console.log(err))
message.channel.send("You must wait 30 seconds before sending another attachment - " + message.author.username).then(msg => {
setTimeout(() => message.delete(), 10000)
}).catch(/*Your Error handling if the Message isn't returned, sent, etc.*/)
}
}
}
});
There are 5 issues with this code.
You are strict-comparing boolean to a string, which of course... will always return false
// DON'T DO THIS
if (!thing === 'one') {
// DO THIS
if (thing !== 'one') {
That's not how we compare multiple values in JS
// DON'T DO THIS
if (thing === "one" || "two" || "three")
// DO THIS
if ([ "one", "two", "three" ].includes(thing)) {
The channel IDs must be changed, there are no channels IDs such as channelN
You are deleting the same message 2 times
// Right here
setTimeout(()=> message.delete(), 5000).catch(err => console.log(err))
// ...
// And here
.then(msg => { setTimeout(() => message.delete(), 10000) }).catch(/*Your Error handling if the Message isn't returned, sent, etc.*/)
Your code is outdated and requires an update
See the update guide here.
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.
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 would like to know if it is possible to do some sort of "SlowMode" for a specific person on Discord.
The reason is that I have a "spammer" friend, and I would like to calm him down with a command that might slow him down when he speaks for "x" secondes.
So I would like to know if this is possible? and if yes, how?
Thank you for your kindness =) (and sorry for this english i use GoogleTraductor)
Here's how I'd do that.
let ratelimits = [];
client.on("message", (msg) => {
// APPLYING RATELIMITS
const appliedRatelimit = ratelimits.find(
(value) =>
value.user === msg.author.id && value.channel === msg.channel.id
);
if (appliedRatelimit) {
// Can they post the message?
const canPostMessage =
msg.createdAt.getTime() - appliedRatelimit.ratelimit >=
appliedRatelimit.lastMessage;
// They can
if (canPostMessage)
return (ratelimits[
ratelimits.indexOf(appliedRatelimit)
].lastMessage = msg.createdAt.getTime());
// They can't
msg.delete({ reason: "Enforcing ratelimit." });
}
// SET RATELIMIT
if (msg.content === "!ratelimit") {
// Checking it's you
if (msg.author.id !== "your id") return msg.reply("You can't do that.");
// You can change these values in function of the received message
const targetedUserId = "whatever id you want";
const targetedChannelId = msg.channel.id;
const msRateLimit = 2000; // 2 seconds
// Delete existant ratelimit if any for this user on this channel
ratelimits = ratelimits.filter(
(value) =>
!(
value.user === targetedUserId &&
value.channel === targetedChannelId
)
);
// Add ratelimit
ratelimits.push({
user: targetedUserId,
channel: targetedChannelId,
ratelimit: msRateLimit,
lastMessage: 0,
});
}
// CLEAR RATELIMITS
if (msg.content === "!clearRatelimits") {
// Checking it's you
if (msg.author.id !== "your id") return msg.reply("You can't do that.");
// Clearing all ratelimits
ratelimits = [];
}
});
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.