首页 > 解决方案 > 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行的变体但无济于事。

谢谢你。

标签: javascript

解决方案


经过数小时的敲击键盘后发现了这个问题。

要创建频道,您首先需要公会信息。大多数情况下,人们使用聊天命令来执行此操作,这些命令存储了消息所需的所有信息。

但是当创建一个没有不和谐事件的频道时,你必须定义你的公会。我之前确实尝试过,但忘记了swith guilds

这是解决我的问题的行。

client.guilds.cache.get('Guild ID here').channels.create().then()


推荐阅读