通过Discord Bot发送图像-NodeJS

时间:2019-01-13 21:10:21

标签: javascript node.js discord discord.io

我已经创建了令牌,权限并授权了正确的服务器

enter image description here

代码

var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');
// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(new 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);
       switch(cmd) {
          // !ping
            case 'ping':
            bot.sendMessage({
                to: channelID,
                message: 'Pong'
            });
            break;
            // Just add any case commands if you want to..


    }
    }
});

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

        switch(cmd) {
            // !ping
            case 'img':
            bot.sendMessage("img", {
                file: "https://i.imgur.com/hIK7JKq.jpg" // Or replace with FileOptions object
            });
            break;
            // Just add any case commands if you want to..
        }
    }
});

// I've tried with this Perm Int : 522304

重新启动服务器

enter image description here

我测试了

enter image description here

我看不到任何图像发送。

如何进一步调试呢?

2 个答案:

答案 0 :(得分:2)

看看Discord.io文档,不是uploadFile吗?因为我没有使用Discord.io,而是使用Discord.js,所以我可能是错的,所以我提前致歉。像这样:

bot.uploadFile({
    to: id,
    file: FileBuffer
}).catch(console.error);

您也不需要两个消息侦听器。您可以在单个消息事件中拥有所有内容。

bot.on('message', (user, userID, channelID, message, evt) => {
  if (user.bot) return; // prevents bots interacting with one another or itself.
  if (message.substring(0, 1) == '!') {
    var args = message.substring(1).split(' ');
    var cmd = args[0];

    args = args.splice(1);

    switch (cmd) {
      case 'ping':
      bot.sendMessage({
        to: channelID,
        message: 'Pong'
      }).catch(console.error);
      break;
      case 'img':
      bot.uploadFile({
        to: channelID,
        file: FileBuffer
      }).catch(console.error);
    };
  };
});

特别注意:将.catch()放在发送函数的末尾将捕获Promise错误。

答案 1 :(得分:0)

看例子-https://github.com/izy521/discord.io/blob/master/example.js 您需要获得辅助功能

function sendFiles(channelID, fileArr, interval) {
    var resArr = [], len = fileArr.length;
    var callback = typeof(arguments[2]) === 'function' ? arguments[2] : arguments[3];
    if (typeof(interval) !== 'number') interval = 1000;

    function _sendFiles() {
        setTimeout(function() {
            if (fileArr[0]) {
                bot.uploadFile({
                    to: channelID,
                    file: fileArr.shift()
                }, function(err, res) {
                    resArr.push(err || res);
                    if (resArr.length === len) if (typeof(callback) === 'function') callback(resArr);
                });
                _sendFiles();
            }
        }, interval);
    }
    _sendFiles();
}

然后

sendFiles(channelID, ["ep.gif"]);