-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (57 loc) · 1.89 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const Discord = require('discord.js');
const { IgApiClient } = require('instagram-private-api');
const client = new Discord.Client();
const igClient = new IgApiClient();
const config = require('./config.json')
const users = [];
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', async message => {
if (message.content === '!addme') { // When doing this command, The bot will notifies you when the user or you posted a new post in ig
if (!users.includes(message.author.id)) {
users.push(message.author.id);
await message.reply('You have been added to the notification list!');
} else {
await message.reply('You are already in the notification list!');
}
}
});
async function getLatestPost() {
const user = await igClient.user.usernameinfo('THE USERNAME YOU WANT TO GET NOTIFIED');
const feed = igClient.feed.user(user.pk);
const posts = await feed.items();
return posts[0];
}
async function notifyUsers(post) {
const embed = new Discord.MessageEmbed()
.setAuthor(post.user.username)
.setDescription(post.caption.text)
.setImage(post.image_versions2.candidates[0].url)
.setColor('#405DE6')
.setTimestamp();
users.forEach(async userId => {
try {
const user = await client.users.fetch(userId);
await user.send(embed);
} catch (error) {
console.error(error);
}
});
}
async function startPolling() {
await igClient.state.generateDevice(config.handlename);
await igClient.account.login(config.igusername, config.igpassword);
let latestPost = await getLatestPost();
while (true) {
await new Promise(resolve => setTimeout(resolve, 60000));
const post = await getLatestPost();
if (post.id !== latestPost.id) {
latestPost = post;
await notifyUsers(post);
}
}
}
client.login(config.token)
.then(() => startPolling())
.catch(console.error);