【问题标题】:discord.io to send Image via Discord Bot - NodeJSdiscord.io 通过 Discord Bot - NodeJS 发送图像
【发布时间】:2019-06-07 22:53:08
【问题描述】:

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

代码

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

重启我的服务器

我测试过

我没有看到任何发送的图像。

如何进一步调试?

【问题讨论】:

  • bot.on('message' 被触发了吗?
  • 是的,它触发了。
  • 我试过!ping,我得到了"pong"
  • 为什么你有两个“消息”监听器?这对我来说没有多大意义,其中一个可能不会触发。
  • 我听了 2 次,因为当我添加另一个案例时出现了一些问题,第二个案例不断触发。

标签: javascript node.js discord discord.io


【解决方案1】:

查看 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 错误。

【讨论】:

  • 我们在哪里指定文件的路径?
  • bot.uplaodFile() 函数中,带有file 选项。应该能够通过直接链接或/path/to/img.png/ 发送图像,但后者可能还需要通过fileName。那或 Discord 会将其命名为“未知”,这不会引起问题。
【解决方案2】:

看例子——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"]);

【讨论】:

  • 我必须在本地保存 gif。不适用于网址。
猜你喜欢
  • 2019-11-18
  • 2020-05-13
  • 2019-02-22
  • 2021-09-14
  • 2019-02-13
  • 2018-01-22
  • 2020-08-18
  • 2022-07-08
  • 2019-01-06
相关资源
最近更新 更多