Discord.Js bot doesn't wait for a response - discord.js

I made a announce command for my Discord.js v12 bot.
What happens when I run my command:
Here is my code:
// title
message.channel.send("What would be the title ?");
const titlecollector = new Discord.MessageCollector(
message.channel,
(m) => m.author.id === message.author.id,
{ time: 60000 }
);
// description
message.channel.send("What would be the description ?");
const descCollector = new Discord.MessageCollector(
message.channel,
(m) => m.author.id === message.author.id,
{ time: 180000 }
);
// ping
message.channel.send("Would I ping everyone, here or none ?");
const pingCollector = new Discord.MessageCollector(
message.channel,
(m) => m.author.id === message.author.id,
{ time: 60000 }
);
// channel
message.channel.send("Which channel should I send it to ?");
const channelCollector = new Discord.MessageCollector(
message.channel,
(m) => m.author.id === message.author.id,
{ time: 60000 }
);
// color
message.channel.send("What color should I use ?");
const colourCollector = new Discord.MessageCollector(
message.channel,
(m) => m.author.id === message.author.id,
{ time: 180000 }
);
// THE ANNONCEMENT
const announcementEmbed = new Discord.MessageEmbed()
.setTitle(collector)
.setDescription(descriptionCollector)
.setFooter(`Sent by ${message.member.user.tag}`);
const channel = Client.channels.cache.find(
(channel) => channel.name === channelCollector
);
if (pingCollector == "everyone") {
channel.send("#everyone");
} else if (pingCollector == "here") {
channel.send("#here");
}
channel.send(announcementEmbed);
I want it to wait for a response and then store that. What is wrong with my code?

