Search code examples
javascriptdiscorddiscord.js

Discord - Automatically extract a message immediately after it appears in a channel?


I am yet to code this but actually struggling to understand how to do this

There is a private server with paid membership. I am a member. There are many channels on that server. I am particularly interested in a specific channel. Once someone posts a message in that channel, I want to immediately extract that message from a different program automatically and then process it in downstream systems. How to do that?


Solution

  • I know I'm a bit late to this question, but I'm going to tell you the secrets the Discord elites don't want you to know: This is completely doable, and many people do it. It's called a "self-bot".

    Disclaimer: As the other answer stated, this is against ToS. You probably won't be caught, but I'd keep this on the down-low. Definitely don't go around telling this to everyone you meet.

    Ok, now here's the actual coding part:

    Discord.js, the most common library for discord bots in JavaScript, no longer supports self-bots, so you'll need to use an older version of discord.js. The official old versions have some unresolved bugs when used with modern discord, so I like to go with discord.js.v11.patch. Using this patched package will fix any weird bugs you get. Note: this is still discord.js v11, so if you need documentation, be sure to look at the v11 docs.

    Ok, so after you run npm install discord.js.v11.patch (or npm install -g discord.js.v11.patch if you want to install it globally), you'll need to start writing code. Everything is basically the same as any old discord.js bot, but this is v11 so some stuff might be different. Here's some code to get you started. It should do everything you want it to do:

    const discord = require('discord.js.v11.patch');
    const client = new discord.Client();
    
    const USER_TOKEN = 'XXXXXXXXXXXXXX'; // change this to your token
    const CHANNEL_ID = 'XXXXXXXXXXXXXX'; // change this to the chanel you want to listen to.
    
    client.on('ready', () => {
        console.log('bot is running');
    });
    
    client.on('message', msg => {
        if (msg.channel.id != CHANNEL_ID) return;
        const message_text = msg.content;
    
        console.log(message_text); // just an example
        // send message_text somewhere to process it.
    });
    
    client.login(USER_TOKEN);
    

    Now, all you need to do is change USER_TOKEN to your discord token and CHANNEL_ID to the ID of the channel you want to listen to.

    To get your token, I recommend using this gist. It is safe and minimal. If you are worried about accessing your token, don't be. As long as you don't give it to anyone else you're fine.

    To get the channel ID, simply turn on developer mode, then right-click on the channel you want to listen to. You should see a Copy ID menu button at the bottom of the context menu.