Hide commands for Discord bot from text chat

Hello everyone,

I’m wondering if there’s a simple way to have the Discord channel hide all of the commands for a Discord bot? It’d be beneficial if people could still type the commands in the text chat and instruct the bot, but have the actual text of the commands be hidden after they’re posted, like an auto-hide feature. This may not be possible and that’s okay. The bot’s responses can be visible obviously, just trying to hide the commands themselves.

Let me know - thank you in advance to anyone that writes in.

1 Like

Hey again, in order to “hide” a message containing a command, that message must be deleted. The discord.io library has a deleteMessage() function, giving you the capability of deleting a user’s message before or after the bot handles the command.

Here’s an example of how that’d be done with a certain command. I will include comments in the code:

bot.on("message", function (user, userID, channelID, message, evt) {
    // If the message equals "test"
    if (message == "test") {
        // This will delete the command message prior to the bot sending the command result
        bot.deleteMessage(message);
        bot.sendMessage({
            to: channelID,
            message: "hello"
        });
    }
});

This method should work assuming the discord.io library does not require a message I.D. to be passed through the function. The discord.io library hasn’t been updated in over two years, so it has been archived and valid documentation no longer exists.

I hope this helps! If you have any questions please don’t hesitate to ask!

Hey man, thanks again for the help. Would you mind pasting this snippet into my template from our last thread so I can save it into my file for a successful upload? This would help me out a lot if the commands were auto-deleted before the bot response is sent. Happy to give you another “solved” marking on this if you could incorporate the snippet into my template.

1 Like

No problem! I added the deletion command to the “/hi” command and also made the fixes that @lee-dohm corrected in this topic. Be aware that the replies array in this snippet does not have your custom replies, so make sure you make a note of the current replies you have in your code.

var Discord = require(‘discord.io’);
var logger = require(‘winston’);
var auth = require(’./auth.json’);

// Configure the array used for random replies
let replies = ["reply 1", "reply 2", "reply 3"];

// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(new logger.transports.Console, {
    colorize: true
});
logger.level = ‘debug’;

// Initialize Discord Bot
var bot = new Discord.Client({
    token: auth.token,
    autorun: true
});

bot.on(‘ready’, function (evt) {
    logger.info(‘Connected’);
    logger.info(‘Logged in as: ‘);
    logger.info(bot.username + ’ - (’ + bot.id + ‘)’);
});
bot.on(‘message’, function (user, userID, channelID, message, evt) {
    // Configure the randomizer that will pick a random integer from 0 to the length of the array; used for array index
    let random = Math.floor(Math.random() * replies.length);

    // Our bot needs to know if it will execute a command
    // It will listen for messages that will start with /
    if (message.substring(0, 1) == ‘/’) {
        var args = message.substring(1).split(' ');
        var cmd = args[0];

        args = args.splice(1);
        switch(cmd) {
            // /Hi
            case 'hi':
                // Delete the command message before having the bot execute the command
                bot.deleteMessage(message);

                bot.sendMessage({
                    to: channelID,
                    message: replies[random]
                });
        break;
        // Just add any case commands if you want to..
         }
     }
});

Hope this works out for you. Once again, this may not work depending on whether or not the deleteMessage() requires a message I.D. as an argument. Let me know what happens!

The code uploads successfully, so nothing wrong there, it doesn’t delete the command though prior to sending the bot response. Is there a work-around for this or are we out of luck?

1 Like

It seems that the deleteMessage() function possibly requires a message I.D. in order to delete a message. However, this happens to be an easy fix thanks to the message event that is returned from the function. Here’s the entire snippet that should fix this issue (of course remember that you need to specify your custom replies again):

var Discord = require(‘discord.io’);
var logger = require(‘winston’);
var auth = require(’./auth.json’);

// Configure the array used for random replies
let replies = ["reply 1", "reply 2", "reply 3"];

// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(new logger.transports.Console, {
    colorize: true
});
logger.level = ‘debug’;

// Initialize Discord Bot
var bot = new Discord.Client({
    token: auth.token,
    autorun: true
});

bot.on(‘ready’, function (evt) {
    logger.info(‘Connected’);
    logger.info(‘Logged in as: ‘);
    logger.info(bot.username + ’ - (’ + bot.id + ‘)’);
});
bot.on(‘message’, function (user, userID, channelID, message, evt) {
    // Configure the randomizer that will pick a random integer from 0 to the length of the array; used for array index
    let random = Math.floor(Math.random() * replies.length);

    // Our bot needs to know if it will execute a command
    // It will listen for messages that will start with /
    if (message.substring(0, 1) == ‘/’) {
        var args = message.substring(1).split(' ');
        var cmd = args[0];

        args = args.splice(1);
        switch(cmd) {
            // /Hi
            case 'hi':
                // Delete the command message before having the bot execute the command
                bot.deleteMessage(evt.d.id);

                bot.sendMessage({
                    to: channelID,
                    message: replies[random]
                });
        break;
        // Just add any case commands if you want to..
         }
     }
});

Hopefully this will work now!

Ah, no, the command still remains in the chat, it’s not auto-deleting before the bot responds. Any other ideas? If not, no worries, I really appreciate your hard work.

1 Like

In your code where it uses bot.deleteMessage(evt.d.id) , replace it with this:

bot.deleteMessage({
    channelID: channelID,
    messageID: evt.d.id
});

The documentation is very poor for the discord.io package since it is no longer being developed.

That worked! Fantastic! Thank you!

1 Like

No problem! Glad to hear it worked out!

1 Like