请帮帮我。下面是关于discord.js聊天机器人的代码
const fetch = require("node-fetch");
const {ChannelType} = require("discord.js");
client.on('messageCreate', async (message) => {
const chatbots = require("./All-Commands-Schemas/ChatbotSchema")
chatbots.findOne({ guild: message.guild.id }, async (err, data) => {
if (!data) return;
if (err) throw err
const channell = data.channel;
if (message.author.bot || message.channel.type === ChannelType.DM) return;
if (message.channel.id === channell) {
message.channel.sendTyping();
await fetch(`http://api.brainshop.ai/get?bid=164279&key=rhBjCAZC83ztKzYO&uid=${message.author.id}&msg=${message.content}`)
.then(cnt => cnt.json())
.then(data => {
message.channel.send(data.cnt);
})
.catch(() => {
message.channel.send("Couldn't fetch response!");
})
}
})
})
Chatbot模式
const mongoose = require("mongoose");
const chatbots = new mongoose.Schema({
guild: String,
channel: String,
name: String
});
module.exports = mongoose.model("chatbots", chatbots);
问题是聊天机器人没有回复或在控制台日志中显示错误。我什么都试过了
"node-fetch"^ 3.3.0","discord.js"^ 14.6.0","mongoose"^ 6.7.0"
Replace:
const channell = data.channel;
:
const channell = client.channels.cache.get(data.channel);
错误是不能直接使用
const channell = data.channel
要动态获取通道,必须使用
从缓存中获取通道channels.cache.get()
所以,你应该替换
const channell = data.channel
const channell = client.channels.cache.get(data.channel)