discord消息提醒的问题

in hive-180932 •  4 years ago 

这几天的discord消息提醒总是不提醒提到的消息

在测试在回复中提到自己,倒是能获得消息提醒

但是在帖子里提到自己就不行了

看了一下代码,发现是逻辑上的问题:


if (user && user.mention && params.body.includes(`@${user.account}`)) {
    let content = '';
    let title = '';
    let comment = '';
    let url = '';
    if (isRootPost) {
        content = `@${user.account}:\n"@${user.account}" was  mentioned by @${params.author} in a post`;
        title = `${params.title}`;
        comment = params.body.substring(0, 100);
    } else {
        content = `@${user.account}:\n"@${user.account}" was  mentioned by @${params.author} in a comment`;
        title = `${params.permlink}`;
        comment = params.body.substring(0, 100);
    }
    url = `${user.platform}/@${params.author}/${params.permlink}`;
    createMessge(user.id, content, title, comment, url, time);
}

问题出在第一行,如果body里面包含用户账号,就发送消息提醒

听起来没啥问题,但是问题就出在包含的用户账号这里

这里的用户账号是发帖人的账号,而不知要收到提醒的账号

所以如果你不是发帖人,并且也不是收到提醒的账号,那就永远收不到消息提醒

所以要先获取所有要获得消息提醒的账号,然后把账号逐一试试body里是否有包含这个账号。如果有,就发消息给这个账号。

修复后的代码如下:


let accounts = Array.from(usersMap.keys());
for (account of accounts) {
    user = usersMap.get(account);
    if (user && user.mention && params.body.includes(`@${account}`)) {
        let content = '';
        let title = '';
        let comment = '';
        let url = '';
        if (isRootPost) {
            content = `@${user.account}:\n"@${user.account}" was  mentioned by @${params.author} in a post`;
            title = `${params.title}`;
            comment = params.body.substring(0, 100);
        } else {
            let post = await getContent(params.parent_author, params.parent_permlink);
            content = `@${user.account}:\n"@${user.account}" was  mentioned by @${params.author} under the post`;
            title = `${post.root_title}`;
            comment = params.body.substring(0, 100);
        }
        url = `${user.platform}/@${params.author}/${params.permlink}`;
        createMessge(user.id, content, title, comment, url, time);
    }
}

最后成功获得消息提醒:

image.png

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!