Discord.js - How to get last message in channel ?
Discover bepo

Discord.js - How to get last message in channel ?

Published on 
September 7, 2022

Hello fellow developer, and welcome to RTFM, the blog listing that explains whatever you're too lazy to find in the documentation.

In this article, we answer an apparently common question related to the popular Discord.js library:

How to get last message in channel? 

For those looking for a shortcut right into the documentation, here is your wrapped gift:
How to get last message in channel?

For the others, let's look into it together!

What is Discord and how does it work?

If you're here looking for how to get the last message in a channel, you most likely already know what Discord is.
However, it doesn't necessarily mean you know how it works and how you can leverage it properly.

By reading their about page, we get a brief grasp of what Discord is:

Discord is a voice, video and text communication service used by over a hundred million people to hang out and talk with their friends and communities.

Now, let's dive a little more.

On Discord, people usually communicate by video and text inside a Guild (or Server).
Guild are like communities of people with the same shared interest and are manually created by Discord users.

Each Guild has both voice and text communication channels, that members can use to communicate with one another.

Sometimes, you encounter some weird users able to interact faster than light with your answers, do things immediately when you react to their message.
Well, the reason for that is that they are softwares developed on top of Discord, and the generic term for this kind of users is Bot.

Discord.js is a JavaScript library meant to help creating those Discord bots.
More generally speaking, it's a wrap around Discord API in order to manipulate common mechanisms used by Discord such as channels, messages, guilds and so on.

Designing a solution

Before diving into the documentation, we need to take some time to think about what we are doing, why we are doing it and what would be the most straightforward way to achieve a meaningful result.

Meaningful result is the idea we should get some code working before trying to optimise anything or think about solving problems that might appear later.
This is a simple application of the YAGNI principle, and in practice, it means we are looking for a quick win: minimum amount of effort producing the best result.

What are we doing: We are trying to get last message in a Discord channel.

Why are we doing it: We want to pull out each last message from Discord and store it/display it on another medium (website, screen, ...).

What is the quick win: Connect to the Guild => Get channel information => Read last message of channel => Store
Our quick win automatically poses two constraints:

  • The channel must be a text channel
  • The library and/or the API allows fetching the last message directly

The first one is an assumption, so it's ok. The second one must be verified in the documentation, so now you can dive into it.

I'd recommend you get some practice and try to find the information by yourself before clicking on this link.
Being able to read a documentation and navigate through it is by far more important than being able to write complex code, as you read code far more often that you write it in your day to day job.

How to get last message in channel?

Here is a very simple code extract you can inspire yourself with to solve the problem you are facing.

⚠️ This is NOT production ready and you should not use this code as it is.
Always look for flaws in the code you will find elsewhere, in particular when it's meant to be educative.
For the sake of learning ,we often need to bypass some important notions that only happen in deployed environments. ⚠️

import { Client, Intents } from "discord.js";

const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

client.once("ready", () => {
  console.log("Discord client ready ✅.");
  // Access your server data thanks to your GuildID
  const server = client.guilds.cache.get(process.env.GUILD_ID!)!;
  // Access your channel thanks to its channelId
  const channel = client.channels.cache.get(process.env.MY_CHANNEL_ID!) as TextChannel;
  const message = channel.lastMessage;
  // Store inside a db, send through the network to another application...


axel founder of bepo
by Axel
Founder of Bepo

Founder @bepohq building products with ♥️ I also share my journey as an indiehacker on social media.


Read our other articles

Continue reading the same serie