Last active
January 4, 2021 18:07
-
-
Save LiamTownsley/3444e48e79a0bb14f3339941e8e5d839 to your computer and use it in GitHub Desktop.
To help for https://stackoverflow.com/posts/65565159/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Client } = require('discord.js'); | |
const bot = new Client(); | |
const axios = require('axios').default; | |
let lastTimestamp = new Date() / 1000; | |
let latestPosts = []; | |
setInterval(() => { | |
console.log('Data Collected!'); | |
axios.get('https://www.reddit.com/r/AskReddit/new.json?limit=25') | |
.then(res => { | |
latestPosts = []; | |
for (const post of res.data.data.children.reverse()) { | |
if(new Date(post.data.created_utc * 1000) > lastTimestamp) { | |
latestPosts.push({ name: post.data.title, url: post.data.permalink }); | |
lastTimestamp = new Date(post.data.created_utc * 1000); | |
} | |
} | |
}); | |
}, 60000); | |
bot.on('message', msg => { | |
if(msg.author.bot) return; | |
if(msg.content === '!test') { | |
msg.channel.send(latestPosts.map(arr => arr.name).join('\n')); | |
} | |
}); | |
bot.login('TOKEN'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment