Build a Discord Bot with AI Memory

Most Discord bots forget everything between messages. This tutorial shows how to build a Discord bot with persistent AI memory using discord.js and REM Labs. Your bot will remember user preferences, past conversations, and server-specific context -- making every interaction smarter than the last.

Prerequisites

Project Setup

mkdir discord-memory-bot && cd discord-memory-bot npm init -y npm install discord.js @remlabs/sdk openai

The Bot Code

// bot.js import { Client, GatewayIntentBits } from "discord.js"; import { RemLabs } from "@remlabs/sdk"; import OpenAI from "openai"; const discord = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent ] }); const memory = new RemLabs({ apiKey: process.env.REMLABS_API_KEY }); const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY }); discord.on("messageCreate", async (msg) => { if (msg.author.bot) return; if (!msg.mentions.has(discord.user)) return; const query = msg.content.replace(/<@!?\d+>/g, "").trim(); const namespace = `discord-${msg.guild.id}`; // Recall relevant memories for this server const memories = await memory.recall({ query, namespace, limit: 5 }); const context = memories .map(m => m.content) .join("\n"); // Generate response with memory context const completion = await openai.chat.completions.create({ model: "gpt-4o", messages: [ { role: "system", content: `You are a helpful bot. Context from memory:\n${context}` }, { role: "user", content: query } ] }); const reply = completion.choices[0].message.content; await msg.reply(reply); // Store this interaction as a memory await memory.remember({ content: `${msg.author.username} asked: ${query} Bot replied: ${reply}`, namespace, tags: ["discord", msg.channel.name] }); }); discord.login(process.env.DISCORD_TOKEN);

Per-User Memory

To give each user their own memory space, use user-scoped namespaces:

// Per-user namespace const namespace = `discord-${msg.guild.id}-${msg.author.id}`; // Or combine server + user context const serverMemories = await memory.recall({ query, namespace: `discord-${msg.guild.id}`, limit: 3 }); const userMemories = await memory.recall({ query, namespace: `discord-user-${msg.author.id}`, limit: 3 });

Slash Commands

Add slash commands for explicit memory management:

// /remember command if (interaction.commandName === "remember") { const note = interaction.options.getString("note"); await memory.remember({ content: note, namespace: `discord-${interaction.guild.id}`, tags: ["manual", interaction.user.username] }); await interaction.reply("Remembered."); } // /recall command if (interaction.commandName === "recall") { const query = interaction.options.getString("query"); const results = await memory.recall({ query, namespace: `discord-${interaction.guild.id}`, limit: 5 }); const text = results.map(m => `- ${m.content}`).join("\n"); await interaction.reply(text || "No memories found."); }

Scaling: REM Labs handles the memory infrastructure. Whether your bot is in 10 servers or 10,000, each server gets its own namespace with instant recall. No database setup required on your end.

Build a bot that remembers

Free tier. 1,000 memories. Works with any Discord.js setup.

Get started free →