【问题标题】:discord.js v12 / 'must be 2048 or fewer characters in length'discord.js v12 / '长度不能超过 2048 个字符'
【发布时间】:2021-06-23 06:42:04
【问题描述】:

我在格式化时遇到了“麻烦”,因为我专门为其制作这个机器人的当前公会有太多禁止机器人格式化的禁令,因此错误是它必须是 '2048 或更少的字符长度',

代码仅在字符不超过该长度时才有效。

谁能帮忙?

const { MessageEmbed, Message } = require('discord.js');

module.exports.run = async(client, message, args, getMember) => {
    if (!message.guild.me.hasPermission("BAN_MEMBERS")) return;

    message.guild.fetchBans()
    .then(bans => {
        const obj = bans.map(c => ({
            user: `__${c.user.tag}__ — (${c.user.id})`
        }));

        const blist = Array.from(obj);
        if ( blist.length < 1 ) return message.channel.send(new MessageEmbed()
        .setColor('#030303')
        .setDescription(`there are no banned users — ${message.guild.name}`))
        let index = 0;

        message.channel.send({
            embed: new MessageEmbed()
            .setTitle(`ban list`)
            .setDescription(`${blist.map(bl => bl.user).join('\n')}`)
            .setColor('#030303')
            .setTimestamp()
        })
    })
}

module.exports.config = {
    name: 'fetchbans',
    aliases: ["bans", "fb"]
}

【问题讨论】:

  • 您需要发送多条消息或对一个列表进行分页
  • 我是这方面的初学者,我知道分页是什么,但我不知道分页是什么,你能帮忙吗?

标签: javascript discord discord.js


【解决方案1】:

如 cmets 中所述,您需要将大文本拆分为多个部分,然后您可以使用分页来循环浏览它们。

因此,为了完成这项工作,我们将从分割文本开始。一种方法是将整个文本拆分为一个数组,然后将该数组拆分为 2048 个字符的块。

const text = "some text"; // this is your large text.
const array = text.split(''); // here we split the text with each char being a new element.
const largeArray = []; // this is the array we will place the batches in.

var i, j, temparray, chunk = 2048; // here we define some variables and also the length of each batch.
// now we iterate over the array and split it into the defined batches
// then we join them back together and place them in the new array
for (i = 0, j = array.length; i < j; i += chunk) {
    temparray = array.slice(i, i + chunk);
    largeArray.push(temparray.join(''));
}

现在我们有一个数组,每个元素的长度不超过 2048 个字符。现在我们需要创建一个页面解析器。我们在这里这样做的方式是反应。所以首先我们需要创建一个函数来处理我们的反应。

// this needs to be an async function since we need to await the reactions
// using the author here ensures that only the person who sends the command
// can leaf through the pages
async function reactMessage(message, author, time, validReactions) {
    // the time is converted to milliseconds
    time *= 1000;

    // now we cycle through the valid reactions and add them to the message
    for (const reaction of validReactions) await message.react(reaction);

    // now we use a reaction filter to grab the used reaction
    const filter = (reaction, user) => validReactions.includes(reaction.emoji.name) && user.id === author.id;
    
    // here we return the reaction used
    return message
        .awaitReactions(filter, { max: 1, time: time })
        .then(collected => collected.first() && collected.first().emoji.name);
}

现在我们有一个函数来处理我们的反应,我们需要创建一个处理分页的函数。此函数将使用先前的函数来创建所需的反应。这是一个更大的,所以留在我身边。基本上,它的作用是获取一个迭代器和数组长度,以所需的反应对消息做出反应,然后返回迭代器以再次使用。

async function pageParser(message, msg, i, time, chooseArr, array_length) {
    // create the reaction arrays for the first and last pages
    const chooseArrfirst = [chooseArr[1], chooseArr[2]];
    const chooseArrlast = [chooseArr[0], chooseArr[1]];

    // this part handles the first page
    if (i === 0) {
        var reaction = await reactMessage(msg, message.author, time, chooseArrfirst);

        if (reaction === chooseArr[2]) {
            msg.delete();
            i++;
            return i;
        } else if (reaction === chooseArr[1]) {
            i = array_length + 1;
            msg.delete();
            return;
        }
    // this part handles the last page
    } else if (i === array_length - 1) {
        var reaction = await reactMessage(msg, message.author, time, chooseArrlast);

        if (reaction === chooseArr[0]) {
            msg.delete();
            i--;
            return i;
        } else if (reaction === chooseArr[1]) {
            i = array_length + 1;
            msg.delete();
            return;
        }
    // this part handles the pages in the middle
    } else {
        var reaction = await reactMessage(msg, message.author, time, chooseArr);

        if (reaction === chooseArr[0]) {
            msg.delete();
            i--;
            return i;
        } else if (reaction === chooseArr[2]) {
            msg.delete();
            i++;
            return i;
        } else if (reaction === chooseArr[1]) {
            i = array_length + 1;
            msg.delete();
            return;
        }
    }
}

太棒了!现在我们已经拥有了我们需要的所有组件,让我们将它们放在一起。 首先,我们创建迭代器和包含反应的数组。

var i = 0; // this is the iterator
var a = true;
const chooseArr = ['◀', '⏹', '▶']; // these are the reactions in order

现在我们使用while 循环来确保页面得到更新。还记得之前的largeArray 吗?现在我们再次需要它。我们创建一个嵌入并将描述设置为数组中的迭代器索引。这会更新描述。然后我们可以发送消息并使用我们的pageParser 函数。

while (a && i < largeArray.length) {
    const embed = new Discord.MessageEmbed()
        .setDescription(largeArray[i])

    // since our function returns something, the iterator, we need to overwrite
    // the previous iterator
    i = await message.channel.send(embed).then(msg => pageParser(message, msg, i, 240000, chooseArr, largeArray.length))
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-28
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多