如何将本地文件附加到嵌入?

时间:2020-06-17 11:49:09

标签: node.js discord.js

const Discord = require('discord.js');
const {Client, MessageAttachment} = require('discord.js');
const bot3 = new Client();


const mark2 = '*info personal'
const mark3 = '*info guild'
const mark4 = '*info roles'
bot3.on('message', msg =>{

    if (msg.content.startsWith(mark2)){
        const hashitag = msg.author.id
        const actualhashitag = msg.author.discriminator
        const evenbetterhashitag = "#" + actualhashitag 
        const personalembed = new Discord.MessageEmbed()
            .setTitle('Stuff about you')
            .setImage(msg.author.displayAvatarURL())
            .setColor('#D11111')
            .addField('Your username',  msg.author.username)
            .addField('Your hashtag', hashitag, true)
            .addField('Your actual hashtag', actualhashitag, true)
            .addField('Your even realer hashtag', evenbetterhashitag, true)   
            .addField('Your amount of friends', 'Discord bots can not know that sadly')
            .addField('Your role', msg.member.roles.cache.map(role => role.name).join(", ") )
            .addField('Your role id', msg.member.roles.cache.map(role => role.id).join(", ") )
            .addField('Your nickname', msg.member.nickname);
        msg.channel.send(personalembed)
    }
    if (msg.content.startsWith(mark3)){




        const guildembed = new Discord.MessageEmbed()

            .setTitle(msg.guild.name)
            .setImage(msg.guild.iconURL())
            .setColor('#97FF00')
            .addField('the id of the server', msg.guild.id)
            .addField('the owner', msg.guild.owner.user.username)
            .addField('the owner tag', msg.guild.owner.user.tag)
            .addField('the owner id', msg.guild.ownerID)
            .addField('the owner nickname', msg.guild.owner.nickname)
            .addField('all roles', msg.guild.roles.cache.map(role => role.name).join(", ") ) 
            .addField('the bots', msg.guild.members.cache.filter(member => member.user.bot).map(member => member.user.tag).join(' | '))
            .addField('the bots actual name', msg.guild.members.cache.filter(member => member.user.bot).map(member => member.user.username).join(' | '))
            .addField('the bot ids', msg.guild.members.cache.filter(member => member.user.bot).map(member => member.user.id).join(' | '))
            .addField('the bots hashtag(without the hashtag)', msg.guild.members.cache.filter(member => member.user.bot).map(member => member.user.discriminator).join(' | '))
        msg.channel.send(guildembed)

    }  

    if(msg.content.startsWith(mark4)){
        const attachmento = new MessageAttachment('./stuff\role5.png', 'name') 
        const roleembed = new Discord.MessageEmbed()
            .setTitle('All roles and the people with the roles')
            .attachFiles(attachmento)
            .setThumbnail(attachmento)
            .setColor('FFA737')
        msg.channel.send(roleembed)
    }


})

bot3.login(process.env.token3)

如果您想知道,discord.js版本是12.2.0。 我想将图像附加到第三个embed(忽略该embed的标题,稍后再尝试处理)这是一个名为role5的本地文件。我似乎无法将该文件附加到嵌入文件中。这就是发生的情况。

2020-06-17T11:33:33.792109+00:00 heroku[Worker.1]: State changed from up to starting
2020-06-17T11:33:34.000000+00:00 app[api]: Build succeeded
2020-06-17T11:33:35.043286+00:00 heroku[Worker.1]: Stopping all processes with SIGTERM
2020-06-17T11:33:35.136963+00:00 heroku[Worker.1]: Process exited with status 143
2020-06-17T11:33:35.621163+00:00 heroku[Worker.1]: Starting process with command `node index.js`
2020-06-17T11:33:36.314618+00:00 heroku[Worker.1]: State changed from starting to up
2020-06-17T11:33:38.193183+00:00 app[Worker.1]: This bot is online
2020-06-17T11:34:17.887173+00:00 app[Worker.1]: (node:4) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, stat '/app/stuff
2020-06-17T11:34:17.887251+00:00 app[Worker.1]: (node:4) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
2020-06-17T11:34:17.887307+00:00 app[Worker.1]: (node:4) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

1 个答案:

答案 0 :(得分:1)

discord.js docs中,创建嵌入时,您需要使用.attachFiles()将本地文件附加到嵌入中。之后,您可以在设置嵌入图像/图标时使用attachment://fileName.extension

嵌入角色的示例:

const roleembed = new Discord.MessageEmbed()
            .setTitle('All roles and the people with the roles')
            .attachFiles('./stuff/role5.png')
            .setThumbnail("attachment://role5.png")
            .setColor('FFA737')
        msg.channel.send(roleembed)