D
D
Den182021-09-04 18:29:15
Node.js
Den18, 2021-09-04 18:29:15

Good afternoon, how can I use the time limit function for entering a certain command in discord js?

I use:

const talkedRecently = new Set();
if(message.content.startsWith(prefix + "day")) {
        if (talkedRecently.has(message.author.id)) {
               message.channel.send({ content: Вы уже использовали эту команду. Повторите через 24 часа 
               })
        } else {
                  message.channel.send({ content: Успешно })
                  talkedRecently.add(message.author.id);
                                setTimeout(() => {
                                     talkedRecently.delete(message.author.id);
                                }, 86400000);
        }
}

I need to display the remaining time in the message in the format "days, hours, minutes, seconds".
Help make this happen.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Den18, 2021-10-04
@Den18

I solved the issue myself

spoiler
pool.query(`SELECT date_added FROM moneys WHERE UserID = ${interaction.user.id} AND GuildID = ${interaction.guild.id}`, async function (err, result, fields) {
                            if (err) throw err;
                            
                            if (result.length > 0) {    
                                if (result[0]['date_added'] === null) {
                                    pool.query(`UPDATE moneys SET Moneyscol = Moneyscol + 500, date_added = NOW(), date_next = (NOW() + INTERVAL 1 DAY)  WHERE UserID = ${interaction.user.id} AND GuildID = ${interaction.guild.id}`, async function (err, result, fields) {
                                        if (err) throw err;
                                        
                                        return interaction.reply({ content: "Успешно" })
                                        
                                    }); 
                                } else {
                                    pool.query(`UPDATE moneys SET date_added = NOW() WHERE UserID = ${interaction.user.id} AND GuildID = ${interaction.guild.id}`, async function (err, result, fields) {
                                        if (err) throw err;
                                        
                                        pool.query(`SELECT date_added, date_next FROM moneys WHERE UserID = ${interaction.user.id} AND GuildID = ${interaction.guild.id}`, async function (err, result, fields) {
                                            if (err) throw err;
                                            
                                            if (result[0]['date_added'] >= result[0]['date_next']) {
                                                pool.query(`UPDATE moneys SET Moneyscol = Moneyscol + 500, date_added = NOW(), date_next = (NOW() + INTERVAL 1 DAY) WHERE UserID = ${interaction.user.id} AND GuildID = ${interaction.guild.id}`, async function (err, result, fields) {
                                                    if (err) throw err;
                                                    
                                                    return interaction.reply({ content: "Успешно" })                                                   
                                                });
                                            } else {
                                                pool.query(`SELECT TIME_FORMAT(TIMEDIFF(date_next, date_added), '%H ч, %i мин, %s сек') as time from moneys WHERE UserID = ${interaction.user.id} AND GuildID = ${interaction.guild.id}`, async function (err, result, fields) {
                                                    if (err) throw err;
                                                    const data = {
                                                        time: result[0]["time"]
                                                    }                                                                                           
                                                    let embed = new MessageEmbed()
                                                    .setColor("#FF0000")
                                                    .setTitle(`${interaction.member.displayName}`)
                                                    .setDescription(`Вы уже использовали эту команду. Попробуйте снова через \`${data.time}\``)
                                                    .setThumbnail(interaction.user.displayAvatarURL())
                                                    .setTimestamp(new Date())
                                                    return interaction.reply({ embeds: [embed] })
                                                });
                                            }
                                            
                                        });
                                        
                                    });
                                }

                            } else {
                                pool.query(`INSERT INTO moneys (GuildID, UserID, Moneyscol, date_added, date_next) VALUES (${interaction.guild.id}, ${interaction.user.id}, 500, NOW(), (NOW() + INTERVAL 1 DAY))`, async function (err, result, fields) {
                                    if (err) throw err;
                                    
                                    let embed = new MessageEmbed()
                                        .setColor("#FFCC4D")
                                        .setTitle(`${interaction.member.displayName}`)
                                        .setDescription(`Вы получили ежедневный бонус в размере \`500\` :coin:`)
                                        .setThumbnail(interaction.user.displayAvatarURL())
                                        .setTimestamp(new Date())
                                    return interaction.reply({ embeds: [embed] }) 
                                    
                                }); 
                            }
                            
                        });

N
not important ., 2021-09-09
@26DiDi12

It took me an hour to solve, here is your code remade by me:

let talkedRecently = 0;
let talkedRecentlyDAYS = 0;
let talkedRecentlyHOURS = 0;
let talkedRecentlyMINUTES = 0;
let talkedRecentlySECUNDES = 0;

if(message.content.startsWith(prefix + "day")) {
        if (talkedRecently > 0) {
               message.channel.send(`Вы уже использовали эту команду. Повторите через ${Math.floor(talkedRecentlyDAYS)} дней / ${Math.floor(talkedRecentlyHOURS)} часов / ${Math.floor(talkedRecentlyMINUTES)} минут / ${talkedRecentlySECUNDES} секунд.`)
        } else {
                  message.channel.send("Успешно!")
                  talkedRecently = 86400000;
                  talkedRecentlyDAYS = talkedRecently/86400000;
                  let time = setInterval(() => {
                        if (talkedRecently <= 0) { clearInterval(time); return; }
                        talkedRecentlySECUNDES -= 1;
                        if (talkedRecentlySECUNDES < 0) { talkedRecentlySECUNDES = 59; talkedRecentlyMINUTES -= 1; }
                        if (talkedRecentlyMINUTES < 0) { talkedRecentlyMINUTES = 59; talkedRecentlyHOURS -= 1; }
                        if (talkedRecentlyHOURS < 0) { talkedRecentlyHOURS = 23; talkedRecentlyDAYS -= 1; }
                        talkedRecently -= 1000;
                  }, 1000);
        }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question