-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_send.js
246 lines (212 loc) · 7.98 KB
/
message_send.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import fetch from "node-fetch";
import { auth } from "./config/config.js";
import { Client, Intents, MessageEmbed, MessageAttachment } from "discord.js";
import { addTxHashesToDB } from "./bot.js";
import dotenv from "dotenv";
import sharp from "sharp";
dotenv.config();
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]
}); //create new client
const T = auth();
let success = true;
const createMessageEmbed = (tweetObject, attachment, hdUrl) => {
let marketPlacePhraseFinal;
const marketPlacePhraseOpenSea = `[View on OpenSea](https://opensea.io/assets/0x177ef8787ceb5d4596b6f011df08c86eb84380dc/${tweetObject.tokenId})`;
const marketPlacePhraseLooksRare = `[View on LooksRare](https://looksrare.org/collections/0x177EF8787CEb5D4596b6f011df08C86eb84380dC/${tweetObject.tokenId})`;
if (tweetObject.marketPlace === "OpenSea") {
marketPlacePhraseFinal = marketPlacePhraseOpenSea;
} else if (tweetObject.marketPlace === "LooksRare") {
marketPlacePhraseFinal = marketPlacePhraseLooksRare;
}
const msgEmbed = new MessageEmbed()
.setTitle(
`${
tweetObject.bundle
? `Smilesss #${tweetObject.tokenId} was bought in a bundle that had a total cost of ${tweetObject.price} ($${tweetObject.usdPrice}) on ${tweetObject.marketPlace}`
: `Smileesss #${tweetObject.tokenId} was bought for ${tweetObject.price} ($${tweetObject.usdPrice}) on ${tweetObject.marketPlace}`
}`
)
.addField(
"Seller",
`[${tweetObject.fromAddress}](https://opensea.io/${tweetObject.fromAddress})`,
true
)
.addField(
"Buyer",
`[${tweetObject.toAddress}](https://opensea.io/${tweetObject.toAddress})`,
true
)
.addField("OpenSea", marketPlacePhraseFinal, false)
.addField("6K Link", `[6k Link](${hdUrl})`);
return msgEmbed;
};
const sendTweets = async (tweetObjects) => {
while (tweetObjects.length > 0) {
try {
const { tokenId } = tweetObjects[0];
const response = await fetch(
`https://api.smilesss.com/metadata/smilesssvrs/${tokenId}`
);
const metadata = await response.json();
let metadata1;
await new Promise((r) => setTimeout(r, 2000));
if (process.env.PFPS === "true") {
const response1 = await fetch(
`http://api.smilesss.com/metadata/smilesssvrs/pfp/${tokenId}`
);
metadata1 = await response1.json();
} else {
const response1 = await fetch(
`https://api.smilesss.com/metadata/smilesssvrs/${tokenId}`
);
metadata1 = await response1.json();
}
console.log("---------------saving picture---------------");
const [b64Content, buffer, b64Content1, buffer1] = await savePhotoFromAPI(
metadata.image,
metadata1.image
);
tweetObjects[0].imageUrl = metadata.image;
const attachment = new MessageAttachment(buffer, "favicon.png");
const attachment1 = new MessageAttachment(buffer1, "favicon.png");
const msgEmbed = createMessageEmbed(
tweetObjects[0],
attachment,
metadata.image
);
if (!success) {
break;
}
if (process.env.SEND_TWEET === "true") {
console.log("---------------send tweet---------------");
const result = await postTweet(tweetObjects[0], b64Content, b64Content1);
if (!result) {
break;
}
await new Promise((r) => setTimeout(r, 2000));
}
if (process.env.SEND_DISCORD === "true") {
console.log("---------------send discord message---------------");
await new Promise((r) => setTimeout(r, 2000));
await new Promise((r) => setTimeout(r, 2000));
const channelProd = await client.channels.cache.get(
"911007274598813716"
);
if (process.env.PFPS === "true") {
await channelProd.send({
embeds: [msgEmbed],
files: [attachment, attachment1]
});
} else {
await channelProd.send({
embeds: [msgEmbed],
files: [attachment]
});
}
}
const dbResponse = await addTxHashesToDB(tweetObjects.shift());
if (!dbResponse) {
break;
}
await new Promise((r) => setTimeout(r, 45000));
} catch (e) {
success = false;
console.log(e);
await new Promise((r) => setTimeout(r, 10000));
throw "there was an error";
}
}
};
const postTweet = async (tweetObject, b64content1, b64content2) => {
let marketPlacePhraseFinal;
const marketPlacePhraseOpenSea = `https://opensea.io/assets/0x177ef8787ceb5d4596b6f011df08c86eb84380dc/${tweetObject.tokenId}`;
const marketPlacePhraseLooksRare = `https://looksrare.org/collections/0x177EF8787CEb5D4596b6f011df08C86eb84380dC/${tweetObject.tokenId}`;
if (tweetObject.marketPlace === "OpenSea") {
marketPlacePhraseFinal = marketPlacePhraseOpenSea;
} else if (tweetObject.marketPlace === "LooksRare") {
marketPlacePhraseFinal = marketPlacePhraseLooksRare;
}
let statusObj = {
status: `${
tweetObject.bundle
? `Smilesss #${tweetObject.tokenId} was bought in a bundle on ${tweetObject.marketPlace} that had a total cost of ${tweetObject.price} ($${tweetObject.usdPrice})`
: `Smilesss #${tweetObject.tokenId} was bought for ${tweetObject.price} ($${tweetObject.usdPrice}) on ${tweetObject.marketPlace}`
}
\n
\n${marketPlacePhraseFinal}
\n${Math.random().toString(36).substring(7)}`
};
try {
if (process.env.PFPS === "true") {
const mediaUpload1 = await T.post("media/upload", {
media_data: b64content1
});
const mediaIdString1 = mediaUpload1.data.media_id_string;
const mediaUpload2 = await T.post("media/upload", {
media_data: b64content2
});
const mediaIdString2 = mediaUpload2.data.media_id_string;
const mediaMetaDataCreate1 = await T.post("media/metadata/create", {
media_id: mediaIdString1,
alt_text: {
text: "image description1"
}
});
const mediaMetaDataCreate2 = await T.post("media/metadata/create", {
media_id: mediaIdString2,
alt_text: {
text: "image description2"
}
});
const response = await T.post("statuses/update", {
status: statusObj.status,
media_ids: [mediaIdString1, mediaIdString2]
});
} else {
const mediaUpload1 = await T.post("media/upload", {
media_data: b64content1
});
const mediaIdString1 = mediaUpload1.data.media_id_string;
const mediaMetaDataCreate1 = await T.post("media/metadata/create", {
media_id: mediaIdString1,
alt_text: {
text: "image description1"
}
});
const response = await T.post("statuses/update", {
status: statusObj.status,
media_ids: [mediaIdString1]
});
}
return true;
} catch (e) {
console.log(e);
return false;
}
};
async function savePhotoFromAPI(imageUrl1, imageUrl2) {
console.log("Start creating buffer for 1");
const imageResponse1 = await fetch(imageUrl1);
const arrayBuffer1 = await imageResponse1.arrayBuffer();
const buffer1 = Buffer.from(arrayBuffer1);
const data1 = await sharp(buffer1).webp({ quality: 50 }).toBuffer();
const b641 = data1.toString("base64");
const newBuffer1 = Buffer.from(b641, "base64");
console.log("created buffer for 1");
console.log("waiting two seconds");
await new Promise((r) => setTimeout(r, 2000));
const imageResponse2 = await fetch(imageUrl2);
const arrayBuffer2 = await imageResponse2.arrayBuffer();
const buffer2 = Buffer.from(arrayBuffer2);
const data2 = await sharp(buffer2).webp({ quality: 50 }).toBuffer();
const b642 = data2.toString("base64");
const newBuffer2 = Buffer.from(b642, "base64");
console.log("finished creating buffer for both");
return [b641, newBuffer1, b642, newBuffer2];
}
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.login(process.env.CLIENT_TOKEN);
export { sendTweets };