-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (51 loc) · 1.7 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
const { Client, Intents } = require('discord.js');
const { Client: NotionClient, APIErrorCode } = require('@notionhq/client');
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}
// Initialize Discord bot
const discordClient = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
// Initialize Notion client
const notionClient = new NotionClient({ auth: process.env.NOTION_SECRET });
const notionDatabaseId = process.env.NOTION_DATABASE_ID;
// Event for when the bot is ready
discordClient.once('ready', () => {
console.log('Discord bot is ready!');
});
// Event for when a message is sent in Discord
discordClient.on('messageCreate', async (message) => {
// Ignore messages from the bot itself
if (message.author.bot) {
return;
}
// Add a new entry to the Notion database with the message content
try {
await notionClient.pages.create({
parent: { database_id: notionDatabaseId },
properties: {
Name: { // Assuming the database has a 'Name' property
title: [
{
type: 'text',
text: { content: message.content },
},
],
},
Channel: { // Assuming the database has a 'Channel' property
type: 'text',
text: { content: message.channel.name },
},
// Add other properties here
},
});
console.log('Entry added to Notion database!');
} catch (error) {
if (error.code === APIErrorCode.ObjectNotFound) {
console.error('The Notion database was not found');
} else {
console.error('An error occurred:', error);
}
}
});
// Start the Discord bot
discordClient.login('your_discord_bot_token');