This repository has been archived by the owner on Apr 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
143 lines (128 loc) · 4.02 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
const { Plugin } = require('powercord/entities');
const Constants = Object.freeze({
SEARCH_ENGINES: {
google: 'g',
lmgtfy: 'l',
bing: 'b',
yahoo: 'y',
aol: 'a',
ask: 'k',
duckduckgo: 'd',
snopes: 's',
startpage: 't'
},
SEARCH_TYPES: {
web: 'w',
images: 'i',
videos: 'v',
news: 'n',
shopping: 's'
}
});
const Settings = require('./Settings');
module.exports = class LMGTFY extends Plugin {
startPlugin () {
powercord.api.settings.registerSettings(this.entityID, {
category: this.entityID,
label: 'LMGTFY',
render: Settings
});
powercord.api.commands.registerCommand({
command: 'lmgtfy',
description: 'Let me Google that for you...',
usage: '{c} [--iie] [...search terms] <search engine> <search type>',
executor: this.handleCommand.bind(this),
autocomplete: this.handleAutocomplete.bind(this)
});
}
pluginWillUnload () {
powercord.api.settings.unregisterSettings(this.entityID);
powercord.api.commands.unregisterCommand('lmgtfy');
}
handleCommand (args) {
if (args.length < 1) {
return;
}
const iie = args[0].includes('--iie') ? !!args.splice(args.indexOf('--iie'), 1) : this.settings.get('iie', false);
const options = args.slice(-2).map(arg => arg.toLowerCase());
const params = {};
params.searchEngine = Constants.SEARCH_ENGINES.google;
for (const key of Object.keys(Constants)) {
if (key === 'SEARCH_ENGINES') {
for (const searchEngine of Object.keys(Constants[key])) {
for (let i = 0; i < options.length; i++) {
const match = options[i].toLowerCase() === searchEngine;
if (match) {
params.searchEngine = Constants.SEARCH_ENGINES[searchEngine];
args.splice(args.lastIndexOf(searchEngine), 1);
options.splice(i, 1);
break;
}
}
}
} else if (key === 'SEARCH_TYPES') {
for (const searchType of Object.keys(Constants[key])) {
for (let i = 0; i < options.length; i++) {
const match = options[i].toLowerCase() === searchType;
if (match) {
if (params.searchEngine === 'g') {
params.searchType = Constants.SEARCH_TYPES[searchType];
args.splice(args.lastIndexOf(searchType), 1);
}
}
}
}
}
}
const { searchEngine, searchType } = params;
const queryString = new URLSearchParams();
queryString.append('q', args.join(' '));
if (searchType) {
queryString.append('s', searchEngine);
queryString.append('t', searchType);
} else if (searchEngine !== 'g') {
queryString.append('s', searchEngine);
}
if (iie) {
queryString.append('iie', +iie);
}
return {
send: true,
result: `<https://lmgtfy.com/?${queryString}>`
};
}
handleAutocomplete (args) {
if (!this.settings.get('autocompletes', true) || args.length === 0) {
return false;
}
if (args[1] === void 0) {
return {
commands: [ {
command: 'Please input your search terms...',
instruction: true
} ]
};
}
const lastArg = args[args.length - 1];
const searchEngines = Object.keys(Constants.SEARCH_ENGINES);
const searchEngine = searchEngines.find(engine => args[args.lastIndexOf(engine)] === engine);
if (!searchEngine) {
return {
commands: searchEngines
.filter(engine => engine.includes(lastArg))
.map(engine => ({ command: engine })),
header: 'select a search engine...'
};
}
const searchTypes = Object.keys(Constants.SEARCH_TYPES);
const searchType = searchTypes.find(type => args[args.indexOf(searchEngine) + 1] === type);
if (searchEngine === 'google' && !searchType) {
return {
commands: searchTypes
.filter(type => type.includes(lastArg))
.map(type => ({ command: type })),
header: 'select a search type...'
};
}
}
};