gpt4 book ai didi

javascript - 让 Discord Bot 接收文件并将内容发送到 channel

转载 作者:太空宇宙 更新时间:2023-11-04 01:47:39 26 4
gpt4 key购买 nike

所以我想做到这一点,如果我这样做:

  1. 我在 channel 中使用 !command..
  2. (机器人要求提供文件)
  3. 我发送文件

然后机器人应该读取文件并发送内容。

我尝试过:

await message.channel.send("Send your file please..");

const MSG1 = await message.channel.awaitMessages(msg => {
const buffer = fs.readFileSync(`${msg.url}`);

message.channel.send('Read the file! Fetching data...');
message.channel.send(buffer)
})

await message.channel.send("Send your file please..");

const MSG1 = await message.channel.awaitMessages(msg => {
fs.readFile(Buffer.from(MSG1.attachments.first()), 'utf8', function(err, data) {
if (err) throw err;

console.log('Read the file! Fetching data...');
console.log(data);
})
})

await message.channel.send("Send your file please..");

const MSG1 = await message.channel.awaitMessages(msg => {
fs.readFile(Buffer.from(`${msg.attachments.first().attachment}`), 'utf8', function(err, data) {
if (err) throw err;

console.log('Read the file! Fetching data...');
console.log(data);
})
})

await message.channel.send("Send your file please..");

const MSG1 = await message.channel.awaitMessages(msg => {
let buffer = Buffer.from(msg.attachments.first().attachment)

message.channel.send('Read the file! Fetching data...');
message.channel.send(buffer)
})

但是这些都不起作用。

最佳答案

我发现您正在使用 TextChannel.awaitMessages()就好像它有一个回调参数,但您传递的函数应该是 filter 。不要使用 awaitMessages(function),而是使用 awaitMessages().then(function)

话虽如此,我还建议使用过滤器并仅在作者相同时才获取附件:

let filter = m => m.author == message.author; //use that message only if the author is the same
message.channel.awaitMessages(filter, {max: 1}).then(msg => {
let file = msg.attachments.first().file;
fs.readFile(file, (err, data) => {
msg.channel.send("Read the file! Fetching data...");
msg.channel.send(data);
});
});

应该可以,如果您有问题请告诉我

关于javascript - 让 Discord Bot 接收文件并将内容发送到 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50863421/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com