Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
210 lines (186 loc) · 4.37 KB
/
Copy pathutils.js
File metadata and controls
210 lines (186 loc) · 4.37 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
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
var https = require( 'https' );
var fs = require( 'fs' );
var path = require( 'path' );
var fetchedIcons = {};
var generalOutputChannel;
var directMessagesServerName = function()
{
return "Direct Messages";
};
function initialize( outputChannel )
{
generalOutputChannel = outputChannel
}
function log( text )
{
if( generalOutputChannel )
{
generalOutputChannel.appendLine( new Date().toLocaleTimeString() + " " + text );
}
else
{
console.log( text );
}
}
function toParentId( channel )
{
if( !channel )
{
return undefined;
}
if( channel.guild )
{
return channel.guild.id.toString();
}
return "directMessages";
}
function toServerName( channel )
{
if( !channel )
{
return undefined;
}
return channel.guild ? channel.guild.name : directMessagesServerName();
}
function toChannelName( channel )
{
if( !channel )
{
return undefined;
}
var name = channel.name;
if( !name )
{
if( channel.type === "dm" )
{
name = "@" + channel.recipient.username;
}
else if( channel.type === "group" )
{
var names = [];
channel.recipients.map( recipient =>
{
names.push( "@" + recipient.username );
} );
name = names.join( ", " );
}
}
return name;
}
function toOutputChannelName( channel )
{
if( !channel )
{
return undefined;
}
var name = 'discord-chat.';
if( channel.guild )
{
name += channel.guild.name;
name += '.' + channel.name;
}
else
{
name += "dm";
name += '.' + toChannelName( channel );
}
return name;
}
function toHash( text )
{
var hash = 0;
for( var i = 0; i < text.length; i++ )
{
hash = text.charCodeAt( i ) + ( ( hash << 5 ) - hash );
}
return hash;
}
var toDarkColour = function( text )
{
var colour = '#';
var hash = toHash( text );
for( var i = 0; i < 3; i++ )
{
var value = ( hash >> ( i * 4 ) ) & 0x80;
colour += ( '00' + value.toString( 16 ) ).substr( -2 );
}
return colour;
}
var toLightColour = function( text )
{
var hash = toHash( text );
var colour = '#';
for( var i = 0; i < 3; i++ )
{
var value = ( ( hash >> ( i * 4 ) ) & 0x7f ) + 0x80;
colour += ( '00' + value.toString( 16 ) ).substr( -2 );
}
return colour;
}
var isReadableChannel = function( user, channel )
{
if( channel && user )
{
if( channel.type === 'dm' )
{
return true;
}
else if( channel.type === 'group' )
{
return channel.recipients.size > 0;
}
else if( channel.type === 'text' )
{
var permissions = channel.permissionsFor( user );
return permissions.has( "VIEW_CHANNEL" )
}
}
return false;
}
function fetchIcon( url, filename, callback )
{
if( !fetchedIcons[ url ] )
{
var file = fs.createWriteStream( filename );
var request = https.get( url, function( response )
{
response.pipe( file );
file.on( 'finish', function()
{
fetchedIcons[ url ] = true;
file.close( callback ); // close() is async, call cb after close completes.
} );
} );
}
else
{
callback();
}
}
function urlExt( url )
{
var filePath = url;
var queryIndex = filePath.indexOf( '?' );
if( queryIndex > -1 )
{
filePath = filePath.substr( 0, queryIndex );
}
return path.extname( filePath );
}
function messageTimeFormat( message )
{
return ( new Date() - message.createdAt > 1000 * 60 * 60 * 24 * 7 ) ? "%D %T" : "%a %T";
}
module.exports.initialize = initialize;
module.exports.log = log;
module.exports.toParentId = toParentId;
module.exports.toServerName = toServerName;
module.exports.toChannelName = toChannelName;
module.exports.toOutputChannelName = toOutputChannelName;
module.exports.toDarkColour = toDarkColour;
module.exports.toLightColour = toLightColour;
module.exports.isReadableChannel = isReadableChannel;
module.exports.directMessagesServerName = directMessagesServerName;
module.exports.fetchIcon = fetchIcon;
module.exports.urlExt = urlExt;
module.exports.messageTimeFormat = messageTimeFormat;