-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·145 lines (124 loc) · 3.79 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
140
141
142
143
144
145
var google = require('googleapis');
var path = require('path');
var fs = require('fs');
var SCOPES = ['https://www.googleapis.com/auth/drive.file'];
var OAUTH;
var OAuth2 = google.auth.OAuth2;
module.exports = function(SupinBot) {
var config = require('./config.js')(SupinBot.config);
function getToken() {
var url = OAUTH.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
});
SupinBot.postMessage(config.get('channel'), 'New token requested, enter the OAuth2 code with the !redeemdocscode <OAuth2 Code> command.\n' + url);
}
function getOAuth() {
OAUTH = new OAuth2(config.get('client_id'), config.get('client_secret'), config.get('redirect_uri'));
if (config.get('token')) {
OAUTH.setCredentials({refresh_token: config.get('token')});
} else {
getToken();
}
}
getOAuth();
SupinBot.CommandManager.addCommand('redeemdocscode', function(user, channel, args, argsStr) {
OAUTH.getToken(args[0], function(err, token) {
if (err) {
SupinBot.log.error('Google Drive API error: ' + err);
SupinBot.postMessage(config.get('channel'), 'An error occured while redeeming the OAuth2 code.\n' + err);
return;
}
config.set('token', token.refresh_token);
SupinBot.postMessage(config.get('channel'), 'Token successfully redeemed.\nRemember to add the following entry to .env:\nSUPINBOT_GOOGLE_REFRESH_TOKEN=' + token.refresh_token);
});
})
.setDescription('Uses the given code to get a Google Drive access token.')
.addArgument('OAuth2 Code', 'string')
.channelRestriction(['dev'])
.ownerOnly();
SupinBot.CommandManager.addCommand('shownotes', function(user, channel, args, argsStr) {
google.drive('v3').files.list({
auth: OAUTH,
q: '\'' + config.get('folder') + '\' in parents',
fields: 'files(name,webViewLink)'
}, function(err, res) {
if (err) {
SupinBot.log.error('Google Drive API: ' + err);
SupinBot.postMessage(channel.id, 'Google Drive API: ' + err);
return;
}
if (res.files.length === 0) {
SupinBot.postMessage(channel.id, null, {
attachments: [
{
title: 'No notes found.',
color: '#EA4335'
}
]
});
} else {
var message = '';
res.files.forEach(function(file) {
message = message + '\n<' + file.webViewLink + '|' + file.name + '>';
});
SupinBot.postMessage(channel.id, null, {
attachments: [
{
title: 'Google Drive Notes',
title_link: 'https://drive.google.com/open?id=' + config.get('folder'),
text: message,
color: '#4688F4'
}
]
});
}
});
})
.setDescription('Shows all created notes.');
SupinBot.CommandManager.addCommand('createnote', function(user, channel, args, argsStr) {
google.drive('v3').files.create({
auth: OAUTH,
resource: {
name: args[0],
parents: [config.get('folder')],
mimeType: 'application/vnd.google-apps.document'
},
fields: 'id,webViewLink'
}, function(err, file) {
if (err) {
SupinBot.log.error('Google Drive API: ' + err);
SupinBot.postMessage(channel.id, 'Google Drive API: ' + err);
return;
}
google.drive('v3').permissions.create({
auth: OAUTH,
fileId: file.id,
resource: {
role: 'writer',
type: 'anyone',
allowFileDiscovery: false
}
}, function(err, permission) {
if (err) {
SupinBot.log.error('Google Drive API: ' + err);
SupinBot.postMessage(channel.id, 'Google Drive API: ' + err);
return;
}
SupinBot.log.info(args[0] + ' Created with ID: ' + file.id);
SupinBot.postMessage(channel.id, null, {
attachments: [
{
title: args[0] + ' Created',
text: '<' + file.webViewLink + '|Click here to view it>.',
color: '#34A853'
}
]
});
});
});
})
.setDescription('Creates a new note.')
.addArgument('Note Name', 'string')
.adminOnly();
};