-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmail.js
127 lines (106 loc) · 3.62 KB
/
mail.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var Discord = require('discord.js');
var Imap = require('imap'),
inspect = require('util').inspect;
const fs = require('fs');
var imap = new Imap({
user: "[email protected]",
password: "app-password-here", // create app password for your mail account from https://support.google.com/mail/answer/185833?hl=en
host: "imap.gmail.com",
port: "993",
tls: "true",
tlsOptions: { rejectUnauthorized: false }
});
const webhook = new Discord.WebhookClient({ url: 'https://discord.com/api/webhooks/XXXXXXXXXXXXXX/XXXXXXXXXXXXXXXX-XXXX-XXXXXXXXXXXXXXXXXXXXXX-XXXXX-XXXXXXXX' });
var lastlastone=[]
fs.readFile('mail.txt', 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
lastlastone=data.trim().split(",");
});
// const allIntents = new Discord.Intents(32767);
// const bot = new Discord.Client({intents: 32767})
// const bot = new Discord.Client({
// intents: ["GUILDS", "GUILD_VOICE_STATES", "GUILD_MEMBERS", "GUILD_BANS", "GUILD_MESSAGES", "DIRECT_MESSAGES"]
// });
// bot.login(config.token);
function openInbox(callback) {
imap.openBox('INBOX', true, callback);
}
var lastone=[];
// Send the newest message to discord
function sendNewest() {
openInbox(function(err, box) {
if (err) throw err;
// console.log(box.messages.total + ':*')
var f = imap.seq.fetch(box.messages.total + ':*', {
bodies: ['HEADER.FIELDS (FROM TO SUBJECT)', '1'],
struct: true
})
f.on('message', (message, index) => {
message.on('body', (stream, info) => {
var buffer = '', count = 0;
var prefix = '(#' + index + ') ';
stream.on('data', function(chunk) {
count += chunk.length;
buffer += chunk.toString('utf8');
// console.log(prefix + 'Body [%s] (%d/%d)', inspect(info.which), count, info.size);
});
stream.once('end', function() {
if(!(lastone.includes(buffer.length))){
if(!(lastlastone.includes(buffer.length.toString()))){
console.log(buffer.length)
if(buffer.length<=300){
webhook.send(buffer)
.catch();
// var channel = bot.channels.get(config.channel); // announcments channel
// channel.send(buffer);
webhook.send("https://cdn.discordapp.com/attachments/951810185205280781/954623686839775272/4M7IWwP.gif")
.catch();
// console.log(prefix + 'Body [%s] Finished', inspect(info.which));
} }
lastone.push(buffer.length);
fs.writeFile('mail.txt', lastone.toString(), err => {
if (err) {
console.error(err);
}
});
}
});
});
});
f.once('error', function(err) {
console.log('Fetch error: ' + err);
});
f.once('end', function() {
console.log('Done fetching all messages!');
// imap.end();
});
});
}
imap.on('end', function() {
console.log("here");
imap.on('mail', mail => {
sendNewest();
});
//sendNewest();
});
imap.once('error', function(err) {
console.log("err here");
console.log(err);
});
imap.once('end', function() {
console.log('Connection endingg');
imap.on('mail', mail => {
sendNewest();
});
});
imap.once('ready', function() {
imap.on('mail', mail => {
sendNewest();
});
//imap.start()
sendNewest();
});
imap.connect();