我已经创建 token 和权限并授权正确的服务器
代码
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
重新启动我的服务器
我测试过
我没有看到任何已发送的图像。
如何进一步调试这个问题?
最佳答案
看看 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/