-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
171 lines (140 loc) · 4.27 KB
/
bot.js
File metadata and controls
171 lines (140 loc) · 4.27 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const EventEmitter = require('events');
const MastodonAPI = require('mastodon-api');
const wurl = require('wurl');
class Bot extends EventEmitter {
/**
* Constructor
* @param {string} api_url
* @param {string} access_token
* @param {array} to_listen
*/
constructor({api_url, access_token}, to_listen) {
super();
this.api_url = api_url || 'https://botsin.space/api/v1/';
this.access_token = access_token;
this.to_listen = to_listen;
this.listeners = new WeakMap();
}
/**
* Run the bot
*/
async start() {
this.M = new MastodonAPI({
api_url: this.api_url,
access_token: this.access_token
});
for (const listen of this.to_listen) {
this.listeners.set(listen, this.M.stream('streaming/' + listen.api_point));
this.listeners.get(listen).on('message', (msg) => {
if (msg.event === listen.event) {
this.emit(listen.emit_on, msg.data)
}
})
}
this.me = (await this.verify_credentials());
}
async verify_credentials() {
return (await this.M.get('accounts/verify_credentials')).data;
}
/**
* Return a Array of Following
* @returns {Array}
*/
async following_list() {
let result = [];
let next_id = 0;
for (let i = 0; true; i++) {
let test;
if (next_id === 0) {
test = await this.M.get('accounts/' + this.me.id + '/following', {limit: 80});
} else {
test = await this.M.get('accounts/' + this.me.id + '/following', {limit: 80, max_id: next_id});
}
next_id = parseInt(wurl('?max_id', test.resp.headers.link.split(",")[0].split(';')[0].slice(1, -1)));
result = result.concat(test.data);
if (test.data.length < 80) break;
}
return result;
}
/**
* Return a Array of Followers
* @returns {Array}
*/
async followers_list() {
let result = [];
let next_id = 0;
for (let i = 0; true; i++) {
let test;
if (next_id === 0) {
test = await client.M.get('accounts/' + client.me.id + '/followers', {limit: 80});
} else {
test = await client.M.get('accounts/' + client.me.id + '/followers', {limit: 80, max_id: next_id});
}
// Sorry for this brainfart...
// test.resp.headers.link = <https://crazynoisybizarre.town/api/v1/accounts/29127/following?max_id=2808>; rel="next", <https://crazynoisybizarre.town/api/v1/accounts/29127/following?since_id=4024>; rel="prev"
next_id = parseInt(wurl('?max_id', test.resp.headers.link.split(",")[0].split(';')[0].slice(1, -1)));
result = result.concat(test.data);
if (test.data.length < 80) break;
}
return result;
}
/**
* Send a new toot
* @param {string} toot
* @param {string|int} visibility
* @param {int|null} reply_to
*/
post(toot, visibility = "unlisted", reply_to) {
this.M.post('statuses', {
status: toot,
visibility: visibility,
in_reply_to: reply_to
})
}
/**
* Reply to an existing toot
* @param data
* @param status
* @param visibility
*/
reply(data, status, visibility) {
visibility = visibility || data.status.visibility;
this.post(`@${data.account.acct} ${status}`, visibility, data.status.id);
}
/**
* Fav an existing toot
* @param {int} id
*/
fav(id) {
this.M.post('statuses/' + id + '/favourite');
}
/**
* Follow a user
* @param {int} id
*/
follow(id) {
this.M.post('accounts/' + id + '/follow');
}
/**
* Unfollow a user
* @param {int} id
*/
unfollow(id) {
this.M.post('accounts/' + id + '/unfollow');
}
/**
* Mute an user
* @param {int} id
*/
mute_user(id) {
this.M.post('accounts/' + id + '/mute');
}
/**
* Block a domain
* @param {string} domain
*/
block_domain(domain) {
this.M.post('domain_blocks', {domain});
}
}
module.exports = Bot;