-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.mjs
74 lines (61 loc) · 2.59 KB
/
main.mjs
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
import { Client, GatewayIntentBits, REST, Routes } from "discord.js"
import puppeteer from "puppeteer"
import { v4 as uuidv4 } from 'uuid';
import express from "express"
const app = express()
app.use("/internal", express.static('static'));
app.use("/image", express.static('img'));
app.listen(3000, () => console.log("express ok"))
const commands = [
{
name: 'jellysay',
description: 'JELLYさんがしゃべります',
options: [
{
name: "text",
required: true,
description: "テキスト",
type: 3
}
]
},
];
const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN);
const setupCommand = async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(Routes.applicationCommands(process.env.CLIENT_ID), { body: commands });
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
}
await setupCommand()
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
client.on('guildCreate', async (guild) => {
guild.commands.set(commands).then(() =>
console.log(`Commands deployed in guild ${guild.name}!`));
})
client.on('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) return;
if (interaction.commandName === 'jellysay') {
const text = interaction.options.get("text")?.value
const reply = await interaction.reply("generating...")
const uuid = await generateJELLYImage(text)
await interaction.channel.send(`https://${process.env.HOST}/image/${uuid}.png`)
await interaction.deleteReply()
}
});
client.login(process.env.DISCORD_TOKEN);
const generateJELLYImage = async (text) => {
const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'], headless: true });
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 720 });
await page.setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36");
await page.goto("http://localhost:3000/internal/index.html?text=" + text, { waitUntil: 'networkidle0' });
await page.waitForTimeout(2600);
const uuid = uuidv4()
await page.screenshot({ path: `./img/${uuid}.png` });
await browser.close();
return uuid
}