-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
139 lines (121 loc) · 3.56 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
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
const dotenv = require("dotenv");
const Discord = require("discord.js");
const Airtable = require("airtable");
const Twitter = require("twitter");
dotenv.config();
const {
DISCORD_BOT_TOKEN,
AIRTABLE_API_KEY,
AIRTABLE_BASE_ID,
AIRTABLE_TABLE_NAME,
TWITTER_CONSUMER_KEY,
TWITTER_CONSUMER_SECRET,
TWITTER_TOKEN_KEY,
TWITTER_TOKEN_SECRET,
RUNTIME_MODE,
} = process.env;
const DiscordClient = new Discord.Client();
Airtable.configure({ apiKey: AIRTABLE_API_KEY });
const base = Airtable.base(AIRTABLE_BASE_ID);
const TwitterClient = new Twitter({
consumer_key: TWITTER_CONSUMER_KEY,
consumer_secret: TWITTER_CONSUMER_SECRET,
access_token_key: TWITTER_TOKEN_KEY,
access_token_secret: TWITTER_TOKEN_SECRET,
});
DiscordClient.once("ready", () => {
console.log("Listening");
});
DiscordClient.on("messageReactionAdd", (messageReaction, _user) => {
if (messageReaction.count === 1 && messageReaction.emoji.name === "🔖") {
const { content, author, createdTimestamp, channel, guild, id } =
messageReaction.message;
const { username } = author;
const { id: channelId } = channel;
if (content) {
const urls = getAllUrls(content);
if (urls.length > 0) {
const entries = urls.map((url) => ({
url,
sharer: username,
timestamp: new Date(createdTimestamp).toISOString(),
content,
channel: channel.name,
}));
entries.forEach(({ channel, content, sharer, timestamp, url }) => {
const fields = {
url,
sharer,
timestamp,
channel,
content,
permalink: getDiscordPermalink(guild.id, channelId, id),
};
console.log("writing", fields);
if (RUNTIME_MODE !== "dev") {
sendTweet(fields);
writeToAirtable(fields);
} else {
console.info(
`Not Tweeting and writing to airtable because RUNTIME_MODE=${RUNTIME_MODE}.`
);
}
messageReaction.message.react("🐦");
});
} else {
console.log("No URLs found in", content);
messageReaction.message.react("❌");
}
}
}
});
DiscordClient.login(DISCORD_BOT_TOKEN);
const URL_REGEX =
/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[.\!\/\\w]*))?)/gi;
/**
* Returns all the URLs in message.
* @param {string} message
*/
const getAllUrls = (message) => {
const urls = message.match(URL_REGEX);
if (!urls) {
return [];
}
return urls.map((url) => url);
};
const getDiscordPermalink = (serverId, channelId, messageId) =>
`https://discord.com/channels/${serverId}/${channelId}/${messageId}`;
const TWITTER_MAX_TWEET_LENGTH = 280;
const sendTweet = ({ url, sharer, channel, content }) => {
const statusPreamble = `${sharer}#${channel}`;
const longMessage = `${statusPreamble}: "${content}"`;
const shortMessage = `${statusPreamble} shared ${url}`;
const status =
longMessage.length < TWITTER_MAX_TWEET_LENGTH ? longMessage : shortMessage;
console.log("Tweeting:", status);
TwitterClient.post(
"statuses/update",
{ status },
function (error, _tweet, response) {
if (error) {
console.error(error);
}
}
);
};
const writeToAirtable = (fields) => {
console.log("Writing to Airtable");
base(AIRTABLE_TABLE_NAME).create(
[
{
fields,
},
],
function (err) {
if (err) {
console.error(err);
return;
}
}
);
};