【问题标题】:Adding '...' to end of message.content if char limit is over 2045如果字符限制超过 2045,则将“...”添加到 message.content 的末尾
【发布时间】:2021-02-10 04:54:27
【问题描述】:

我想做一些事情来防止整个消息不被发送,就像机器人在没有成功发送所述消息时抛出错误一样。

说消息是:

Hello world, I am Johnty *1000+ more chars here*

我希望它能够这样做:

Hello world, I am Johnty *500+ more chars here*...

const embed = new Discord.MessageEmbed()
.setDescription(`${message.content}`);

【问题讨论】:

    标签: node.js discord discord.js


    【解决方案1】:

    非常简单,直到一位 Plexi Development 成员向我展示了如何操作。

    谢谢Cats Are Awesome#3153

    代码:

    const trim = (str, max) => (str.length > max ? `${str.slice(0, max - 3)}...` : str);
    

    用法:

    const Discord = require("discord.js");
    const trim = (str, max) => (str.length > max ? `${str.slice(0, max - 3)}...` : str);
    const embed = new Discord.MessageEmbed()
    .setDescription(`${trim(message.content, 500)}`);
    message.channel.send(embed);
    

    【讨论】:

      猜你喜欢
      • 2022-08-18
      • 2012-06-10
      • 1970-01-01
      • 2012-08-03
      • 2011-03-16
      • 2021-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多