Skip to content

How to use syntax highlighting for code Blocks for Bot in discord.js (v13)?

I am trying to make a terminal accessible in the discord using the bot. The code for this command is :

const { Client, Message, MessageEmbed } = require("discord.js");
const child = require("child_process");

module.exports = {
  name: "terminal",
  /**
   * @param {Client} client
   * @param {Message} message
   * @param {String[]} args
   */
  run: async (client, message, args) => {
    if (message.author.id !== "") return;

    const command = args.join(" ");

    const errorCommand = new MessageEmbed();

    errorCommand
      .setColor("RED")
      .setDescription("Please specify a command to execute");

    if (!command) return message.reply({ embeds: [errorCommand] });

    child.exec(command, (err, res) => {
      if (err) return console.log(err);
      message.channel.send(` ```${res.slice(0, 2000)}``` `, {
        code: "js",
      });
    });
  },
};

When i run the command the syntax highlighting isn’t displayed in the code Block. I have attached a picture of what i meant.

Image 1

Image 2

Answer

I found the answer. Sorry for the inconvenient post, In case someone needs the answer, it is :

const { Client, Message, MessageEmbed } = require("discord.js");
const child = require("child_process");

module.exports = {
  name: "terminal",
  /**
   * @param {Client} client
   * @param {Message} message
   * @param {String[]} args
   */
  run: async (client, message, args) => {
    if (message.author.id !== "") return;

    const command = args.join(" ");

    const errorCommand = new MessageEmbed();

    errorCommand
      .setColor("RED")
      .setDescription("Please specify a command to execute");

    if (!command) return message.reply({ embeds: [errorCommand] });

    child.exec(command, (err, res) => {
      if (err) return console.log(err);
      const jsString = "const value = true;";

      message.channel.send(` ```jsn${res.slice(0, 2000)}``` `);
    });
  },
};

If u want to use another language syntax highlighting u can just replace js of here message.channel.send(` ```jsn${res.slice(0, 2000)}``` `); with your preference.