我正在尝试制作 Karuta 交易应用程序机器人,但它一直在说 Cannot read property “send” of undefined
。
每次我试图找出问题时,它都会变得越来越糟。
有人可以帮我找出这个破坏我申请流程的问题吗?
尝试发送到名为 test 的通道时失败
代码:
const { Client, Message, MessageEmbed } = require('discord.js');
module.exports = {
name: `apply`,
/**
* @param {Client} client
* @param {Message} message
* @param {String[]} args
*/
async execute(client, message, args) {
const questions = [
"Would you like to advertise your Karuta Card Trades?",
"Cards FS:",
"Cards FB:"
]
let collectCounter = 0;
let endCounter = 0;
const filter = (m) => m.author.id === message.author.id;
const appStart = await message.author.send(questions[collectCounter++]);
const channel = appStart.channel
const collector = channel.createMessageCollector(filter);
collector.on("collect", () => {
if(collectCounter < questions.length) {
channel.send(questions[collectCounter++])
} else {
channel.send('Your application has been submitted!')
collector.stop("fulfilled");
}
});
const APPS_CHANNEL_ID = '822605997356875777'
const appsChannel = await client.channels.fetch(APPS_CHANNEL_ID)
collector.on('end', (collected, reason) => {
if(reason == 'fulfilled') {
let index = 1;
const mappedResponses = collected.map((msg) => {
return `${index++}) ${questions[endCounter++]}\n-> ${msg.content}`;
})
.join('\n\n')
appsChannel.send(
new MessageEmbed()
.setAuthor(message.author.tag, message.author.displayAvatarURL({dynamic: true})
)
.setTitle('New Application')
.setDescription(mappedResponses)
.setColor('RED')
.setTimestamp()
)
throw new Error('TOKEN_MISSING');
}
});
},
}
错误:
(node:3435) UnhandledPromiseRejectionWarning: DiscordjsError: Request to use token, but token was unavailable to the client.
at RequestHandler.execute (/Users/mahdiabbas/Documents/Karuta Trade/node_modules/discord.js/src/rest/RequestHandler.js:93:15)
at RequestHandler.execute (/Users/mahdiabbas/Documents/Karuta Trade/node_modules/discord.js/src/rest/RequestHandler.js:97:19)
at RequestHandler.push (/Users/mahdiabbas/Documents/Karuta Trade/node_modules/discord.js/src/rest/RequestHandler.js:39:25)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async ChannelManager.fetch (/Users/mahdiabbas/Documents/Karuta Trade/node_modules/discord.js/src/managers/ChannelManager.js:91:18)
at async Object.execute (/Users/mahdiabbas/Documents/Karuta Trade/commands/tradead.js:35:29)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:3435) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:3435) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Index.js:
const Discord = require('discord.js');
const bot = new Discord.Client()
const client = new Discord.Client()
const prefix = '-';
const queue = new Map();
const fs = require('fs');
bot.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
bot.commands.set(command.name, command);
}
bot.on('ready', () => {
console.log(`${bot.user.tag} is online.`)
bot.user.setStatus(`dnd`)
bot.user.setActivity({
type: `WATCHING`,
name: `Out For Subaru`,
})
})
bot.on("message", async message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const commandName = args.shift().toLowerCase();
const command = bot.commands.get(commandName) || bot.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
try {
command.execute(client, message, args);
} catch (error) {
console.log(error)
}
});```
答案 0 :(得分:3)
看起来 client.channels.cache.get('822605997356875777')
返回 undefined
。这可能意味着频道没有被缓存。您可以使用 ChannelManager#fetch
获取频道。
您还创建了一个额外的 Client
对象(未登录)并将其传递给 execute
函数。你不需要它。从 const client = new Discord.Client()
中删除 index.js
。
并将 command.execute
调用更新为仅发送 message
和 args
。
command.execute(message, args)
您可以在 client
函数中从 message
获取 execute
对象。
async execute(message, args) {
const { client } = message
// ...
const APPS_CHANNEL_ID = '822605997356875777'
const appsChannel = await client.channels.fetch(APPS_CHANNEL_ID)
// ...
}
请注意,您必须更新所有命令的 execute
函数,因为它们只会收到 message
和 args
。