-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.js
43 lines (35 loc) · 1.28 KB
/
run.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
import * as dotenv from 'dotenv'
import { fileURLToPath } from 'url';
import { join, dirname } from 'path';
import Discord from 'discord.js'
import {promises as fs} from 'fs';
dotenv.config()
async function save_json(data,rel_path){
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const filepath = join(__dirname,rel_path)
await fs.writeFile(filepath,JSON.stringify(data,undefined, 2))
console.log(` saved json file ${filepath}`)
}
const client = new Discord.Client({ intents: [
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildMessages
]})
const fetchMessages = async (channelId) => {
const channel = await client.channels.fetch(channelId);
if (!channel) {
throw new Error('Channel not found.');
}
const raw_messages = await channel.messages.fetch();
const messages = raw_messages.map(message=> message.cleanContent)
await save_json(messages,`messages/${channel.name}-${channelId}.json`)
};
client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
const channelId = process.env.CHANNEL_ID;
fetchMessages(channelId).catch((error) => {
console.error(error.message);
});
client.destroy()
});
client.login(process.env.BOT_TOKEN);