From 74408053dc05d20a1b1acde13b3ff73f8ccb4394 Mon Sep 17 00:00:00 2001 From: bignutty <3515180-bignutty@users.noreply.gitlab.com> Date: Tue, 12 Nov 2024 15:10:36 +0100 Subject: [PATCH] add autocomplete to dictionary and urbandictionary --- .../slash/search/urbandictionary.js | 4 ++- .../interaction/slash/utils/dictionary.js | 4 ++- .../autocomplete/googleDictionary.js | 34 +++++++++++++++++++ .../autocomplete/urbanDictionary.js | 24 +++++++++++++ labscore/parameters/index.js | 4 ++- 5 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 labscore/parameters/autocomplete/googleDictionary.js create mode 100644 labscore/parameters/autocomplete/urbanDictionary.js diff --git a/commands/interaction/slash/search/urbandictionary.js b/commands/interaction/slash/search/urbandictionary.js index 2932e7c..6e723d4 100644 --- a/commands/interaction/slash/search/urbandictionary.js +++ b/commands/interaction/slash/search/urbandictionary.js @@ -7,6 +7,8 @@ const { link, iconPill } = require('#utils/markdown') const { editOrReply } = require('#utils/message') const { STATICS } = require('#utils/statics') +const { urbanDictionary } = require('#parameters').autocomplete; + // TODO: Turn this into a general purpose permissions constant const { Permissions, ApplicationCommandOptionTypes } = require("detritus-client/lib/constants"); @@ -52,7 +54,7 @@ module.exports = { { name: 'term', description: 'Term to look up.', - type: ApplicationCommandOptionTypes.TEXT, + onAutoComplete: urbanDictionary, required: true }, { diff --git a/commands/interaction/slash/utils/dictionary.js b/commands/interaction/slash/utils/dictionary.js index bf3b217..5744079 100644 --- a/commands/interaction/slash/utils/dictionary.js +++ b/commands/interaction/slash/utils/dictionary.js @@ -7,6 +7,8 @@ const { acknowledge } = require('#utils/interactions'); const { link, iconPill, smallPill, icon, iconLinkPill, pill } = require('#utils/markdown') const { editOrReply } = require('#utils/message') +const { googleDictionary } = require('#parameters').autocomplete; + const { ApplicationCommandOptionTypes } = require('detritus-client/lib/constants'); const LABELS = { @@ -64,7 +66,7 @@ module.exports = { { name: 'term', description: 'Term to look up.', - type: ApplicationCommandOptionTypes.TEXT, + onAutoComplete: googleDictionary, required: true }, { diff --git a/labscore/parameters/autocomplete/googleDictionary.js b/labscore/parameters/autocomplete/googleDictionary.js new file mode 100644 index 0000000..45c6704 --- /dev/null +++ b/labscore/parameters/autocomplete/googleDictionary.js @@ -0,0 +1,34 @@ + +const { stringwrap } = require("#utils/markdown"); +const superagent = require("superagent"); + +module.exports = async (context)=>{ + let choices = []; + if(context.value){ + try { + let suggestions = await superagent.get("https://www.google.com/complete/search") + .query({ + client: "dictionary-widget", + hl: "en", + requiredfields: "corpus:en-us", + q: context.value + }) + + suggestions = JSON.parse(suggestions.text.substring(19,suggestions.text.length - 1)) + choices = suggestions[1].map((m)=>m[0]) + + // Additional checks + if(choices.includes(context.value.toLowerCase())) { + choices = choices.filter((m)=>m!==context.value.toLowerCase()) + choices.unshift(context.value.toLowerCase()) + } + }catch(e){ + // idk sucks ig? + } + } + + return context.respond({ choices: choices.splice(0, 20).map((l)=>({ + name: l, + value: l + }))}); +} \ No newline at end of file diff --git a/labscore/parameters/autocomplete/urbanDictionary.js b/labscore/parameters/autocomplete/urbanDictionary.js new file mode 100644 index 0000000..b7724d5 --- /dev/null +++ b/labscore/parameters/autocomplete/urbanDictionary.js @@ -0,0 +1,24 @@ + +const { stringwrap } = require("#utils/markdown"); +const superagent = require("superagent"); + +module.exports = async (context)=>{ + let choices = []; + if(context.value){ + try { + let suggestions = await superagent.get("https://api.urbandictionary.com/v0/autocomplete-extra") + .query({ + term: context.value + }) + + choices = suggestions.body.results + }catch(e){ + // idk sucks ig? + } + } + + return context.respond({ choices: choices.splice(0, 20).map((l)=>({ + name: `${l.term} - ${stringwrap(l.preview, 20, false)}`, + value: l.term + }))}); +} \ No newline at end of file diff --git a/labscore/parameters/index.js b/labscore/parameters/index.js index 3d04cfb..f0ccbce 100644 --- a/labscore/parameters/index.js +++ b/labscore/parameters/index.js @@ -1,5 +1,7 @@ module.exports = { autocomplete: { - translateLanguage: require('./autocomplete/translateLanguage') + googleDictionary: require('./autocomplete/googleDictionary'), + translateLanguage: require('./autocomplete/translateLanguage'), + urbanDictionary: require('./autocomplete/urbanDictionary') } } \ No newline at end of file