javascript - Discord.io 通过 Discord Bot 发送图像 - NodeJS

标签 javascript node.js discord discord.io

我已经创建 token 和权限并授权正确的服务器

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

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

如何进一步调试这个问题?

最佳答案

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

关于javascript - Discord.io 通过 Discord Bot 发送图像 - NodeJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54173251/

相关文章:

javascript - 检查进程 A 写入的文件是否准备好被进程 B 读取

javascript - 延迟加载不显示在屏幕上 img

node.js - 将所有 Node 模块的版本号修复为 package.json 中当前使用的版本号

javascript - 持久登录停止使用 Node.js、Express、PassportJS、Connect-Mongo

python - Random.Randint() 重复

python - 在 dm 中发送问题,并在嵌入到 channel discord.py 中发布答案

javascript - e.dataTransfer.files.length 显示为 0

javascript - 为什么我的模态视图会导致 "is not a constructor error"?

javascript - 如何检查用户是否更改了昵称?

javascript - 我输入的输入值没有显示在 JavaScript 中