How do I reset message.content in discord.js? - 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 => {
}
}
});

Related

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.');
});
})

I want to make an eval command in discord.js

I'm trying to use this to create an eval command:
const testcode = "hello";
if (command === "run" || command === "eval") {
if (message.author.id !== "744752301033521233" && message.author.id !== "716343156513439845")
return message.channel.send({embed: {
title: "✍️RUN",
description: "❌You're not bot owner"
}});
try {
const code = args.join(" ");
let evaled = eval(code);
if (typeof evaled !== "string") {
//console.log(evaled);
evaled = await require("util").inspect(evaled);
//console.log(evaled);
message.channel.send(`\`\`\`xl\n${evaled}\`\`\``);
} else {
message.channel.send(`\`\`\`xl\n${evaled}\`\`\``);
}
} catch (err) {
message.channel.send({
embed: {
title: "✍️RUN",
description: "Error:\n```xl\n" + err + "```",
color: 961818,
timestamp: new Date(),
footer: {
icon_url: client.user.avatarURL
}
}
});
}
}
This code works correctly for me. (For example, !run testcode.length will return 5)
but, this code returns an error if it returns more than 4096 characters.
So I used the split option:
const testcode = "hello";
if (command === "run" || command === "eval") {
if (message.author.id !== "744752301033521233" && message.author.id !== "716343156513439845")
return message.channel.send({embed: {
title: "✍️RUN",
description: "❌You're not bot owner"
}});
try {
const code = args.join(" ");
let evaled = eval(code);
if (typeof evaled !== "string") {
//console.log(evaled);
evaled = await require("util").inspect(evaled);
//console.log(evaled);
message.channel.send(`\`\`\`xl\n${evaled}\`\`\``, { split: true });
} else {
message.channel.send(`\`\`\`xl\n${evaled}\`\`\``, { split: true });
}
} catch (err) {
message.channel.send({
embed: {
title: "✍️RUN",
description: "Error:\n```xl\n" + err + "```",
color: 961818,
timestamp: new Date(),
footer: {
icon_url: client.user.avatarURL
}
}
});
}
}
But, when I run !run testcode.length with this code, I get a cannot send an empty message error on the console. How can I fix this bug?

discord.js Welcome message

yea hello I'm making a discord.js bot and I have this code currently and it REFUSES to send a message (it doesn't error either
let chx = db.get(`welchannel_${member.guild.id}`);
if (chx === null) {
return;
}
let data = await canva.welcome(member, { link: "https://i.pinimg.com/originals/f3/1c/39/f31c39d56512dc8fbf30f9d0fb3ee9d3.jpg" })
const attachment = new discord.MessageAttachment(
data,
"welcome-image.png"
);
client.channels.cache.get(chx).send("Welcome to our Server " + member.user.username, attachment);
});
and then i have welcome.js with this code but it aint sending and i cant figure out why ...
const db = require("quick.db")
module.exports = {
name: "setwelcome",
category: "moderation",
usage: "setwelcome <#channel>",
description: "Set the welcome channel",
run: (client, message, args) => {
let channel = message.mentions.channels.first()
if(!channel) {
return message.channel.send("Please Mention the channel first")
}
//Now we gonna use quick.db
db.set(`welchannel_${message.guild.id}`, channel.id)
message.channel.send(`Welcome Channel is set to ${channel}`)
}
}```
just a guess that your not checking for undefined on CHX
if (chx === null && chx === undefined) {
return;
}
This is my modified code from my discord bot (just to keep it general).
this.client.on('ready', () => {
if(botChannelID){
this.client.channels.fetch(botChannelID).then(ch => {
ch.send(`Welcome message here.`);
}).catch(e => {
console.error(`Error: ${e}`);
});
}
//Other stuff. Blah blah blah...
});

Discord.Js bot doesn't wait for a response

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...

So I am trying to ask a question to retrieve a mentioned channel but doesn't work

So I am trying to make my giveaway bot but can't make a create command so the person can add more details about the giveaway! Thus, I need questions but everytime I try it never goes well!
message.channel.send("Please mention the channel you want the giveaway to be in! **e.g #channel**");
try {
let msgs = await message.channel.awaitMessages(u2=>u2.author.id===message.author.id, { time: 15000, max: 1, errors: ["time"]});
if(parseInt(msgs.first().content)==mention.channel) {
const Channel = message.mentions.channels.first();
await Channel.send("HEY")
}
else {
message.channel.send("You did not mentioned a channel!");
}
}catch(e) {
return message.channel.send("Command Cancel!")
}
It either returns to the catch(e) line or "You did not mentioned a channel!"!
This should be what you want i think. (works with mentioning the channel, providing a channel ID or providing a channel name)
message.channel.send("Please mention the channel you want the giveaway to be in! **e.g #channel**");
let channel;
let response;
try {
response = await message.channel.awaitMessages(msg => msg.author.id === message.author.id, { max: 1, time: 1000*60*3, errors: ['time'] })
} catch {
return message.channel.send("Command Cancel!")
}
if (response.first().mentions.channels.first()) {
channel = response.first().mentions.channels.first()
} else if (!isNaN(response.first().content) && message.guild.channels.cache.get(response.first().content)) {
channel = message.guild.channels.cache.get(response.first().content)
} else if (isNaN(response.first().content) && message.guild.channels.cache.find(c => c.name.toLowerCase() === response.first().content.toLowerCase())) {
channel = message.guild.channels.cache.find(c => c.name.toLowerCase() === response.first().content.toLowerCase())
}
if (channel) {
await channel.send("HEY")
} else {
return message.channel.send("You did not mentioned a channel!");
}

Resources