J
J
Jason_Kings2020-07-26 16:33:34
Node.js
Jason_Kings, 2020-07-26 16:33:34

How to block the creation of an existing promotional code?

Hello, there was such a problem. I wrote a command to generate a promo code. In fact, everything works in it, except for the prohibition on creating an existing promotional code. In my case, there is already a promo code `#jk`, but anyone can still create it. I tried many methods already, nothing helped. Your last hope so to speak.

const Discord = module.require("discord.js");
const fs = require("fs");
const settings = require('../botconfig.json');
module.exports.run = async (bot,message,args) => {
    Promo.findOne({guildID: message.guild.id, ownerpromoID: message.author.id}, (err,dat) => {
        if(!dat){
            let errorMess = new Discord.RichEmbed()
            .setColor('RED')
            .setDescription(`**Вы отсутствуете в базе данных. Обратитесь к Разработчику** \`Jason Kings#5476\` **для решения проблемы. Код ошибки:** \`775\``)
            return message.channel.send(errorMess)
          }
          if(err) {
          let xembed = new Discord.RichEmbed()
          .setColor(`RED`)
          .setDescription(`**Не удалось создать промокод. Ошибка:** \`${err}\``)
          return message.channel.send(xembed)
          }
          if(!args[0]) return message.delete() && message.channel.send(`<@${message.author.id}>, \`Введите название вашего будущего промокода! Пример: /createpromocode #jk\``).then(msg => msg.delete(15000));
    if (!args[0].startsWith("#")) return message.delete() && message.channel.send(`<@${message.author.id}>, \`Название вашего промокода должно начинаться с "#"! Пример: /createpromocode jk\``).then(msg => msg.delete(15000));
    if(`${dat.promocode =args[0]}`) return message.delete() && message.channel.send(`<@${message.author.id}>, \`такой промокод уже есть!\``)
    if(dat.promocode !=`${dat.promocode ="-"}`) return message.delete() && message.channel.send(`<@${message.author.id}>, \`Вы уже создали свой промокод!\``).then(msg => msg.delete(15000));
    message.channel.send(`<@${message.author.id}>, \`Вы успешно создали свой промокод\` **${args[0]}**`)
    dat.promocode =`${args[0]}`
    dat.lvlpromo =1
    dat.save()
    })
}
module.exports.help = {
    name: "createpromocode"
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-07-26
@Alexandre888

I don’t understand well that it doesn’t work out for you, but you have a line in your code:

if(`${dat.promocode =args[0]}`) return message.delete() && message.channel.send(`<@${message.author.id}>, \`такой промокод уже есть!\``)

if that is the problem, then I advise you to make a number of the following changes:
1. why do you need a template literal in the condition, if you can do it just like this: 2. instead of = in the condition, use === 3. why do you use if to mention 4. remove the && and message.delete() from the string, and then leave only message.reply . if the code with such changes works, then you can already think about deleting the message. if it does not work, then the problem is in and the approach will already need a completely different one.
if (dat.promocode = args[0]) . . .

message.channel.sendmessage.reply("текст")

dat.promocode

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question