Twitter: https://twitter.com/ndehouche
Some of your best ideas and content are generated on-the-fly in Discord conversations. In this tutorial, we are going to create a Discord bot that will allow you to automatically send messages from a Discord server to Notion, if an Admin reacts to them with a specified emoji, say ✍️.
This can notably be useful to automatically save FAQs, user suggestions, or just generally great content from your Discord server.
First, we are going to create a Discord bot, save its access token, and invite it in our Discord server. You can skip the following section, if you are already familiar with creating Discord bots.
How to create a Discord Bot
For this application, our bot needs to read and write messages, as well as possibly manage emojis if you want to use custom reactions, and read message history, if you want to add support for messages posted before the addition of the bot to the server. We select the corresponding permissions, and copy the generated URL.
This URL will open a Discord permission page that will allow you add the bot to any server you manage.
Now let's create a Notion integration that will allow our Discord bot to write into a Notion page. You can skip the following section if you are already familiar with the Notion API.
The Notion API is currently in Beta, so a few things described here may change, but but as of the time of this writing (January 2022), here is how to create a Notion integration that will allow us to write into a Notion page from an external application.
We need to have npm and Node.js installed and the dotenv package to import the various API keys into Node.js.
We will also need to install the Notion SDK and discord.js.
.env
file in which you will safely store our environment variables for Discord and Notion.
The following environment variables are respectively the Discord access token we got here, the Notion integration token we got here, and the Notion database ID we got here.
DISCORD_TOKEN='...' NOTION_KEY='secret_...' NOTION_DATABASE_ID='...'
index.js
.require('dotenv').config()
const token = process.env.DISCORD_TOKEN const NOTION_KEY=process.env.NOTION_KEY const NOTION_DATABASE_ID=process.env.NOTION_DATABASE_ID
var {Client} =require("@notionhq/client"); const notion = new Client({ auth: NOTION_KEY })
async function addItem(text) { try { const response = await notion.pages.create({ parent: { database_id: NOTION_DATABASE_ID }, properties: { title: { title:[ { "text": { "content": text}}]}},}) console.log(response) console.log("Success! Entry added.")} catch (error) { console.error(error.body) }}
And we are done with Notion! Let's now turn our attention to Discord.var { Client, Intents, MessageEmbed } = require('discord.js')
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS] });
client.on('ready', () => { console.log('I am ready!');});
client.on('messageReactionAdd', (reaction, user) => { if (user.bot) return; console.log('reaction'); if(reaction.emoji.name === "✍️") { if (reaction.message.member.roles.cache.some(role => role.name === 'Admin')) { let embed = new MessageEmbed() .setTitle('Content added to Notion') .setDescription(reaction.message.content) .setAuthor({name: reaction.message.author.tag, iconURL: reaction.message.author.displayAvatarURL()} ) addItem(reaction.message.content) reaction.message.channel.send({embeds: [embed]}).catch(console.error) return;}}});
client.login(token);
We can now run node index.js
. Here is what the bot should be able to do.
Here is the complete program for your copypasta pleasure.
// Discord token
const token = process.env.DISCORD_TOKEN
// Notion API keys
const NOTION_KEY=process.env.NOTION_KEY
const NOTION_DATABASE_ID=process.env.NOTION_DATABASE_ID
// Notion client
var {Client} =require("@notionhq/client");
const notion = new Client({ auth: NOTION_KEY })
// Function to write to Notion
async function addItem(text) {
try {
const response = await notion.pages.create({
parent: { database_id: NOTION_DATABASE_ID },
properties: {
title: {
title:[
{
"text": {
"content": text
}}]}},})
console.log(response)
console.log("Success! Entry added.")
} catch (error) {
console.error(error.body) }}
// Import the discord.js module
var { Client, Intents, MessageEmbed } = require('discord.js');
// Create an instance of a Discord client
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS] });
// Ready event
client.on('ready', () => {
console.log('I am ready!');
});