-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
87 lines (79 loc) · 2.64 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require("dotenv").config({ path: "./.env" });
const express = require("express");
const puppeteer = require("puppeteer");
const fs = require("fs");
const Discord = require("discord.js");
const app = express();
app.get("/", (req, res) => {
res.send("Bot is live");
});
const client = new Discord.Client();
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}`);
});
const prefix = "-";
client.on("message", async function (message) {
try {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;
const commandBody = message.content.slice(prefix.length);
const args = commandBody.split(" ");
const command = args.shift().toLowerCase();
if (command === "cvrt") {
const url = message.content.split(" ")[1];
if (url.startsWith("https://") || url.startsWith("http://")) {
console.log(url);
let file = { url };
let options = {
format: "A4",
args: ["--no-sandbox", "--disable-setuid-sandbox"],
margin: {
top: "40px",
bottom: "40px",
left: "40px",
right: "40px",
},
};
let args = ["--no-sandbox", "--disable-setuid-sandbox"];
await message.reply("Converting webpage to PDF, please wait...");
const browser = await puppeteer.connect({
browserWSEndpoint: "wss://chrome.browserless.io/",
// browserURL: url,
// args: args,
// ignoreDefaultArgs: ['--disable-extensions'],
});
const page = await browser.newPage();
await page.goto(file.url, {
waitUntil: "networkidle0",
});
const data = await page.pdf(options);
await browser.close();
await message.reply("Generating PDF, please wait...");
console.log(Buffer.from(Object.values(data)));
const fileName = Date.now();
fs.writeFileSync(
`./pdfs/${fileName}` + ".pdf",
Buffer.from(Object.values(data))
);
await message.channel.send("Here is your pdf.", {
files: [`./pdfs/${fileName}.pdf`],
});
fs.unlink(`./pdfs/${fileName}.pdf`, function (err) {
if (err) {
throw err;
} else {
console.log("Successfully deleted the file.");
}
});
} else {
await message.reply("URL must start with http:// or https://");
}
}
} catch (err) {
console.log(err);
await message.reply("PDF generation failed. Please try again!");
}
});
// App server to listen
app.listen(process.env.PORT || 8080, () => console.log("Bot connected"));
client.login(process.env.BOT_TOKEN);