-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
81 lines (73 loc) · 2.56 KB
/
Copy pathindex.js
File metadata and controls
81 lines (73 loc) · 2.56 KB
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
// @ts-check
const { Client, IntentsBitField, AttachmentBuilder } = require("discord.js");
const { token, prefix, authorized } = require("./config.json");
const fs = require("fs");
const path = require("path");
const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent,
]
});
const messages = fs.readdirSync(path.join(__dirname, "messages"))
.map(e => ({
name: e.substring(0, e.lastIndexOf(".")),
data: require(path.join(__dirname, "messages", e))
}));
if (messages.some(e => e.name.includes(" "))) {
console.log("Fuck you. One of your files contains spaces.");
process.exit(0);
}
client.on("ready", () => {
console.log(`Hi - ${client.user?.tag}`);
console.log(`Messages found: ${messages.map(e => e.name).join(", ")}`);
});
/**
* @param {string} channelId
*/
async function safeFetchChannel(channelId) {
let channel = null;
try {
channel = await client.channels.fetch(channelId.match(/\d+/g)?.[0] ?? "")
} catch (err) {}
return channel;
}
async function fetchBuffer(url) {
const response = await fetch(url);
const array = await response.arrayBuffer();
return Buffer.from(array);
}
client.on("messageCreate", async (message) => {
if (!message.content.startsWith(prefix)) return;
if (!authorized.channels.includes(message.channelId)) return;
if (!authorized.users.includes(message.author.id)) return;
const args = message.content.split(/ +/g);
const name = args[0].replace(prefix, "");
const msg = messages.find(e => e.name === name);
if (!msg) return;
/** @type {any} */
const channel = args.at(1) ? (
(await safeFetchChannel(args[1])) ?? message.channel
) : message.channel;
for (let index = 0; index < msg.data.length; index++) {
const data = msg.data[index];
/** @type {import("discord.js").BaseMessageOptions} */
const payload = {
content: data.content ?? undefined,
embeds: data.embeds ?? undefined,
components: data.components ?? undefined
};
const attachments = (data.attachments ?? []);
payload.files = [];
for (let index = 0; index < attachments.length; index++) {
const attachment = attachments[index];
const buffer = await fetchBuffer(attachment.url);
payload.files.push(
new AttachmentBuilder(buffer, { name: attachment.filename })
)
}
await channel.send(payload);
}
});
client.login(token);