Javascript 如何使用discord bot发送文件?

Javascript 如何使用discord bot发送文件?,javascript,discord,discord.io,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 })

这是当前我的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,但我不知道如何使用它

当有人输入命令',我如何发送图像;基勒比恩'

编辑:在依赖项或其他任何重要的情况下抛出package.json


表情机器人用户也被称为表情机器人。

你的思路是正确的,但你应该改变两件事:

  • 如果希望图像显示在命令
    所在的同一频道中,请通过
    频道
    实例发送附件;已发送killerbean
    。您可以使用从发送的消息中获取
    频道
  • 使用而不是
    .sendFile
    。在文档中,您可以看到
    .sendFile
    已被弃用
  • 以下是如何在发送命令的同一通道中发送图像(不要在第一个参数中发送空字符串
    '
    ):

    此外,您的
    bot.on('message'…
    侦听器应该只有一个参数-
    message
    (如文档中的中所示)。如果您的许多参数没有出现错误(
    user
    userID
    channelID
    message
    evt
    ),我会感到惊讶

    因此,您的最终命令应该如下所示:

    // ...
    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({ 致:channelID, 文件:“killerbean.png”
    })

    这帮了大忙!但需要注意的是,我使用了discord.io和discord.js中的方法并阅读了两个不同的文档:discord.io和discord.js。我选择了discord.js,因为这是您提供的文档,并与您的答案一起使用。@Moralous-这很好知道!我最近看到了
    discord.io
    ,但没有成功我经常使用它,没有将它与语法联系起来,所以我只是为
    discord.io
    创建了一个标记,希望这不会让将来的其他人感到困惑。
    message.channel.send(new Discord.Attachment('./emojis/killerbean.png', 'killerbean.png') )
    .catch(console.error);
    
    // ...
    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;
             }
         }
    });