Mad_ Questionnaire
Mad_ Questionnaire

Reputation: 963

discord.js Send message as code block?

I'm trying to make a bot that will send a message to my channel, but in a code block because using RichEmbed doesn't work.

I looked some other bots and they send messages like this

```
  Their title
    Body text blah blah
```

I want to send something similar, however when I tried

var msg = ``` 
  Their Title
    Body text blah blah
```;

and

var msg = "```
  Their Title
    Body text blah blah
```";

These don't work.

const Discord = require("discord.js");
const bot = new Discord.Client();
const TOKEN = "MY_TOKEN_ID";

bot.on("message", function(message) {

    console.log(message.content);

    if ( message.author.equals(bot.user)) 
        return;

    message.channel.send(msg);



});

bot.login(TOKEN);

My code is above, any ideas how to send code blocks?

Upvotes: 4

Views: 22746

Answers (4)

Emmet_v15
Emmet_v15

Reputation: 21

If anyone is still looking, you can do this:

const { codeBlock } = require("@discordjs/builders");

<channel>.send(codeBlock("js", 'var foo = "bar";'));

Upvotes: 1

Greenman
Greenman

Reputation: 39

Kind of strange, but you can also do...

msg.channel.send( {
  content: "Please send this as a code block !",
  code: "js"
});

Upvotes: 0

tguichaoua
tguichaoua

Reputation: 173

function codeblock(
    language:
        | "asciidoc"
        | "autohotkey"
        | "bash"
        | "coffeescript"
        | "cpp"
        | "cs"
        | "css"
        | "diff"
        | "fix"
        | "glsl"
        | "ini"
        | "json"
        | "md"
        | "ml"
        | "prolog"
        | "py"
        | "tex"
        | "xl"
        | "xml",
    code: string,
) {
    return `\`\`\`${language}\n${code}\`\`\``;
}

Usage

const msg = codeblock("css", `
#element {
    width: 500 px;
}
.button {
    width: 300 px;
}
`);

Upvotes: 2

Siki
Siki

Reputation: 114

Have you tried using this?

var msg = "```Their Title\nBody text blah blah```";

\n is a new line, it's basically pressing ENTER when writing. You can send it as normal text message afterward.

Upvotes: 10

Related Questions