How do I send a file using a discord bot?

18,740

You're on the right track, but you should change two things:

  1. Send attachments via a channel instance, if you want the image to show up in the same channel where the command ;killerbean was sent. You can get the channel from the sent message using message.channel.
  2. Use .send instead of .sendFile. In the docs you can see that .sendFile is deprecated.

Here's how to send the image in the same channel where the command was sent (don't send an empty string '' in the first parameter):

message.channel.send(new Discord.Attachment('./emojis/killerbean.png', 'killerbean.png') )
.catch(console.error);

Also, your bot.on('message'... listener should only have one parameter - message (as seen in the example usage in the docs). I would be surprised if you don't run into errors with your many parameters (user, userID, channelID, message, evt).

So your final command should look something like this:

// ...
bot.on('message', function (message) {
    // Our bot needs to know if it will execute a command
    // It will listen for messages that will start with `!`
    if (message.substring(0, 1) == ';') {
        var args = message.substring(1).split(' ');
        var cmd = args[0];

        args = args.splice(1);

        switch(cmd) {

            case 'killerbean':
                message.channel.send(new Discord.Attachment('./emojis/killerbean.png', 'killerbean.png') )
                .then(msg => {
                    // do something after message sends, if you want
                })
                .catch(console.error);
                break;
         }
     }
});

Edit: The discord.io tag had not been added to the question when I added this answer. Now I know the old syntax was allowed, but the syntax in my answer works for basic discord.js code.

Share:
18,740
notjoshno
Author by

notjoshno

Studying Computer Science and Maths at the University of Exeter

Updated on June 09, 2022

Comments

  • notjoshno
    notjoshno almost 2 years

    This is currently my bot.js

    var Discord = require('discord.io');
    var logger = require('winston');
    var auth = require('./auth.json');
    // Configure logger settings
    logger.remove(logger.transports.Console);
    logger.add(logger.transports.Console, {
        colorize: true
    });
    logger.level = 'debug';
    // Initialize Discord Bot
    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) {
        // Our bot needs to know if it will execute a command
        // It will listen for messages that will start with `!`
        if (message.substring(0, 1) == ';') {
            var args = message.substring(1).split(' ');
            var cmd = args[0];
    
            args = args.splice(1);
            console.info(cmd);
            switch(cmd) {
                // !ping
                case 'killerbean':
                //    bot.sendFile({
                 //       to: channelID,
                //      files: ['./emojis/killerbean.png']
                 //   });
                 logger.info(bot.channels);
                 User.sendFile('./emojis/killerbean.png');
                 //bot.channels[0].send('', new Discord.Attachment( './emojis/killerbean.png'));
    
                break;
                // Just add any case commands if you want to..
             }
         }
    });
    

    The commented code is some stuff that I tried that hasn't worked. bot.sendFile is apparently not a method. I'm currently looking at using User.send, but I cannot figure out how to use it.

    How do I go about sending an image when someone types in the command ';killerbean'

    Edit: Throwing in the package.json in case the dependencies or anything else there matters

        {
          "name": "emoji-bot",
          "version": "1.0",
          "description": "Emojis+",
          "main": "bot.js",
          "author": "Joshua",
          "dependencies": {
            "discord-irc": "^2.5.1",
            "discord.io": "github:woor/discord.io#gateway_v6",
            "winston": "^2.4.0"
          }
    
        }
    

    The emoji-bot user is also called emoji-bot.

  • notjoshno
    notjoshno about 6 years
    This helped a lot! However important to note is that I was using methods from and reading documentation for two separate things: discord.io and discord.js. I settled on discord.js as that was the documentation you provided and worked with your answer.
  • Blundering Philosopher
    Blundering Philosopher about 6 years
    @Moralous - that is very good to know! I recently saw discord.io but haven't worked with it much, and didn't connect it to your syntax, so I just created a tag for discord.io so hopefully this doesn't confuse anyone else in the future.