Discord.js();如何发送文件(它总是发送一个 0 字节的文件?)
Discord.js(); How to send file (It always sends a 0 byte file?)
我在使用 ytdl 下载后发送文件时遇到问题。我注意到这很奇怪,它会成功地将文件发送到服务器,前提是我手动将它命名为标题以外的名称......否则它只会向我的服务器发送一个 0 字节的文件。我在我的标题方案中尝试了 运行 各种字符串方法,认为这可能是错误的,但那没有用。我有一种预感,可能与以下内容有关:
https://github.com/hydrabolt/discord.js/issues/1907
但是伙计,我不知道我是无知的。任何帮助将不胜感激。以下是我的来源。我在发送文件时遇到问题的函数是命令 === 'ytm'
const Discord = require('discord.js'); //need discord.js library of course.
const config = require("./config.json"); //load up the token and prefix from our object configuration file.
const bot = new Discord.Client(); //establishing the bot as the client. "bot" means "client" when looking at the documentation!
bot.on('ready', () => {
console.log('I am ready!');
});
bot.on('message', message => {
if(message.author.bot) return; //prevents the bot from reacting to itself.
if(message.content.indexOf(config.prefix) !== 0) return; //reads out the first character of the message, and if its not our prefix we don't do break away
const args = message.content.slice(config.prefix.length).trim().split(/ +/g); //setting up to split things into arguments for handling commands, and usage of the prefix
const command = args.shift().toLowerCase();
if (command === 'ping') {
message.channel.send('pong');
//console.log(args);
//console.log(command);
}
if (command === 'ytm') {
const fs = require('fs');
const ytdl = require('ytdl-core');
url = args[0];
//console.log(ytdl.getURLVideoID(url));
//tried to actually work with the "title" information outside of the callback. apparently impossible?
ytdl.getInfo(url,function(err, info){
var title = info['title'];
var length = title.length;
title = title.substring(0, (length / 3));
title = title.trim();
title = './' + title + '.mp3'
console.log(title);
//console.log(info['size']);
ytdl(url, {filter:'audioonly', format:'mp3'}).pipe(fs.createWriteStream(title.toString()));
message.channel.send("Here's your mp3, boi.", {files:[(title)]});
//message.channel.sendFile(('./' + title), title, 'Heres your mp3, boi,'); //deprecated?
})
}
});
bot.login(config.token);
问题是您正试图将 title 变量作为文件发送,您已通过执行 title = './' + title + '.mp3'
明确设置为字符串。您的问题是您试图发送视频 stream 而不是视频 file。 ytdl-core 是一个明确用于 流式视频 而不是 下载视频 的模块。要获取和发送 MP3 文件,您必须改用 youtube-dl(ytdl-core 的基础)。这将在文件下载完成时发出一个事件,然后您可以通过 Discord.js.
发送该事件
我在使用 ytdl 下载后发送文件时遇到问题。我注意到这很奇怪,它会成功地将文件发送到服务器,前提是我手动将它命名为标题以外的名称......否则它只会向我的服务器发送一个 0 字节的文件。我在我的标题方案中尝试了 运行 各种字符串方法,认为这可能是错误的,但那没有用。我有一种预感,可能与以下内容有关: https://github.com/hydrabolt/discord.js/issues/1907
但是伙计,我不知道我是无知的。任何帮助将不胜感激。以下是我的来源。我在发送文件时遇到问题的函数是命令 === 'ytm'
const Discord = require('discord.js'); //need discord.js library of course.
const config = require("./config.json"); //load up the token and prefix from our object configuration file.
const bot = new Discord.Client(); //establishing the bot as the client. "bot" means "client" when looking at the documentation!
bot.on('ready', () => {
console.log('I am ready!');
});
bot.on('message', message => {
if(message.author.bot) return; //prevents the bot from reacting to itself.
if(message.content.indexOf(config.prefix) !== 0) return; //reads out the first character of the message, and if its not our prefix we don't do break away
const args = message.content.slice(config.prefix.length).trim().split(/ +/g); //setting up to split things into arguments for handling commands, and usage of the prefix
const command = args.shift().toLowerCase();
if (command === 'ping') {
message.channel.send('pong');
//console.log(args);
//console.log(command);
}
if (command === 'ytm') {
const fs = require('fs');
const ytdl = require('ytdl-core');
url = args[0];
//console.log(ytdl.getURLVideoID(url));
//tried to actually work with the "title" information outside of the callback. apparently impossible?
ytdl.getInfo(url,function(err, info){
var title = info['title'];
var length = title.length;
title = title.substring(0, (length / 3));
title = title.trim();
title = './' + title + '.mp3'
console.log(title);
//console.log(info['size']);
ytdl(url, {filter:'audioonly', format:'mp3'}).pipe(fs.createWriteStream(title.toString()));
message.channel.send("Here's your mp3, boi.", {files:[(title)]});
//message.channel.sendFile(('./' + title), title, 'Heres your mp3, boi,'); //deprecated?
})
}
});
bot.login(config.token);
问题是您正试图将 title 变量作为文件发送,您已通过执行 title = './' + title + '.mp3'
明确设置为字符串。您的问题是您试图发送视频 stream 而不是视频 file。 ytdl-core 是一个明确用于 流式视频 而不是 下载视频 的模块。要获取和发送 MP3 文件,您必须改用 youtube-dl(ytdl-core 的基础)。这将在文件下载完成时发出一个事件,然后您可以通过 Discord.js.