Javascript 使用 NodeJS 使 Discord 机器人发送带有消息的图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45684472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:10:25  来源:igfitidea点击:

Make Discord bot send picture with message with NodeJS

javascriptnode.jsdiscord

提问by Kaito Kid

I have a few pictures, all on imgur with direct image link (format: https://i.imgur.com/XXXXXX.jpg), and a Discord bot made with NodeJS.

我有几张图片,都在 imgur 上,带有直接图片链接(格式:https://i.imgur.com/XXXXXX.jpg ),还有一个用 NodeJS 制作的 Discord 机器人。

I send messages like this:

我发送这样的消息:

bot.sendMessage({
    to: channelID,
    message: "My Bot's message"
});

I have tried this:

我试过这个:

bot.sendMessage({
    to: channelID,
    message: "My Bot's message",
    file: "https://i.imgur.com/XxxXxXX.jpg"
});

but I only get the text. I have looked it up, and this questionwas the only one to even come close to saying what I need to do, and it didn't work.

但我只得到文本。我查了一下,这个问题是唯一一个接近说出我需要做什么的问题,但它没有用。

So how am I supposed to do this?

那么我该怎么做呢?

Here is how the bot is created:

以下是机器人的创建方式:

var bot = new Discord.Client({
   token: auth.token,
   autorun: true
});
bot.on('ready', function (evt) {
    logger.info('Connected');
    logger.info('Logged in as: ');
    logger.info(bot.username + ' - (' + bot.id + ')');
});
bot.on('message', function (user, userID, channelID, message, evt) {
    // My code
}

回答by Caleb

ClientUser.sendMessageis deprecated, as is the fileparameter in its options. You should be using Channel.send(message, options), with filesas an array of strings or FileOptions.

ClientUser.sendMessage已弃用file其选项中的参数也是如此。您应该使用Channel.send(message, options), withfiles作为字符串数组或FileOptions

bot.on('message' message => {
    message.channel.send("My Bot's message", {files: ["https://i.imgur.com/XxxXxXX.jpg"]});
});

If you want to stick to your deprecated methods, ClientUser.sendFilemight be something of interest to you, though I do recommend you move over to the stuff that's more current.

如果您想坚持使用已弃用的方法,您ClientUser.sendFile可能会感兴趣,但我建议您转向使用更流行的内容

回答by koubi

You can send local files in v11.2 like this:

您可以像这样在 v11.2 中发送本地文件:

var Discord = require('discord.js');
var bot = new Discord.Client();

bot.on('message', message => {
    var prefix = '!'
    var msg = message.content;

    if (msg === prefix + 'image') {
        message.channel.send('Message that goes above image', {
            files: [
                "./image-to-send.png"
            ]
        });
    }
});

bot.login('TOKEN');

回答by Premier Bromanov

Since this is one of the top results on google in 2019, I'm adding the new method of how to upload files with discord.io

由于这是 2019 年谷歌上的最佳结果之一,我正在添加如何使用 discord.io 上传文件的新方法

First thing that's different is the on()function takes some additional parameters.

首先不同的是该on()函数需要一些额外的参数。

Next is that there's a new method called uploadFilethat takes an uploadFileOptsobject. the filecan take a string that is a local path from your bot file to the image.

接下来是有一个新方法被调用uploadFile,它接受一个uploadFileOpts对象。在file可以采取一个字符串,它是从你的机器人文件到图像的本地路径。

uploadFileOpts = {
  to: string,
  file: string|Buffer,
  filename?: string,
  message?: string
}

So, if you place your image next to your bot script, your code should look like this

所以,如果你把你的图片放在你的机器人脚本旁边,你的代码应该是这样的

bot.on('message', function (user, userID, channelID, message, evt) {
 bot.uploadFile({
            to: channelID,
            file: 'myImage.jpg'
        });
}

If you still want to snag that image from the internet, you'll need to convert it into a Bufferobject. However, storing the file locally is simpler.

如果您仍想从互联网上获取该图像,则需要将其转换为Buffer对象。但是,将文件存储在本地更简单。

回答by scoutchorton

If you're using discord.io instead of Discord.js, refer to this syntax:

如果您使用的是 discord.io 而不是 Discord.js,请参考以下语法:

https://izy521.gitbooks.io/discord-io/content/Methods/Channels.html

https://izy521.gitbooks.io/discord-io/content/Methods/Channels.html

I'm still trying to get it to work.

我仍在努力让它发挥作用。