当某人发布带有机器人的视频时发送消息以不和谐

时间:2021-07-12 19:27:42

标签: node.js discord.js

这是我用过的代码

const Database = require('easy-json-database')
const db = new Database('./database.json')

const Discord = require('discord.js')
const client = new Discord.Client()

const tiktok = require('tiktok-scraper')

client.login('tokenishere')

const resolveID = async () => (await tiktok.getUserProfileInfo('follow.for.afollownocap')).user.id

const sync = async (userID) => {
    const cache = db.get('cache')
    const { collector: newPosts } = await tiktok.user(userID)
    if (newPosts.length === 0) return
    const newPostsSorted = newPosts.sort((a, b) => b.createTime - a.createTime).slice(0, 10)
    if (cache) {
        const post = newPostsSorted.filter((post) => !cache.includes(post.id))[0]
        if (post && (post.createTime > ((Date.now() - 24 * 60 * 60 * 1000) / 1000))) {
            const author = post.authorMeta.nickName
            const link = post.webVideoUrl
            const embed = new Discord.MessageEmbed()
                .setAuthor(author, client.user.displayAvatarURL())
                .setTitle(post.text)
                .setThumbnail('https://sf-tb-sg.ibytedtos.com/obj/eden-sg/uhtyvueh7nulogpoguhm/tiktok-icon2.png')
                .setImage(post.covers.default)
                .setColor('#00FF00')
                .setTimestamp()
                .setFooter(author, client.user.displayAvatarURL())
            client.channels.cache.get('862092037348130868').send(`[@everyone]\n\n**${author} Just posted a new video!\n\nGo check it out: ${link} !**`, embed)
        }
    }
    db.set('cache', newPostsSorted.map((post) => post.id))
}

client.on('ready', async () => {
    client.user.setActivity('online', {
        type: 'WATCHING'
    })
    const userID = await resolveID()
    sync(userID)
    setInterval(() => sync(userID), 120 * 1000)
})

我发布了一个视频,它什么也没做。我等了一个小时仍然没有任何错误,任何人都可以帮助我吗?我尝试使用不同的 api,但到目前为止都没有奏效。然而它应该工作它不是为什么。 我已经为此工作了几个小时,它打开了,其他一切正常,这不是我主要用来编写代码的,所以请帮忙。

2 个答案:

答案 0 :(得分:0)

首先,出于安全原因,我强烈建议您将密钥保存在环境变量中。您也可以在顶部解构它以获得干净的代码。

require('dotenv').config();
const { BOT_TOKEN } = process.env;

我相信您的问题的解决方案是您没有等待不和谐登录。 client.login() 返回一个承诺。在执行依赖于客户端的功能之前,您应该等待该登录。

最后,如果这不起作用,在没有看到您收到的数据的情况下,我们无能为力。如果您还没有,请熟悉您的调试器并找出您正在接收哪些数据,或者更重要的是,在每个步骤中都接收到哪些数据。

答案 1 :(得分:0)

尝试将 client.login() 移到文件底部