Reputation: 35
I don't know much Javascript, so please bear with me.
So, I'm trying to make a command for my Discord Bot. Basically, what I want to happen is, when you post "!records", I want the Bot to send a RichEmbed with the 2 options you can choose.
if (message.content.startsWith(config.prefix + 'records')) {
const embed = new Discord.RichEmbed()
.setTitle('D E I C I D E Records')
.setAuthor('D E I C I D E', 'https://cdn.discordapp.com/attachments/430238345826664448/430504575502385154/logo.png')
.setColor(3447003)
.setDescription('Please select an option.')
.setTimestamp()
.addField('1. Raid Wins', 'Respond with "1" for Raid Wins.')
.addField('2. Defense Wins', 'Respond with "2" for Defense Wins.')
message.channel.send({embed})
I have that part nailed. It does exactly what I want there. It sends the RichEmbed with the two options.
What I want to happen next is, when you send either "1", or "2", I want the Bot to reply with the correct records. This is part I can't get right. Here's all the code together:
if (message.content.startsWith(config.prefix + 'records')) {
const embed = new Discord.RichEmbed()
.setTitle('D E I C I D E Records')
.setAuthor('D E I C I D E', 'https://cdn.discordapp.com/attachments/430238345826664448/430504575502385154/logo.png')
.setColor(3447003)
.setDescription('Please select an option.')
.setTimestamp()
.addField('1. Raid Wins', 'Respond with "1" for Raid Wins.')
.addField('2. Defense Wins', 'Respond with "2" for Defense Wins.')
message.channel.send({embed})
message.channel.awaitMessages(response => response.content === '1', {
max: 1,
time: 10000,
errors: ['Ran out of time!'],
})
message.channel.send(drecords.RaidWins)
message.channel.awaitMessages(response => response.content === '2', {
max: 1,
time: 10000,
errors: ['Ran out of time!']
})
message.channel.send(drecords.DefenseWins)
} else
At the moment, when you post "!records", it sends the RichEmbed, but also sends the records, instead of waiting for a reply.
Can someone outline what I'm doing wrong here please?
Also, I would like to be able to say a single command to update the records without having to go into the Bot's files and do it manually.
if (message.content.startsWith(config.prefix + 'updateraids')) {
let newRecords = message.content.split(" ").slice(1, 2)[0];
drecords.RaidWins = newRecords;
fs.writeFile('./drecords.json', JSON.stringify(drecords), (err) => console.error);
} else
if (message.content.startsWith(config.prefix + 'updatedefenses')) {
let newRecords = message.content.split(" ").slice(1, 2)[1];
drecords.DefenseWins = newRecords;
fs.writeFile('./drecords.json', JSON.stringify(drecords), (err) => console.error);
}
At the moment, when you say either one of those commands and then some text after, so for example, "!updatedefenses DefenseWin2", it replaces the first string (DefenseWin1) instead of adding another in the Bot's files. And how would I go about removing an entry instead of adding one? Sorry if there's a bit too much here, wanted to squeeze it all into one post as it's all related.
I have done quite a few hours of research, going through other Stackoverflow questions, going through Discord.js, and the An Idiot's Guide YouTube tutorials but no luck.
Any help here would be really appreciated.
Upvotes: 2
Views: 1084
Reputation: 3424
You aren't using awaitMessages
correctly. You only need one, not 2 or more for your case. The filter will check if the message is either a 1 or 2. Here's what your code should look like. I also fixed your "time up" error handler.
client.on("message", message => {
if (message.content.startsWith("/records")) {
const embed = new Discord.RichEmbed()
.setTitle('D E I C I D E Records')
.setAuthor('D E I C I D E', 'https://cdn.discordapp.com/attachments/430238345826664448/430504575502385154/logo.png')
.setColor(3447003)
.setDescription('Please select an option.')
.setTimestamp()
.addField('1. Raid Wins', 'Respond with "1" for Raid Wins.')
.addField('2. Defense Wins', 'Respond with "2" for Defense Wins.');
message.channel.send({embed})
message.channel.awaitMessages(response => (response.content === '1' || response.content === "2"), {
max: 1,
time: 10000,
errors: ['time']
}).then(mg => {
if (mg.first().content === "1"){ //Have to use .first() because mg is a Collection
message.channel.send(drecords.RaidWins);
}else if (mg.first().content === "2"){
message.channel.send(drecords.DefenseWins);
}
}).catch((err) => {
message.channel.send("Ran out of time!");
})
}
})
As for your second question, when you use .writeFile
, you're essentially overwriting the file. You'll want to use appendFile
. However, you're using JSON. You can simply import the file (require
it) and update it. Might I suggest you change the json file to look like this if this is what you're going for.
{
"RaidWins": 0,
"DefenseWins": 0
}
And then importing and updating would look like this
let records = require("./drecords.json");
records.RaidWins += 1; //adding one to the wins
fs.writeFile("./drecords.json", JSON.Stringify(records), err => console.error);
Upvotes: 1
Reputation: 760
If you look at the example in the awaitMessages
section of the docs it appears you need to put the part of your code that sends the response in a .then
.
Something like
message.channel.awaitMessages(response => response.content === '1', {
max: 1,
time: 10000,
errors: ['Ran out of time!'],
})
.then(message.channel.send(drecords.RaidWins));
message.channel.awaitMessages(response => response.content === '2', {
max: 1,
time: 10000,
errors: ['Ran out of time!']
})
.then(message.channel.send(drecords.DefenseWins));
As for the file issue, could you elaborate more on the format of the files and what the commands would look like?
Upvotes: 0