使用Discord.J编辑嵌入字段

时间:2020-04-25 17:12:39

标签: javascript discord.js

我要执行以下操作:

我将使用命令创建嵌入(!setupembed) 这将在特定通道中创建嵌入,并向嵌入添加反应。 有一个名为Codes的嵌入字段,用于计算.txt的行数

每次用户对带有表情符号的嵌入内容做出反应时,他都应该使用存储在.txt中的代码获取DM。 现场计数器:代码应减少1 到目前为止,这就是我的代码:

bot.on("ready", () =>{
    console.log("Succesfully started the tools bot");
})




    bot.on("message", message=>{

        let args = message.content.substring(prefix.length).split(" ");
        var mcodes = fs.readFileSync('./text.txt').toString().split("\n").length;
        const embed = new Discord.MessageEmbed().setTitle("Codes").setDescription("Pls React").addField("Instock", "`" + mcodes + "`")




        switch(args[0]){

             case "code": 
             let codetext = message.content.replace(prefix + "code", "")
                fs.appendFile("./text.txt", codetext + "\n",(err)=>{
                if (err) throw err;
                      message.channel.send("message written")
                 })
                const newembed = new Discord.MessageEmbed()
                .setTitle("CODES")
                .addField("Instock", "`" + snscodes + 1 + "`")
                message.edit(embed)
            break;


            case "getcode":
            let getcode = fs.readFileSync("./text.txt", "utf8", (err)=>{
                if (err) throw err;
            })
            var code_array = getcode.split("\n")
                message.channel.send(code_array[0])
            let spliced = code_array.splice(0);
                fs.writeFile("./text.txt", "utf8", (err)=>{
                if (err) throw err;
                 })
            break;


            case "setupembed":
                bot.channels.cache.get("698221179810349187").send(embed).then(sent => {
                        sent.react("?️")
                    })

            break;






        }
    })
    ```

I already tried to write the part bymyself but i cant get it to work thats my attempt: (see answers)



1 个答案:

答案 0 :(得分:0)

  case "setupembed": 
        const embed = new Discord.MessageEmbed()
            .setTitle("Codes")
            .addField("CODES", "`" + mcodes + "`")
            bot.channels.cache.get("698221179810349187").send(embed)
            .then(sent => {
                sent.react("?️")
            })
            const filter = (reaction, user) => {
                return reaction.emoji.name === '?️' && user.id === message.author.id;
            }; 
            const collector = message.createReactionCollector(filter, { time: 15000 });
            collector.on('collect', (reaction, reactionCollector) => {
                let getcode = fs.readFileSync("./text.txt", "utf8", (err)=>{
                    if (err) throw err;
                })
                var code_array = getcode.split("\n")
                message.author.send(code_array[0])
                let spliced = code_array.splice(0);
                fs.writeFile("./text.txt", "utf8", (err)=>{
                    if (err) throw err;
                })
            });
            collector.on('end', collected => {
                console.log(`Collected ${collected.size} items`);
            });
            break;
    ```