最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - discord.js Send message as code block? - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited Jun 1, 2017 at 4:11 Mad_ Questionnaire asked May 31, 2017 at 11:53 Mad_ Questionnaire Mad_ Questionnaire 9733 gold badges10 silver badges11 bronze badges 3
  • In your second example where you did var msg " " it doesn't have an equals sign. Is that how it is in your code or you left it out for this example. – Wright Commented Jun 1, 2017 at 1:09
  • I just left it out in the example, fixed it now. – Mad_ Questionnaire Commented Jun 1, 2017 at 4:12
  • "Doesn't work" isn't very helpful. What are you expecting vs. what you're actually seeing? – André Dion Commented Jun 1, 2017 at 12:25
Add a comment  | 

4 Answers 4

Reset to default 10

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.

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;
}
`);

If anyone is still looking, you can do this:

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

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

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

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

评论列表(0)

  1. 暂无评论