【问题标题】:Hibernate Discord bot discord.js休眠不和谐机器人 discord.js
【发布时间】:2021-07-30 06:55:24
【问题描述】:

我正在尝试创建一个休眠模式,其中机器人不执行任何操作,但它保持在线状态。我在下面写了一些东西

    let configstuffs = JSON.parse(fs.readFileSync('config.json')) //figuring out if hibernate mode is on
    if (configstuffs.hibernate === true) {
    client.user.setPresence({ status: 'idle' })
    client.user.setActivity('Bot is hibernating')
    console.log('hibernating')
    return
    } else {
        client.user.setPresence({ status: 'online' })
        client.user.setActivity('')
        console.log('no longer hibernating')
    }

我把它放在我的消息侦听器之外,在我的 bot.once('ready', () => {} 内,但它说的是“无法读取属性 'setPresence' of null”。

我的目标是让当休眠模式处于活动状态时,机器人变得空闲,并将其游戏设置为“休眠”或类似的东西,然后忽略所有消息,或绕过消息侦听器。有人有什么想法吗?

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    我的机器人中有一个休眠选项,这就是我所做的

    let isHibernating = false; //Global (top level) variable
    const Hibernate = (client) => {
        if(!client){ return(false) }
        client.user.setPresence({ //Sets detailed presence
            activity: {
                name: "Hibernating",
                type: "PLAYING"
            },
            status: "idle",
            afk: true
        });
        isHibernating = true;
    }
    

    在你的消息处理程序中

    client.on("message", message => {
        if(isHibernating){ return(false) } //Bot is Hibernating
        // other things
        if(message.content === "hibernate" && message.author.id == "Bot owner ID"){
            Hibernate(client);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2021-08-30
      • 2021-10-19
      • 2021-07-13
      • 2020-05-10
      • 2018-03-20
      • 1970-01-01
      • 2020-11-27
      • 2021-11-04
      • 2020-10-11
      相关资源
      最近更新 更多