You need to await the messages, instead of using a message collector.
//title
message.channel.send("Enter a title").then(() => {
const filter = m => m.author.id == message.author.id //this ensures that it's waiting messages from the person who sent the message
message.channel.awaitMessages(filter, {time: 60000, max: 1, errors: ['time']) //the time variable is the amount of milliseconds it should wait for, change this accordingly
.then(async messages => {
let title = messages.first().content
//description
message.channel.send("Enter a description!").then(() => {
message.channel.awaitMessages(filter, {time: 60000, max: 1, errors: ['time'])
.then(async messages2 => {
let description = messages2.first().content
}).catch(() => {
message.channel.send("You didn't enter anything!")
})
})
}).catch(() => {
message.channel.send("You didn't enter anything!")
})
})
And so on...

Related

discord interaction.deferUpdate() is not a function

I'm creating a message with buttons as reaction roles but do the reaction role handling in another file so it stays loaded after a reset but it either says "interaction.deferUpdate() is not a function, or in discord it says "this interaction failed" but it gave/removed the role
my code for creating the message:
const { ApplicationCommandType, ActionRowBuilder, ButtonBuilder, EmbedBuilder } = require('discord.js');
module.exports = {
name: 'role',
description: "reactionroles",
cooldown: 3000,
userPerms: ['Administrator'],
botPerms: ['Administrator'],
run: async (client, message, args) => {
const getButtons = (toggle = false, choice) => {
const row = new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setLabel('member')
.setCustomId('member')
.setStyle(toggle == true && choice == 'blue' ? 'Secondary' : 'Primary')
.setDisabled(toggle),
new ButtonBuilder()
.setLabel('member2')
.setCustomId('member2')
.setStyle(toggle == true && choice == 'blue' ? 'Secondary' : 'Primary')
.setDisabled(toggle),
);
return row;
}
const embed = new EmbedBuilder()
.setTitle('WΓ€hle eine rolle')
.setDescription('WΓ€hle die rolle, die du gern haben mΓΆchtest')
.setColor('Aqua')
message.channel.send({ embeds: [embed], components: [getButtons()] })
.then((m) => {
const collector = m.createMessageComponentCollector();
collector.on('collect', async (i) => {
if (!i.isButton()) return;
await i.deferUpdate();
});
});
}
};
code for the reaction role:
const fs = require('fs');
const chalk = require('chalk')
var AsciiTable = require('ascii-table')
var table = new AsciiTable()
const discord = require("discord.js");
table.setHeading('Events', 'Stats').setBorder('|', '=', "0", "0")
module.exports = (client) => {
client.ws.on("INTERACTION_CREATE", async (i) => {
let guild = client.guilds.cache.get('934096845762879508');
let member = await guild.members.fetch(i.member.user.id)
let role = guild.roles.cache.find(role => role.name === i.data.custom_id);
if (!member.roles.cache.has(role.id)) {
member.roles.add(role);
} else {
member.roles.remove(role);
}
return i.deferUpdate()
})
};
The client.ws events give raw data (not discord.js objects), so the .deferUpdate() method doesn't exist there. You should be using client.on("interactionCreate")
client.on("interactionCreate", async (i) => {
// ...
return i.deferUpdate()
})

Cant get the reactions to recognise on a specific message now

In a pervious post I was having issues getting the bot to recognise reactions and the fix worked however and then changed it to react on a message that the bot says afterwards and now it isnt working again, I have tried changing the user condition so its the original command author but that didn't seem to work
So you run the code and it makes the embed perfectly and reacts to it however it doesnt recognise when you react and makes the timeout message
exports.run = async (client, message, args) => {
message.delete()
const MINUTES = 5;
const questions = [
{ answer: null, field: 'placeholdquestion' },
{ answer: null, field: 'placeholdquestion' },
{ answer: null, field: 'placeholdquestion' },
]; //to add more questions just add another line of the above code {answes: null, field: `Question Here`}
let current = 0;
const commanduser = message.author.id
// ...
// wait for the message to be sent and grab the returned message
// so we can add the message collector
const sent = await message.author.send(
`${questions[current].field}`,
);
const filter = (response) => response.author.id === message.author.id;
// send in the DM channel where the original question was sent
const collector = sent.channel.createMessageCollector(filter, {
max: questions.length,
time: MINUTES * 60 * 1000,
});
// fires every time a message is collected
collector.on('collect', (message) => {
//if (questions > 1 && questions < 10) {
// add the answer and increase the current index HERE
questions[current++].answer = message.content;
const hasMoreQuestions = current < questions.length; //change to be an imput of how many questions you want asked
if (hasMoreQuestions) {
message.author.send(
`${questions[current].field}`,
);
}
});
// fires when either times out or when reached the limit
collector.on('end', (collected, reason) => {
if (reason === 'time') {
return message.author.send(
`I'm not saying you're slow but you only answered ${collected.size} questions out of ${questions.length} in ${MINUTES} minutes. I gave up.`,
);
}
const embed = new MessageEmbed()
.setTitle("LOA Request")
.addFields(
{ name: 'placehold', value: questions[0].answer+'/10' },
{ name: 'placehold', value: questions[1].answer+'/10' },
{ name: 'placehold', value: questions[2].answer+'/10', inline: true },)
.setColor(`#1773BA`)
.setTimestamp()
.setThumbnail("https://media.discordapp.net/attachments/772915309714735205/795378037813805126/mvg_clean_2.png")
.setFooter("request by: " + message.author.tag);
;
message.channel.send(embed)
.then(function (message) {
message.react("πŸ‘")
message.react("πŸ‘Ž")})
const filter = (reaction, user) => {
return ['πŸ‘', 'πŸ‘Ž'].includes(reaction.emoji.name) && user.id === commanduser; //changed to try and fix it didnt work as message.author.id or this
};
message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] } )
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === 'πŸ‘') {
message.channel.send('you reacted with a thumbs up.');
}
else {
message.reply('you reacted with a thumbs down.');
}
})
.catch(collected => {
console.log(`After a minute, only ${collected.size} out of 4 reacted.`);
message.reply('you didn\'t react with neither a thumbs up, nor a thumbs down.');
});
});
;
}
You have a slight logic error. You need to fit the code from your second filter to the message.awaitReactions inside of your message.channel.send(embed).then(function (message)...) method. In your code, the bot is trying to check for reactions from the original message, which you already deleted (since the awaitReactions is outside the scope of your function where you send and react to the embed).
Like this:
message.channel.send(embed)
.then(function (message) {
message.react("πŸ‘")
message.react("πŸ‘Ž")
const filter2 = (reaction, user) => {
return ['πŸ‘', 'πŸ‘Ž'].includes(reaction.emoji.name) && user.id === commanduser;
};
message.awaitReactions(filter2, { max: 1, time: 60000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === 'πŸ‘') {
message.channel.send('you reacted with a thumbs up.');
}
else {
message.reply('you reacted with a thumbs down.');
}
})
.catch(collected => {
console.log(`After a minute, only ${collected.size} out of 4 reacted.`);
message.reply('you didn\'t react with neither a thumbs up, nor a thumbs down.');
});
})

