Отправить изображение через Discord Bot - NodeJS - PullRequest
0 голосов
/ 14 января 2019

Я уже создаю токен, разрешаю и авторизую правильный сервер

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

Я не вижу ни одного отправленного изображения.

Как можно отладить это дальше?

1 Ответ

0 голосов
/ 14 января 2019

Глядя на документы 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.

...