如何使用 discord bot 发送文件?

How do I send a file using a discord bot?

这是我目前的 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..
         }
     }
});

评论的代码是一些我试过但没有用的东西。 bot.sendFile 显然不是一种方法。我目前正在考虑使用 User.send,但我不知道如何使用它。

当有人输入命令“;killerbean”时,我该如何发送图像

编辑:加入 package.json 以防依赖项或其他任何重要事项

    {
      "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"
      }

    }

emoji-bot 用户也称为 emoji-bot。

你走在正确的轨道上,但你应该改变两件事:

  1. 如果您希望图像显示在发送命令 ;killerbean 的同一频道中,请通过 channel 实例发送附件。您可以使用 message.channel.
  2. 从发送的消息中获取 channel
  3. 使用 .send 而不是 .sendFile。在文档中,您可以看到 .sendFile 已弃用。

下面是如何在发送命令的同一频道发送图片(不要在第一个参数中发送空字符串''):

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

此外,您的 bot.on('message'... 侦听器应该只有一个参数 - message(如文档中的 example usage 所示)。如果您没有 运行 错误地使用您的许多参数(useruserIDchannelIDmessageevt,我会感到惊讶).

所以你的最终命令应该是这样的:

// ...
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;
         }
     }
});

编辑: 当我添加这个答案时,discord.io 标签还没有添加到问题中。现在我知道旧语法是允许的,但我的回答中的语法适用于基本 discord.js 代码。

bot.sendFile({ 至:频道ID, 文件:"killerbean.png" })