我在 Replit 中托管我的 Discord Bot。而且,我使用 node-html-to-image
包将 html 转换为 jpeg。当我在 localhost
中尝试时,它运行良好。但是当我在 Replit 中尝试时,它返回此错误:
(node:250) UnhandledPromiseRejectionWarning: Error: Unable to launch browser, error message: Failed to
launch the browser process!
这是我使用的代码,htmltoPng.js
:
const { MessageAttachment } = require("discord.js");
const nodeHtmlToImage = require("node-html-to-image");
const puppeteer = { args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage', '--disable-accelerated-2d-canvas', '--no-first-run', '--headless', '--no-zygote', '--disable-gpu'], headless: true, ignoreHTTPSErrors: true };
module.exports = async (msg, user, data) => {
const _htmlTemplate = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<style>
body {
font-family: "Poppins", Arial, Helvetica, sans-serif;
background: rgb(22, 22, 22);
color: #fff;
max-width: 300px;
}
.app {
max-width: 300px;
padding: 20px;
display: flex;
flex-direction: row;
border-top: 3px solid rgb(16, 180, 209);
background: rgb(31, 31, 31);
align-items: center;
}
img {
width: 50px;
height: 50px;
margin-right: 20px;
border-radius: 50%;
border: 1px solid #fff;
padding: 5px;
}
</style>
</head>
<body>
<div class="app">
<img src="${user.username}" />
<h4>Welcome ${msg.author.username}</h4>
</div>
</body>
</html>
`;
const images = await nodeHtmlToImage({
html: _htmlTemplate,
quality: 100,
type: "jpeg",
puppeteerArgs: puppeteer,
});
return msg.channel.send(new MessageAttachment(images, `${name}.jpeg`));
};
非常感谢任何帮助!
谢谢!
答案 0 :(得分:-1)
您在这里的目标是创建一张欢迎卡,然后将其发布到频道中,对吗?为什么不为此使用画布,它会更快更高效?甚至还有专门针对不和谐的在线指南:
https://discordjs.guide/popular-topics/canvas.html#setting-up-canvas
快乐编码!
答案 1 :(得分:-1)
包 node-html-to-image
当前使用的 pupeteer 3.0.0
不包含所有必要的依赖项。这已在 pupeteer 3.0.4
中修复。
您可以在等待包更新的同时安装必要的依赖项。
sudo apt-get install -y libgbm-dev
这是 node-html-to-image
的问题
https://github.com/frinyvonnick/node-html-to-image/issues/91
这是 ppeteer 方面的问题 https://github.com/actions/virtual-environments/issues/732#issuecomment-614809415
拉取请求已被接受。 node-html-to-image
v3.2.0 应该适合您。