-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
106 lines (92 loc) · 2.44 KB
/
utils.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
const getUrls = require('get-urls');
const discord = require('discord.js');
const isImageUrl = require('is-image-url');
function getEmbedUrl(message) {
var link = undefined;
message.embeds.forEach(embed => {
if (embed.url) {
link = embed.url;
}
});
return link;
}
function getPostedUrl(message) {
if (message instanceof discord.Message) {
const embedUrl = getEmbedUrl(message);
if (embedUrl) {
return embedUrl;
}
}
const text = message instanceof discord.Message ? message.content : message;
const urlSet = getUrls(text);
if (urlSet.size > 0) {
return urlSet.values().next().value;
}
return null;
}
function getChannel(guild, channelName) {
const channel = guild.channels.find('name', channelName);
if (channel) {
return channel;
}
throw new Error(
`Couldn't find channel ${channelName} in guild ${guild.name}`
);
}
function postEmbedToChannel(guild, embed, channelName) {
const channel = getChannel(guild, channelName);
if (channel) {
channel.send({ embed });
}
}
function postTextToChannel(guild, text, channelName) {
const channel = getChannel(guild, channelName);
if (channel) {
channel.send(text);
}
}
function getMessageLink(message) {
if (!message || !message.guild || !message.channel) {
return null;
}
return buildMessageLink(message.guild.id, message.channel.id, message.id);
}
function buildMessageLink(guildId, channelId, messageId) {
return `https://discordapp.com/channels/${guildId}/${channelId}/${messageId}`;
}
async function formatImageLinkAsMessage(link) {
// Link is bare image link
if (isImageUrl(link, false)) {
return new discord.RichEmbed().setImage(link);
}
// Link is "disguised" image link
if (isImageUrl(link, true)) {
const res = await fetch(link);
if (res.ok) {
const buffer = await res.buffer();
return {
files: [new discord.Attachment(Buffer.from(buffer), 'attachment')],
};
}
return link;
}
// Fallback to just posting message link
return link;
}
// Takes in a string with arguments seperated by '-' and returns an array of arguments
// Allows the delimiter to be escaped with \
function argparse(str) {
return str
.replace(/\\?\s\-\s/g, t => (t == ' - ' ? '\u000B' : ' - '))
.split('\u000B');
}
module.exports = {
buildMessageLink,
formatImageLinkAsMessage,
getEmbedUrl,
getMessageLink,
getPostedUrl,
postEmbedToChannel,
postTextToChannel,
argparse,
};