Quick.db unwarn command unwarns all the warns in a member

I am coding my own discord bot, and making warn system with quick.db package, and having a problem. If I warn a person 2 times, and unwarn him, It removes all the warns of the user. The code is:
//I have imported discord.js and others. This is only the part of warn and unwarn command.
if(command === "warn" ) {
const db = require('quick.db')
const Wuser = message.mentions.users.first();
const member = message.guild.member(Wuser)
if(!message.member.hasPermission("MANAGE_MESSAGES"))
return message.channel.send("You dont have the permission to warn anyone").then(msg => {
msg.delete({ timeout: 10000 })
})
if (!Wuser) return;
if(Wuser.id === message.author.id) return message.channel.send("You cant warn yourself").then(msg => {
msg.delete({ timeout: 10000 })
})
if(Wuser.id === client.user.id) return message.channel.send("You cant warn me").then(msg => {
msg.delete({ timeout: 10000 })
})
db.add(`warn.${Wuser.id}`, 1);
const data = db.get(`warn.${Wuser.id}`);
if(data === undefined ) {
let data = 0
}
message.channel.send(`${Wuser} you are warned. Additional infractions may result in a mute. You have ${data} warns.`)
logchannel.send(`${Wuser} is warned. He have ${data} warns. He is warned by ${message.author}.`)
blogchannel.send(`${Wuser} is warned. He have ${data} warns. He is warned by ${message.author}.`)
}
if(command === "unwarn" ) {
if(!message.member.hasPermission("MANAGE_MESSAGES"))
return message.channel.send("You dont have the permission to unwarn anyone").then(msg => {
msg.delete({ timeout: 10000 })
})
const db = require('quick.db')
let Wuser = message.mentions.users.first();
let member = message.guild.member(Wuser)
if (!Wuser) return;
if(Wuser.id === message.author.id) return message.channel.send("You cant unwarn yourself").then(msg => {
msg.delete({ timeout: 10000 })
})
if(Wuser.id === client.user.id) return message.channel.send("You cant unwarn me").then(msg => {
msg.delete({ timeout: 10000 })
})
db.delete(`warn.${Wuser.id}`)
const data = db.get(`warn.${Wuser.id}`)
message.channel.send(`${Wuser} is unwarned. πŸ‘`)
logchannel.send(`${Wuser} is unwarned by ${message.author}.`)
blogchannel.send(`${Wuser} is unwarned by ${message.author}.`)
}
if(command === "userlog") {
if(!message.member.hasPermission("MANAGE_MESSAGES"))
return message.channel.send("Why are you looking other's user log?").then(msg => {
msg.delete({ timeout: 10000 })
})
let Wuser = message.mentions.users.first()
let member = message.guild.member(Wuser)
if (!Wuser) return message.channel.send("User not specified").then(msg => {
msg.delete({ timeout: 10000 })
})
const db = require('quick.db')
const data = db.get(`warn.${Wuser.id}`)
let logEmbed = new MessageEmbed()
.setTitle(`Log of ${Wuser.tag}`)
.setDescription(`${Wuser} currently have ${data} warns.`)
.setThumbnail(member.user.displayAvatarURL)
message.channel.send(logEmbed)
logchannel.send(`${message.author.tag} used **.userlog** command.`)
}
What I have to edit the code? Thanks in advance.
In the unwarn command
db.delete(`warn.${Wuser.id}`)
Instead of this add to the db
db.add(`warn.${Wuser.id}`, -1);
or subtract from the value
db.subtract(`warn.${Wuser.id}`, 1);

