X不是函数-如何修复

时间:2019-07-08 16:12:36

标签: javascript node.js

我正在使用this

创建TS3查询机器人

我想出了core.js,register.js和2个配置,它们在这里并不重要。

Core.js:

const TeamSpeak3 = require("ts3-nodejs-library")
var config = require("./configs/config.js")

//Create a new Connection
const ts3 = new TeamSpeak3({
    host: "localhost",
    queryport: 10011,
    serverport: 9987,
    username: "serveradmin",
    password: "",
    nickname: "NodeJS Query Framework"
})

/*
  Ready gets fired when the Bot has connected to the TeamSpeak Query and
  issued login commands (if username and password has been given)
  selected the appropriate Server (also if given in the config)
  and set the nickname
*/
ts3.on("ready", async () => {
    Promise.all([
        ts3.registerEvent("server"),
        ts3.registerEvent("channel", 0),
        ts3.registerEvent("textserver"),
        ts3.registerEvent("textchannel"),
        ts3.registerEvent("textprivate")
    ]).then(() => {
        console.log("[ JANbot ] Nasluchuje eventow: server, channel (0), textserver, textchannel, textprivate")
    }).catch(e => {
        console.log("Catched an error!")
        console.error(e)
    })
})

if(config.registerEnabled = true) {
    var register = require("./inc/functions/register.js")
    ts3.on("clientmoved", event => {
            register.registerUser(event, event.client)
    })
}

ts3.on("error", e => console.log("Error", e.message))
ts3.on("close", e => console.log("Connection has been closed!", e))

register.js:

module.exports = function() {
    this.registerUser = function(event, client) 
    {
        var register_config = require("/home/node_modules/bot/configs/register_config.js")
        if(event.channel.cid == register_config.channelId) {
            if(! client.servergroups.includes(register_config.groupId)) {
                event.client.addGroups(config.groupId)
                event.client.message("You have been successfully registered!")
                event.client.kickFromChannel()
                return console.log("zarejestrowano " + event.client.nickname)
            } else {
                event.client.message("You are already registred!")
                event.client.kickFromChannel()
                return console.log(event.client.nickname + " juz jest zarejestrowany")
            }
        }
    };
}

主要思想是处理bot及其在core.js中的连接,只需对其特定功能即可进行更轻松的更新(您无需下载具有许多功能,只需一个文件,最后一个配置的整个core.js为此)

我遇到的错误:

Error register.registerUser is not a function

我在JS方面并不先进,所以如果我做错了什么,除了我的问题,请告诉我。我有些想法不对,但是我无法解决。我认为registerUser不起作用,但我不知道如何在此处实现register.js:

if(config.registerEnabled = true) {
    var register = require("./inc/functions/register.js")
    ts3.on("clientmoved", event => {
            register.registerUser(event, event.client)
    })
}

顺便说一句。我添加返回值只是因为我认为它将把registerUser变成函数。1

//编辑: 它有效,但是现在我遇到了第二个问题。 现在的代码:

module.exports = {
    registerUser: function(event, client) 
    {
        var register_config = require("/home/node_modules/bot/configs/register_config.js")
        if(event.channel.cid == register_config.channelId) {
            var grupa = register_config.groupId
            if(!event.client.servergroups.includes(register_config.groupId)) {
                var date = new Date(),
                    g = date.getHours(),
                    m = date.getMinutes(),
                    s = date.getSeconds()
                event.client.addGroups(register_config.groupId)
                event.client.poke("You have been successfully registered!")
                event.client.kickFromChannel()
                console.log(" [ " + g + " : " + m + " : " + s + " ] " +'Zarejestronwalem: ' + client.nickname)
            } else {
                event.client.poke("You are already registered!")
                event.client.kickFromChannel()
            }
        }
    }
}

该部分:

  

if(!event.client.servergroups.includes(register_config.groupId)){

始终为真,因为客户端的数组服务器组在某种程度上没有数字11,因此脚本只认为它是真的。我添加了console.log(event.client.servergroups)来查看是否是register_config.groupId或其他问题,这在控制台中显示:

  

[9,11]

因此,在此之后,我发现register_config.groupId存在问题,因为bot的代码如下: (core.js与第二个问题出现之前的状态相同)

register.js:

module.exports = {
    registerUser: function(event, client) 
    {
        var register_config = require("/home/node_modules/bot/configs/register_config.js")
        if(event.channel.cid == register_config.channelId) {
            var grupa = register_config.groupId
            if(!event.client.servergroups.includes(register_config.groupId)) {
                var date = new Date(),
                    g = date.getHours(),
                    m = date.getMinutes(),
                    s = date.getSeconds()
                event.client.addGroups(register_config.groupId)
                event.client.poke("You have been successfully registered!")
                event.client.kickFromChannel()
                console.log(" [ " + g + " : " + m + " : " + s + " ] " +'Zarejestronwalem: ' + client.nickname)
            } else {
                event.client.poke("You are already registered!")
                event.client.kickFromChannel()
            }
        }
    }
}

register_config.js:

module.exports = {
        channelId: 4,
        groupdId: 11
}

机器人的行为如下:

  

随机频道上的客户端(不适用于CID 4)   客户以CID 4加入频道   机器人踢了他,       戳他,       没有给他服务器组

结论:register_config.groupId不被定义为11。

//编辑#2:

我已经做到了:

var register_config = {

        "groupId": 11,
        "channelId": 4

};

module.exports = register_config;

它有效:)没关系,谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

register.js 中导出对象而不是函数:

// module.exports = function() {
module.exports = {
    // this.registerUser = function(event, client) 
    registerUser: function(event, client) 
    {
        var register_config = require("/home/node_modules/bot/configs/register_config.js")
        if(event.channel.cid == register_config.channelId) {
            if(! client.servergroups.includes(register_config.groupId)) {
                event.client.addGroups(config.groupId)
                event.client.message("You have been successfully registered!")
                event.client.kickFromChannel()
                return console.log("zarejestrowano " + event.client.nickname)
            } else {
                event.client.message("You are already registred!")
                event.client.kickFromChannel()
                return console.log(event.client.nickname + " juz jest zarejestrowany")
            }
        }
    };
}