javascript - Discordjs - 使用 fs.watchFile 事件创建频道
问题描述
我试图让我的机器人在fs.watchFile
事件触发时创建一个频道。
这是代码
const Discord = require('discord.js')
const client = new Discord.Client()
//1000 lines of code here
const banLogFile = "logs/banlist_info.log";
fs.watchFile(banLogFile, (curr, prev) => {
fs.readFile(banLogFile, 'utf-8', (err, data) => {
// Parsing the new line in the file.
const banSendPubAdmin = {} //embed here, I cut the details
const baname2 = 'br-' + banName // Name of the channel + banned player name
guild.channels //ISSUE IS HERE
.create(baname2, {
type: 'text',
permissionOverwrites: [
{
id: 'config.AdminRoleId',
allow: ['VIEW_CHANNEL']
},
]
})
.then((channelBanReview) => {
const categoryId = '793625324927057982'
channelBanReview.setParent(config.BanReviewCategoryId)
client.channels.cache.get(channelBanReview.id).send({ embed: banSendPubAdmin });
})
});
});
//Another 500 lines of codes here too
client.login(config.token)
TypeError: Cannot read property 'create' of undefined
触发事件时,我遇到了一些其他类似的未定义问题。
我已经尝试了许多.create
行的变体但无济于事。
谢谢你。
解决方案
经过数小时的敲击键盘后发现了这个问题。
要创建频道,您首先需要公会信息。大多数情况下,人们使用聊天命令来执行此操作,这些命令存储了消息所需的所有信息。
但是当创建一个没有不和谐事件的频道时,你必须定义你的公会。我之前确实尝试过,但忘记了s
with guilds
。
这是解决我的问题的行。
client.guilds.cache.get('Guild ID here').channels.create().then()
推荐阅读
- python - 如何在 Python 和 Bash 中解析 JSON?
- java - PayPal NVP/SOAP API TransactionSearchReq 调用
- android - 在 Android 中使用来自网络 URL 的图像从视图中生成 PDF 文件
- c++ - Qt 在特定代码之后没有继续运行指令
- c++ - Qt Creator 在设计选项卡中调整小部件的大小
- angularjs - 在angularjs中具有搜索能力的多选
- r - 将R中的两个向量按元素粘贴在一起
- python-3.x - 如何解决这个 cv2.cvtColor() 错误?
- haskell - 使用 Streamly 消费无限单子流
- reactjs - 如何在反应js中发生事件(按钮单击)时将组件替换为另一个组件