基本 FTP 配置

问题描述 投票:0回答:1

我正在尝试使用 basic-ftp 将文件上传到 FTP 服务器。我想做的是通过“discord-html-transcripts”(我已配置)创建一个不和谐频道记录,但随后我希望将其上传到脚本无法理解的 FTP 服务器。

我尝试过使用许多不同的项目,例如创建缓冲区、删除缓冲区,但它似乎仍然不起作用。

代码

const { Colors } = require("discord.js");
const { Client } = require("basic-ftp");
const config = require("../../configuration");
const ticketTranscript = require("discord-html-transcripts");
const enquiriesNotifications = config.enquiriesNotifications;

module.exports = {
  data: {
    id: "enquiry-transcript",
  },
  async execute(interaction) {
    const channelName = interaction.channel.name;
    const transcriptName = `${channelName}_${Date.now()}.html`;

    const clientFtp = new Client();
    try {
      await clientFtp.access({
        host: "ftp.southlondonroleplay.co.uk",
        port: 21,
        user: "InspectorColeAccess",
        password: "1234567890",
      });

      console.log("[File Transfer] Uploading transcript to FTP server...");

      // Create the transcript content
      console.log("[Debugging Information] Creating transcript...");
      const transcriptAttachment = await ticketTranscript.createTranscript(
        interaction.channel,
        {
          limit: -1,
          returnType: "attachment",
          filename: `${transcriptName}`,
          saveImages: true,
          footerText: "Central London Roleplay",
          poweredBy: false,
        }
      );
      console.log(
        "[Debugging Information] Transcript created:",
        transcriptAttachment
      );

      // Extract transcript content
      const transcriptContent =
        transcriptAttachment.attachment.toString("utf-8");
      console.log(
        "[Debugging Information] Transcript content:",
        transcriptContent
      );

      // Upload the transcript to the FTP server
      await clientFtp.uploadFrom(
        Buffer.from(transcriptContent),
        `/PortalTickets/${transcriptName}`
      );

      console.log("[File Transfer] Transcript uploaded successfully");

      const transcriptLink = `https://clrp.co.uk/enquirytranscripts/${transcriptName}`;

      // Send message to the enquiriesNotifications channel with transcript link
      await interaction.guild.channels.cache.get(enquiriesNotifications).send({
        embeds: [
          {
            title: "Enquiry Closed",
            description:
              "A community enquiry has been closed with a transcript. Details relating to this action are reflected below:",
            fields: [
              {
                name: "Closed by",
                value: `${interaction.user}`,
                inline: true,
              },
              {
                name: "Enquiry Channel",
                value: `${interaction.channel.name}`,
                inline: true,
              },
              {
                name: "Enquiry Transcript",
                value: `${transcriptLink}`,
                inline: true,
              },
            ],
            color: Colors.Red,
            footer: {
              text: "Central London Roleplay",
            },
            timestamp: new Date(),
          },
        ],
        files: [
          {
            attachment: Buffer.from(transcriptContent),
            name: transcriptName,
          },
        ],
      });

      // Send confirmation message to the enquiry channel
      await interaction.channel.send({
        embeds: [
          {
            title: "Enquiry Deletion",
            description: `This enquiry has been closed by ${interaction.user}\n\nAll Transcripts are saved and stored within our Discord notification channels and Community Portal for training and audit purposes. All data is processed in accordance with our Data Privacy & Protection Policy.`,
            color: Colors.Red,
            footer: {
              text: "Central London Roleplay",
            },
            timestamp: new Date(),
          },
        ],
      });

      // Delete the enquiry channel after 5 seconds
      setTimeout(() => {
        interaction.channel.delete();
      }, 5000);
    } catch (error) {
      console.error(
        "[Debugging Information] Error Uploading Transcript to the Portal",
        error
      );
      // Handle error gracefully, inform the user, and log the error
      await interaction.reply({
        content: "An error occurred while processing your request.",
        ephemeral: true,
      });
    } finally {
      // Close the FTP connection
      await clientFtp.close();
    }
  },
};

错误

[Debugging Information] Error Uploading Transcript to the Portal TypeError: source.once is not a function
    at Client._uploadFromStream (F:\Stuffs\CentralLondon\node_modules\basic-ftp\dist\Client.js:391:16)
    at Client._uploadWithCommand (F:\Stuffs\CentralLondon\node_modules\basic-ftp\dist\Client.js:366:21)
    at Client.uploadFrom (F:\Stuffs\CentralLondon\node_modules\basic-ftp\dist\Client.js:347:21)
    at Object.execute (F:\Stuffs\CentralLondon\communityBot\communityComponents\clientButtons\enquiry-transcript.js:53:23)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Object.execute (F:\Stuffs\CentralLondon\communityBot\communityEvents\clientEvents\interactionCreate.js:28:9)
node.js discord
1个回答
0
投票

似乎您正在从

Buffer
创建
string
,而 uploadFrom 需要流。

const { Readable } = require('stream');

const stream = Readable.from(string); // converts string to a readable stream

简而言之,您正在寻找

const { Readable } = require('stream');
...

await clientFtp.uploadFrom(
  Readable.from(transcriptContent),
  `/PortalTickets/${transcriptName}`
);

注意:您还可以将缓冲区传递给

Readable.from

有关 Readable.from 的更多信息 https://nodejs.org/api/stream.html#streamreadablefromiterable-options

需要流的类似错误 https://github.com/search?q=repo%3Apatrickjuchli%2Fbasic-ftp%20source.once&type=issues

© www.soinside.com 2019 - 2024. All rights reserved.