gaber-elsayed

tiktok bot discord

Aug 27th, 2021
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. const Database = require('easy-json-database')
  2. const db = new Database('./database.json')
  3.  
  4. const Discord = require('discord.js')
  5. const client = new Discord.Client()
  6.  
  7. const tiktok = require('tiktok-scraper')
  8. const config = require('./config.json')
  9.  
  10. client.login(config.token)
  11.  
  12. const resolveID = async () => (await tiktok.getUserProfileInfo(config.tiktokAccount)).user.id
  13.  
  14. const sync = async (userID) => {
  15. const cache = db.get('cache')
  16. const { collector: newPosts } = await tiktok.user(userID)
  17. if (newPosts.length === 0) return
  18. const newPostsSorted = newPosts.sort((a, b) => b.createTime - a.createTime).slice(0, 10)
  19. if (cache) {
  20. const post = newPostsSorted.filter((post) => !cache.includes(post.id))[0]
  21. if (post && (post.createTime > ((Date.now() - 24 * 60 * 60 * 1000) / 1000))) {
  22. const author = post.authorMeta.nickName
  23. const link = post.webVideoUrl
  24. const embed = new Discord.MessageEmbed()
  25. .setAuthor(author, client.user.displayAvatarURL())
  26. .setTitle(post.text)
  27. .setThumbnail(config.embed_icon_url)
  28. .setImage(post.covers.default)
  29. .setColor('#00FF00')
  30. .setTimestamp()
  31. .setFooter(author, client.user.displayAvatarURL())
  32. client.channels.cache.get(config.notifChannel).send(`[@everyone]\n\n**${author} vient de poster un nouveau Tiktok !\n\nVa vite le voir ici : ${link} !**`, embed)
  33. }
  34. }
  35. db.set('cache', newPostsSorted.map((post) => post.id))
  36. }
  37.  
  38. client.on('ready', async () => {
  39. client.user.setActivity(config.activity, {
  40. type: 'WATCHING'
  41. })
  42. const userID = await resolveID()
  43. sync(userID)
  44. setInterval(() => sync(userID), 120 * 1000)
  45. })
Advertisement
Add Comment
Please, Sign In to add comment