-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
157 lines (141 loc) · 3.89 KB
/
index.js
File metadata and controls
157 lines (141 loc) · 3.89 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
require("console-stamp")(console, {
colors: {
stamp: "yellow",
label: "cyan",
label: true,
metadata: "green"
}
});
const Discord = require("discord.js");
const { token, app, group_name } = require("./config.json");
const Twit = require("twit");
const client = new Discord.Client();
const base64 = require("node-base64-image");
var Twitter = new Twit({
consumer_key: app.consumer.key,
consumer_secret: app.consumer.secret,
access_token: app.access.token,
access_token_secret: app.access.secret
});
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("message", msg => {
if (msg.content === "ping") {
msg.reply("pong");
}
if (msg.channel.name == "success") {
if (msg.attachments || msg.content) {
const result = msg.attachments.map((res, i) => {
return res.proxyURL;
});
const cookGroup = `https://www.twitter.com/${group_name}`;
const success_msg = `Success from ${cookGroup} - By ${
msg.author.username
}`;
const splitMsg = msg.content.split(" ");
const newMsg = splitMsg.filter((result, i) => {
if (result.includes("https") || result.includes("http")) {
return result;
}
});
const obj = {
result: result[0],
content: newMsg[0]
};
// encoding image for twitter..
base64.encode(
obj.result || obj.content,
{
string: true
},
(err, image) => {
if (!err) {
post(image);
} else {
return false;
}
}
);
/* posting to twitter */
function post(img) {
Twitter.post(
"media/upload",
{
media_data: img
},
(err, data, response) => {
if (!err) {
var mediaIdStr = data.media_id_string;
var altText = success_msg;
var meta_params = {
media_id: mediaIdStr,
alt_text: { text: altText }
};
Twitter.post(
"media/metadata/create",
meta_params,
(err, data, response) => {
if (!err) {
var params = { status: altText, media_ids: [mediaIdStr] };
Twitter.post(
"statuses/update",
params,
(err, data, response) => {
const tweet_id = data.id_str;
console.log(
`Tweet Sent: https://twitter.com/${
data.user.screen_name
}/status/${data.id_str}`
);
// Favorite tweet
favorite(tweet_id);
// retweeting tweet
retweet(tweet_id);
}
);
} else {
return false;
}
}
);
} else {
return false;
}
}
);
}
/* Liking tweet */
function favorite(tweetid) {
Twitter.post(
"favorites/create",
{
id: tweetid
},
(err, data, response) => {
if (!err && response) {
console.log("Favorited Tweet with id " + tweetid);
return true;
}
}
);
}
/* Retweeting tweet */
function retweet(tweetid) {
Twitter.post(
"statuses/retweet/:id",
{
id: tweetid
},
(err, data, response) => {
if (!err && response) {
console.log("Retweeted tweet with id " + tweetid);
return true;
}
}
);
}
}
}
});
client.login(token);