commit 532652de17369477e08e7ddd7c9a6064fad87077
parent d28546c60095248a4750ee06ff7ba918d33b1cb5
Author: Demonstrandum <moi@knutsen.co>
Date: Wed, 18 Mar 2020 19:17:23 +0000
Only allow NSFW search results in NSFW channels.
Diffstat:
2 files changed, 13 insertions(+), 6 deletions(-)
diff --git a/lib/api/google.ts b/lib/api/google.ts
@@ -4,7 +4,8 @@ type CSE = {
kind: 'image' | 'web',
key: string,
id: string,
- query: string
+ query: string,
+ nsfw: boolean
};
// Cache grows from the bottom, and deletes from the top.
@@ -44,15 +45,17 @@ const web_search = (param : CSE) => new Promise((resolve, reject) => {
q: param.query,
searchType: (param.kind === 'web') ? undefined : param.kind,
start: 0,
- num: 1
+ num: 1,
+ safe: param.nsfw ? 'off' : 'active'
}).then(res => {
if (!res.data || !res.data.items || res.data.items.length === 0)
return reject('No such results found.')
const item = res.data.items[0];
const answer = `Search for ‘${param.query}’: ${item.link}\n>>> ${item.title}`;
- // Cache this query
- CACHE[param.query] = answer;
+ // Cache this query (DO NOT CACHE NSFW)
+ if (!param.nsfw)
+ CACHE[param.query] = answer;
return resolve(answer);
}).catch(e =>
reject(`No results, or API capped...\n\`\`\`\n${e}\n\`\`\``));
diff --git a/lib/main.ts b/lib/main.ts
@@ -337,23 +337,27 @@ export class SimpOMatic {
break;
} case 'search': {
const query = args.join(' ').toLowerCase();
+ const channel = message.channel as TextChannel;
web_search({
kind: 'web',
query,
key: SECRETS.google.api_key,
- id: SECRETS.google.search_id
+ id: SECRETS.google.search_id,
+ nsfw: channel.nsfw
}).then((res) => message.answer(res))
.catch(e => message.answer(e));
break;
} case 'image': {
const query = args.join(' ').toLowerCase();
+ const channel = message.channel as TextChannel;
web_search({
kind: 'image',
query,
key: SECRETS.google.api_key,
- id: SECRETS.google.search_id
+ id: SECRETS.google.search_id,
+ nsfw: channel.nsfw
}).then(res => message.answer(res))
.catch(er => message.answer(er));
break;