久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

讀取文件附件(例如;.txt 文件)- Discord.JS

Reading file attachments (Ex; .txt file) - Discord.JS(讀取文件附件(例如;.txt 文件)- Discord.JS)
本文介紹了讀取文件附件(例如;.txt 文件)- Discord.JS的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

第二次在 StackOverflow 上發帖,如有錯誤,我深表歉意.請多多包涵.

與標題相同;您如何閱讀不和諧附件的內容,比如 .txt 文件并打印內容?

我嘗試使用 fs 但不幸失敗了,我也搜索了文檔但也失敗了.

想法?

解決方案

您不能為此使用 fs 模塊,因為它只處理本地文件.當您將文件上傳到 Discord 服務器時,它會被上傳到 CDN,您所能做的就是從

Second time posting on StackOverflow so I apologize for any mistakes. Please bear with me.

Same with the title; How do you read contents of a discord attachment let's say a .txt file and print the contents?

I have tried with fs but unfortunately failed and I have also searched the documentation but failed also.

Ideas?

解決方案

You can't use the fs module for this as it only deals with local files. When you upload a file to the Discord server, it gets uploaded to a CDN and all you can do is grab the URL of this file from the MessageAttachment using the url property.

If you need to get a file from the web, you can fetch it from a URL using the built-in https module, or you can install one from npm, like the one I used below, node-fetch.

To install node-fetch, run npm i node-fetch in your root folder.

Check out the working code below, it works fine with text files:

const { Client } = require('discord.js');
const fetch = require('node-fetch');

const client = new Client();

client.on('message', async (message) => {
  if (message.author.bot) return;

  // get the file's URL
  const file = message.attachments.first()?.url;
  if (!file) return console.log('No attached file found');

  try {
    message.channel.send('Reading the file! Fetching data...');

    // fetch the file from the external URL
    const response = await fetch(file);

    // if there was an error send a message with the status
    if (!response.ok)
      return message.channel.send(
        'There was an error with fetching the file:',
        response.statusText,
      );

    // take the response stream and read it to completion
    const text = await response.text();

    if (text) {
      message.channel.send(````${text}````);
    }
  } catch (error) {
    console.log(error);
  }
});

這篇關于讀取文件附件(例如;.txt 文件)- Discord.JS的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
主站蜘蛛池模板: 亚洲婷婷一区 | 国产成人精品一区二区三区视频 | 一级片在线观看 | 欧美久久一级特黄毛片 | 日韩欧美不卡 | 国产精品2区 | 一区在线免费视频 | 日韩欧美一区在线 | 黄色免费在线观看 | 高清国产一区二区 | 亚洲高清视频一区二区 | 亚洲精品三级 | 天天射色综合 | 国产99久久精品一区二区永久免费 | 羞羞免费网站 | 爱爱爱av| 国产精品3区 | 欧美日韩一区二区三区在线观看 | www.伊人.com| 亚洲欧美视频在线观看 | 中文字幕一区二区三区精彩视频 | 亚洲免费在线 | av在线免费观看网站 | 亚洲日本三级 | 国产精品久久精品 | 国产精品一二三区 | 国产中文字幕在线 | 一区二区成人 | 中文字幕 在线观看 | 亚洲成av人片在线观看 | 日韩在线视频播放 | 俺去俺来也www色官网cms | 91在线免费视频 | www亚洲精品| 久久国产成人 | 色狠狠一区| 久久久久久一区 | 国产一区二区三区欧美 | 国产日韩欧美激情 | 国产成人精品综合 | 五月槐花香 |