TypeError: Cannot read property 'send' of undefined discord.js

I have this code:
msg.author.send(Finished);
And it isn't working. Finished is a const variable which is a Discord.MessageEmbed. This issue is very annoying and I can't figure out how to fix it. Any help is appreciated.
If you want to see most of the code here it is:
client.on('message', msg => {
if (msg.content === '-na') {
if (msg.channel === Discord.DMChannel) {
return
} else
if (msg.member.roles.cache.has('756713479003701338')) {
msg.author.send(Ping);
msg.author.dmChannel.awaitMessages(m => m.author.id === msg.author.id,
{max: 1, time: 300000}).then(collected => {
msg.author.send(Title);
EmbedPing = collected.first().content;
msg.author.dmChannel.awaitMessages(m => m.author.id === msg.author.id,
{max: 1, time: 300000}).then(collected => {
msg.author.send(Message);
EmbedTitle = collected.first().content;
msg.author.dmChannel.awaitMessages(m => m.author.id === msg.author.id,
{max: 1, time: 300000}).then(collected => {
msg.author.send(Image);
EmbedMessage = collected.first().content;
msg.author.dmChannel.awaitMessages(m => m.author.id === msg.author.id,
{max: 1, time: 300000}).then(collected => {
if (collected.first().content.toLowerCase() === 'yes') {
msg.author.send(ImageURL);
EmbedImage = collected.first().content;
var FinishedEmbedImage = new Discord.MessageEmbed()
.setColor('#FFD700')
.setTitle(EmbedTitle)
.setDescription(EmbedMessage)
.setImage(EmbedImage)
.setTimestamp()
.setFooter('Sent By: ' + msg.author)
allyAnnouncementChannel.send(EmbedPing, FinishedEmbed);
} else
msg.author.send(Finished);
var FinishedEmbed = new Discord.MessageEmbed()
.setColor('#FFD700')
.setTitle(EmbedTitle)
.setDescription(EmbedMessage)
.setTimestamp()
.setFooter('Sent By: ' + msg.author)
allyAnnouncementChannel.send(EmbedPing, FinishedEmbed);
}).catch(Cancelled)
}).catch(Cancelled)
}).catch(Cancelled)
}).catch(Cancelled)
};
};
});
Ping, Title, Message, Image, ImageURL and Finished are all embed variables.
EmbedPing, EmbedTitle, EmbedMessage, and EmbedImage are all empty string variables.
const allyAnnouncementChannel = client.channels.cache.get('756701256122236941')
Discord is obviously the require function.
client is Discord.Client();.

How do I reset message.content in discord.js?

Basically, what I'm trying to do is detect if a message is being sent, reset the content of that message and await a new reply and then check the content of that message for something in specific. The code looks something like this:
if (message.author.id === "318414570161045505") {
message.channel.send(`${message.author}` + " silence");
if (message.content.toUpperCase().includes("no")) {
console.log(message.content)
}
}
I hope I made myself clear on my problem?
You can use message.channel.awaitmessages
if (message.author.id === "318414570161045505") {
message.channel.send(`${message.author}` + " silence");
if (message.content.toUpperCase().includes("no")) {
message.delete()
const filter = m => m.author.id === message.author.id && m.content === '??????'
message.channel
.awaitMessages(filter, {
max: 1,
time: 60000,
errors: ['time'],
})
.then(collected => {
let messageCollected = collected.first()
})
.catch(collected => {
}
}
});

Resources