【问题标题】:How do I send a file using a discord bot?如何使用不和谐机器人发送文件?
【发布时间】:2018-08-05 09:06:20
【问题描述】:

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

当有人输入命令';killerbean'时,我该如何发送图像

编辑:抛出 package.json 以防依赖关系或其他任何问题

    {
      "name": "emoji-bot",
      "version": "1.0",
      "description": "Emojis+",
      "main": "bot.js",
      "author": "Joshua",
      "dependencies": {
        "discord-irc": "^2.5.1",
        "discord.io": "github:woor/discord.io#gateway_v6",
        "winston": "^2.4.0"
      }

    }

emoji-bot 用户也称为 emoji-bot。

【问题讨论】:

    标签: javascript discord discord.io


    【解决方案1】:

    你在正确的轨道上,但你应该改变两件事:

    1. 如果您希望图像显示在发送命令;killerbean 的同一频道中,请通过channel 实例发送附件。您可以使用message.channel从发送的消息中获取channel
    2. 使用.send 而不是.sendFile。在文档中,您可以看到 .sendFile 已被弃用。

    下面是如何在发送命令的同一通道中发送图像(不要在第一个参数中发送空字符串''):

    message.channel.send(new Discord.Attachment('./emojis/killerbean.png', 'killerbean.png') )
    .catch(console.error);
    

    此外,您的 bot.on('message'... 侦听器应该只有一个参数 - message(如文档中的 example usage 所示)。如果您的许多参数(useruserIDchannelIDmessageevt)没有遇到错误,我会感到惊讶。

    所以你的最终命令应该是这样的:

    // ...
    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 代码。

    【讨论】:

    • 这帮助很大!但是需要注意的是,我使用的方法来自两个不同的东西并阅读了文档:discord.io 和 discord.js。我选择了 discord.js,因为那是您提供的文档并使用了您的答案。
    • @Moralous - 很高兴知道!我最近看到了discord.io,但没怎么用过它,也没有把它和你的语法联系起来,所以我只是为discord.io 创建了一个标签,所以希望这不会在将来让其他人感到困惑。跨度>
    【解决方案2】:

    bot.sendFile({ 至:频道ID, 文件:“杀手豆.png” })

    【讨论】:

      猜你喜欢
      • 2021-04-26
      • 1970-01-01
      • 2021-05-08
      • 1970-01-01
      • 2021-08-07
      • 2020-08-25
      • 2020-10-01
      • 2021-04-03
      • 2021-08-14
      相关资源
      最近更新 更多