Make Discord bot send picture with message with NodeJS

别等时光非礼了梦想. 提交于 2020-12-29 06:38:33

问题


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.

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 question was 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
}

回答1:


ClientUser.sendMessage is deprecated, as is the file parameter in its options. You should be using Channel.send(message, options), with files as an array of strings or 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.sendFile might be something of interest to you, though I do recommend you move over to the stuff that's more current.




回答2:


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

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');



回答3:


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

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

Next is that there's a new method called uploadFile that takes an uploadFileOpts object. the file can take a string that is a local path from your bot file to the image.

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 Buffer object. However, storing the file locally is simpler.




回答4:


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

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

I'm still trying to get it to work.



来源:https://stackoverflow.com/questions/45684472/make-discord-bot-send-picture-with-message-with-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!