-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
148 lines (126 loc) · 4.59 KB
/
plugin.js
File metadata and controls
148 lines (126 loc) · 4.59 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
const Promise = require('bluebird');
const URL = require('url').URL;
const yargs = require('yargs');
// The name of the environment variable to check for the giphy API key
const API_KEY_ENV_VAR_NAME = 'GIPHY_API_KEY';
const _getNotice = function(message) {
return {
'intent': 'notice',
'query': true,
'message': message
};
};
var TennuGiphy = {
configDefaults: {
'giphy': {
// https://developers.giphy.com/docs/optional-settings/#rating
// See also https://www.npmjs.com/package/giphy-api#phrase-search
"minRating": "pg-13"
},
},
init: function(client, imports) {
var config = client.config("giphy");
if (!config['api-key'] && !process.env[API_KEY_ENV_VAR_NAME]) {
throw Error("API Key not found. config: giphy.api-key or environment variable GPIHY_API_KEY");
}
const apiKey = config['api-key'] || process.env[API_KEY_ENV_VAR_NAME];
const minRating = config['minRating'] || "pg-13";
const giphy = require("giphy-api")(apiKey);
const giphyHelp = '{{!}}giphy,giph,gif [-g|-pg|-pg13|-r|-y|-unrated|-nsfw] [<search phrase>]';
const helps = {
'giphy': [
giphyHelp
],
'giph': [
giphyHelp
],
'gif': [
giphyHelp
]
};
function getErrorResponse(message) {
return {
intent: 'notice',
query: true,
message: message
};
}
var giphyCommand = function(command) {
const parsedArgs = yargs.parse(command.message);
const hasSearchQuery = command.args.length === 0;
let rating = minRating;
if(parsedArgs.g) {
rating = "g";
} else if (parsedArgs.pg) {
rating = "pg";
} else if (parsedArgs.pg13) {
rating = "pg13";
} else if (parsedArgs.r) {
rating = "r";
} else if (parsedArgs.y) {
rating = "y";
} else if (parsedArgs.unrated) {
rating = "unrated";
} else if (parsedArgs.nsfw) {
rating = "nsfw";
}
return Promise.try(function() {
if (hasSearchQuery) {
console.debug(`Fetching '${rating}' rated random giphy`);
return giphy.random({
rating: rating
}).then(function(res) {
const urlStr = clearParams(res.data.images.original.url)
return formatResponse(res.data, urlStr);
});
} else {
console.debug(`Fetching '${rating}' rated giphy search`);
return giphy.search({
q: command.args.join(' '),
rating: rating,
limit: 1
}).then(function(res) {
if(res.pagination.count === 0) {
return _getNotice('No gifs found matching your search.');
}
const urlStr = clearParams(res.data[0].images.original.url)
return formatResponse(res.data[0], urlStr);
});
}
})
.catch(function(err) {
return getErrorResponse(err.message);
});
}
/**
* Clear params from a URL instance and return as a string.
* @param {string} url A url
* @return {string} A url as a string.
*/
function clearParams(url) {
new URL(url);
url.search = "";
return url.toString();
}
/**
* Takes an image data object from giphy and returns a formatted string with the URL and rating.
* @param {Object} data https://developers.giphy.com/docs/api/schema/#gif-object
* @param {string} url A url as a string.
* @return {String} human readable response
*/
function formatResponse(data, url) {
const rating = data.rating || "unrated";
return `Rated '${rating}' - ${url}`;
}
return {
handlers: {
'!giphy': giphyCommand,
'!giph': giphyCommand,
'!gif': giphyCommand
},
help: helps,
commands: ['giphy', 'giph', 'gif']
}
}
};
module.exports = TennuGiphy;