如何使用discord bot发送文件?

时间:2018-02-25 23:18:49

标签: javascript discord discord.io

这是我目前的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。

2 个答案:

答案 0 :(得分:1)

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

  1. 如果您希望图片显示在发送命令channel的同一频道中,请通过;killerbean实例发送附件。您可以使用message.channel从发送的邮件中获取channel
  2. 使用.send代替.sendFile。在文档中,您可以看到.sendFile已弃用。
  3. 以下是如何在发送命令的同一频道中发送图片(不要在第一个参数中发送空字符串''):

    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代码。

答案 1 :(得分:0)

bot.sendFile({    到:channelID,    档案:“ killerbean.png” })

相关问题