Discord.js -- 让 presenceUpdate 发送消息

Discord.js -- make presenceUpdate send a message

我正在尝试让我的超级简单的机器人发送一条说明用户状态的消息。就好像有人离线、在线等一样,它会自动向服务器发送一条消息,说明发生了什么。 我只是在做一个工作,这样它就可以得到更新(我知道我每次都需要 !status)

有人有想法让它在 presenceUpdate 触发后立即发送相同的消息吗?

let userStatus = [];

bot.on("presenceUpdate", (oldMember, newMember) => {
    let username = newMember.user.username;
    let status = newMember.user.presence.status;
    userStatus.push(username, status);
    console.log(`${newMember.user.username} is now ${newMember.user.presence.status}`);
})

bot.on('message', (message) => {
    // if (!message.content.startsWith(prefix)) return;
    if (console.log())
        let [username, status] = userStatus;

    if (message.content.startsWith(prefix + "status")) {
        let botembed = new Discord.RichEmbed()
            .setDescription("Status Update")
            .setColor("#FFF")
            .addField('.............................................', `${username} is now ${status}`);

        message.channel.send(botembed);

        userStatus = [];
    }
});

我认为您 运行 遇到的问题是您不再直接引用频道,这就是为什么您不能 "easily" 调用 <TextChannel>.send(...).您必须在 presenceUpdate 事件侦听器中决定要将消息发送到哪个频道。决定后,您可以使用此代码通过频道的 name:

获取对该频道的引用
client.on('presenceUpdate', (oldMember, newMember) => {
    // get a reference to all channels in the user's guild
    let guildChannels = newMember.guild.channels;

    // find the channel you want, based off the channel name
    // -> replace '<YOUR CHANNEL NAME>' with the name of your channel
    guildChannels.find('name', '<YOUR CHANNEL NAME>')
        .send('test message!')
        .then(msg => {
            // do something else if you want
        })
        .catch(console.error)
});

注意:您不必使用频道的 name 属性 来标识唯一频道,您可以通过

使用频道的 id
guildChannels.get('<YOUR CHANNEL ID')
.send('...

希望对您有所帮助!