【问题标题】:Discord.js Writing to a JSON File with Channel MessageDiscord.js 使用通道消息写入 JSON 文件
【发布时间】:2021-10-20 17:37:58
【问题描述】:

我正在尝试使用 discord-fetch-all 为我的机器人创建一个 -transcript 命令,因为我认为这比一次获取 100 条消息更容易(我也不知道该怎么做)。我让我的代码正常工作,因为它将所有内容都记录到我的 ticket.json 文件中,但它完全是一团糟!我真的不知道如何格式化它(我认为它不是 npm 的一部分,对吧?)。所以我想知道我是否可以得到一些帮助?我真正希望它记录的只是TIMESTAMP | AUTHOR: "MESSAGE'我当前的代码:

const Discord = require(`discord.js`);
const fetchAll = require('discord-fetch-all');
const fs = require('fs');
const path = require('path');


module.exports = {
    name: 'transcript',
    description: "logs the ticket transcript!",
    
    async execute(message, args) {

        const channel = message.channel

        const allMessages = await fetchAll.messages(channel, {
            reverseArray: true, 
            userOnly: false, 
            botOnly: false, 
            pinnedOnly: false, 
        });

        fs.writeFileSync(path.resolve(__dirname, `ticket`), JSON.stringify(allMessages));
    }}

以及我的 ticket.json 的示例:

[{"channelID":"864239107161063434","deleted":false,"id":"864239107592290314","type":"DEFAULT","system":false,"content":"**Channel Nuked**","authorID":"844924130759409685","pinned":false,"tts":false,"nonce":null,"embeds":[],"attachments":[],"createdTimestamp":1626121060036,"editedTimestamp":0,"webhookID":null,"applicationID":null,"activity":null,"flags":0,"reference":null,"guildID":"783070743209705494","cleanContent":"**Channel Nuked**"},}

除了频道中的每条消息都有一个;(

感谢所有帮助...如果有更好的方法可以做到这一点,请随时告诉我。

【问题讨论】:

  • 您可以简单地提取 allMessages 数组中每条消息的时间戳、作者和内容,并将它们输出到一个文件中。
  • 我该怎么做?它会进入 const allmessages 吗?

标签: javascript discord discord.js format


【解决方案1】:

你应该循环allMessages变量,然后你可以用任何你喜欢的方式格式化它,例如

const results = []
for(let i in array) {
  results.push({
    timestamp: i.timestamp,
    author: i.author,
    message: i.message,
  });
}
fs.writeFileSync(path.resolve(__dirname, `ticket`), JSON.stringify(results));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-04
    • 2021-04-22
    • 2021-12-25
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 2020-09-16
    • 2021-10-14
    相关资源
    最近更新 更多