Skip to content

Instantly share code, notes, and snippets.

@LiamTownsley
Last active January 4, 2021 18:07
Show Gist options
  • Save LiamTownsley/3444e48e79a0bb14f3339941e8e5d839 to your computer and use it in GitHub Desktop.
Save LiamTownsley/3444e48e79a0bb14f3339941e8e5d839 to your computer and use it in GitHub Desktop.
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