目前这是我的 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。
最佳答案
你走在正确的轨道上,但你应该改变两件事:
- 如果您希望图像显示在发送命令
;killerbean
的同一 channel 中,则通过channel
实例发送附件。您可以使用message.channel
从发送的消息中获取channel
. - 使用
.send
而不是.sendFile
。在文档中,您可以看到.sendFile
已被弃用。
下面是如何在发送命令的同一 channel 中发送图像(不要在第一个参数中发送空字符串''
):
message.channel.send(new Discord.Attachment('./emojis/killerbean.png', 'killerbean.png') )
.catch(console.error);
此外,您的 bot.on('message'...
监听器应该只有一个参数 - message
(如文档中的 example usage 所示) . 如果您的许多参数(user
、userID
、channelID
、message
, evt
).
所以你的最终命令应该是这样的:
// ...
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
代码。
关于javascript - 如何使用 discord bot 发送文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48979524/