首页 > 解决方案 > 如何将本地文件附加到嵌入?

问题描述

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。我想将图像附加到第三个嵌入(忽略嵌入的标题,我稍后会尝试处理)它是一个名为 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.

标签: node.jsdiscord.js

解决方案


discord.js 文档中,创建嵌入时,您需要使用.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)

推荐阅读