Last active
November 7, 2024 19:31
-
-
Save treuks/093e353a7c5b4926815d5d31a99885bb to your computer and use it in GitHub Desktop.
ooc.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
// ============================================================================================ | |
/** | |
* Wade v0.3.3 | |
* Copyright 2017-2018 Kabir Shah | |
* Released under the MIT License | |
* https://github.com/kbrsh/wade | |
*/ | |
const whitespaceRE=/\s+/g;let config={stopWords:["about","after","all","also","am","an","and","another","any","are","as","at","be","because","been","before","being","between","both","but","by","came","can","come","could","did","do","each","for","from","get","got","has","had","he","have","her","here","him","himself","his","how","if","in","into","is","it","like","make","many","me","might","more","most","much","must","my","never","now","of","on","only","or","other","our","out","over","said","same","see","should","since","some","still","such","take","than","that","the","their","them","then","there","these","they","this","those","through","to","too","under","up","very","was","way","we","well","were","what","where","which","while","who","with","would","you","your","a","i"],punctuationRE:/[@!"',.:;?()[\]]/g,processors:[function(e){return e.toLowerCase()},function(e){return e.replace(config.punctuationRE,"")},function(e){var t=config.stopWords,r=getTerms(e);let n=r.length;for(;0!=n--;)-1!==t.indexOf(r[n])&&r.splice(n,1);return r.join(" ")}]};const stringify=function(r){let n="[",i="",o=0;for(let t=0;t<r.length;t++){let e=r[t];void 0===e?o++:("number"!=typeof e&&(e=stringify(e)),0<o&&(n+=i+"@"+o,o=0,i=","),n+=i+e,i=",")}return n+"]"},parse=function(t){var e=[],r=[e];let n=1;for(;0!==r.length;){var i=r[r.length-1];let e="";for(;n<t.length;n++){var o=t[n];if(","===o){if(0!==e.length){if("@"===e[0]){var a=parseInt(e.substring(1));for(let e=0;e<a;e++)i.push(void 0)}else i.push(parseFloat(e));e=""}}else{if("["===o){var h=[];i.push(h),r.push(h),n++;break}if("]"===o){r.pop(),n++;break}e+=o}}0!==e.length&&i.push(parseInt(e))}return e},getTerms=function(e){e=e.split(whitespaceRE);return 0===e[0].length&&e.shift(),0===e[e.length-1].length&&e.pop(),e},processEntry=function(t){if(0!==t.length){var r=config.processors;for(let e=0;e<r.length;e++)t=r[e](t)}return t},update=function(t,r,n,i){var o=i[1];for(let e=2;e<i.length;e++){var a,h=i[e],s=r[h];void 0===s?(a=t.length,t[r[h]=a]={index:h,score:o*n}):t[s].score+=o*n}},Wade=function(e){function t(e){var r=t.index,e=processEntry(e),n=[],i={};if(0===e.length)return n;{var o=getTerms(e),e=o.length,a=e-1,h=1/e;e:for(let e=0;e<a;e++){var s=o[e],u=s.length-1;let t=r;for(let e=0;e<=u;e++){var l=t[0][0],m=s.charCodeAt(e)+l;if(m<1||void 0===l&&e===u||void 0===t[m])continue e;t=t[m]}var f=t[0];1!==f.length&&update(n,i,h,f)}var v=o[a],d=v.length-1;let t=r;for(let e=0;e<=d;e++){var g=t[0][0],c=v.charCodeAt(e)+g;if(c<1||void 0===g&&e===d||void 0===t[c])break;t=t[c]}if(void 0!==t){var p=[t];for(let e=0;e<p.length;e++){var y=p[e],w=y[0];1!==w.length&&update(n,i,h,w);for(let e=1;e<y.length;e++){var x=y[e];void 0!==x&&p.push(x)}}}return n}}return Array.isArray(e)?t.index=Wade.index(e):t.index=parse(e),t};Wade.index=function(e){let t=0;var o={},a=[];for(let r=0;r<e.length;r++){var n=processEntry(e[r]);if(0!==n.length){var i=getTerms(n),h=i.length;for(let e=0;e<h;e++){var s=i[e],u=[];let t=o;for(let e=0;e<s.length;e++){var l=s.charCodeAt(e),m=l>>>8,l=255&l,f=(0!=m&&((void 0===t.minimum||m<t.minimum)&&(t.minimum=m),(void 0===t.maximum||m>t.maximum)&&(t.maximum=m),f=t[m],t=void 0===f?t[m]={}:f,u.push(m)),(void 0===t.minimum||l<t.minimum)&&(t.minimum=l),(void 0===t.maximum||l>t.maximum)&&(t.maximum=l),t[l]);t=void 0===f?t[l]={}:f,u.push(l)}a.push(r),a.push(h),a.push(u)}}t++}var r=o.minimum,v=o.maximum;let d=1,g;void 0!==r&&void 0!==v&&(d=v-r+2,g=1-r);var c=[],p=new Array(d);p[0]=[g];for(let e=0;e<a.length;e+=3){var y=a[e],w=a[e+1],x=a[e+2],b=x.length-1;let r=p,n=o;for(let t=0;t<b;t++){var A,E=x[t],W=E+r[0][0];let e=r[W];n=n[E],void 0===e&&(E=n.minimum,A=n.maximum,(e=r[W]=new Array(A-E+2))[0]=[1-E]),r=e}var k=x[b],R=k+r[0][0];let i=r[R];if(n=n[k],void 0===i){var k=n.minimum,C=n.maximum;let e=1,t;void 0!==k&&void 0!==C&&(e=C-k+2,t=1-k),i=r[R]=new Array(e),c.push(i[0]=[t,1/w,y])}else{C=i[0];1===C.length?(C.push(1/w),C.push(y),c.push(C)):(C[1]+=1/w,C.push(y))}}for(let e=0;e<c.length;e++){var T=c[e];T[1]=1.5-T[1]/t}return p},Wade.save=function(e){return stringify(e.index)},Wade.config=config,Wade.version="__VERSION__"; | |
// ============================================================================================ | |
// ============================================================================================ | |
/* | |
* Copyright (c) 2024, treuks <[email protected]>. | |
* | |
* SPDX-License-Identifier: MIT | |
*/ | |
const PREFIX = "OOC_MSGS" | |
const ALIAS_NAME = "ooc" | |
// stolen from duckduckgo "ai" pepeLaugh | |
const formatPastDate = (date) => { | |
const rtf = new Intl.RelativeTimeFormat('en', { | |
numeric: 'auto', | |
}); | |
const now = new Date() | |
const diff = now - date | |
const seconds = Math.floor(diff / 1000) | |
const minutes = Math.floor(seconds / 60) | |
const hours = Math.floor(minutes / 60) | |
const days = Math.floor(hours / 24) | |
const months = Math.floor(days / 30) | |
const years = Math.floor(months / 12) | |
if (years > 0) { return rtf.format(-years , 'year') } | |
else if (months > 0) { return rtf.format(-months , 'month') } | |
else if (days > 0) { return rtf.format(-days , 'day') } | |
else if (hours > 0) { return rtf.format(-hours , 'hour') } | |
else if (minutes > 0) { return rtf.format(-minutes , 'minute') } | |
else { return rtf.format(-seconds , 'second') } | |
} | |
const getPinnedMessages = async () => { | |
return channelCustomData.get(PREFIX) | |
} | |
const pinsWithAddedMsg = ( data, msg ) => { | |
const msgId = data.currentId + 1 | |
return { | |
currentId: msgId, | |
messages: [ ...data.messages, { id: msgId, text: msg, date: new Date().toISOString() } ] | |
} | |
} | |
const removeById = ( data, id ) => { | |
return { | |
currentId: data.currentId, | |
messages: [...data.messages].filter((x) => x.id !== id) | |
} | |
} | |
const searchForId = ( data, num ) => { | |
return data.messages.filter((d) => d.id === num) | |
} | |
const setPinnedMessages = async ( messages ) => { | |
return channelCustomData.set(PREFIX, messages) | |
} | |
const initData = { currentId: 0, messages: [] } | |
const getRandomPin = ( data ) => { | |
const randomMessage = utils.randArray(data.messages) | |
const pastDate = formatPastDate(new Date(randomMessage.date)) | |
return `Random pinned message (#${randomMessage.id}) from ${pastDate}: ${randomMessage.text }` | |
} | |
const textsFromData = ( data ) => { | |
return data.messages.map((a) => a.text) | |
} | |
const main = async ( args ) => { | |
const Data = await getPinnedMessages() | |
if ( Data === undefined ) { | |
await setPinnedMessages(initData) | |
return "Looks like I'm running for the first time, so I just initialised some stuff. " | |
} | |
if ( args[0] === undefined ) { | |
return utils.unping(getRandomPin(Data)) | |
} | |
if ( args[0] === "add" || args[0] === "pin" ) { | |
if (args.length === 1) { | |
return `Did you want to add a pinned message? Try doing $$${ALIAS_NAME} add {message}` | |
} | |
const msgText = args.slice(1).join(" ") | |
const updatedData = pinsWithAddedMsg(Data, msgText) | |
await setPinnedMessages( updatedData ) | |
return `Pinned the message with ID: ${updatedData.currentId}` | |
} else if ( args[0] === "get" ) { | |
if ( Data.messages.length == 0 ) { | |
return `It looks like there aren't any pinned messages yet. You can pin something with $$${ALIAS_NAME} add {message}` | |
} | |
if (args.length === 1 ) { | |
return `You should provide an ID, like: $$${ALIAS_NAME} get 1. Did you want to get a random message? Try doing just "$$${ALIAS_NAME}"` | |
} | |
const arg1 = Number(args[1]) | |
if ( Number.isNaN(arg1) !== true ) { | |
const searched = searchForId(Data, arg1) | |
const searchedMsg = searched[0] | |
if ( searched.length === 0 ) { | |
return "Couldn't find anything with that ID. " | |
} | |
if ( searched.length > 1) { | |
return "Seems like there's more than 1 message with that ID. Someone needs to fix that :( " | |
} | |
const pastDate = formatPastDate(new Date(searchedMsg.date)) | |
return utils.unping(`ID ${arg1} (created ${pastDate}): ${searchedMsg.text }`) | |
} | |
} else if ( args[0] === "remove" || args[0] === "unpin" || args[0] === "delete") { | |
if (args.length === 1) { | |
return `You should provide an ID, like: $$${ALIAS_NAME} unpin 1` | |
} | |
const arg1 = Number(args[1]) | |
if ( Number.isNaN(arg1) !== true ) { | |
const searched = searchForId(Data, arg1) | |
if ( searched.length === 0 ) { | |
return "Couldn't find anything with that ID. " | |
} | |
if ( searched.length > 1) { | |
return "Seems like there's more than 1 message with that ID. Someone needs to fix that :( " | |
} | |
const intermPins = removeById(Data, arg1) | |
if ( intermPins.messages.length < Data.messages.length ) { | |
await setPinnedMessages(intermPins) | |
return "Succesfully unpinned a message with that ID!" | |
} else { | |
return "I couldn't remove that.. somehow.. :( This should be unreachable" | |
} | |
} | |
} else if ( args[0] === "search") { | |
if (args.length === 1) { | |
return "Try putting some text after this command" | |
} | |
const msgText = args.slice(1).join(" ") | |
const search = Wade(textsFromData(Data)) | |
const searchResponse = search(msgText) | |
const { id, text, date } = Data.messages[searchResponse[0].index] | |
const pastDate = formatPastDate(new Date(date)) | |
return utils.unping(`ID ${id} (created ${pastDate}): ${text}`) | |
} else if ( Number.isNaN( Number(args[0]) ) !== true ){ | |
const num = Number(args[0]) | |
const searched = searchForId(Data, num) | |
const searchedMsg = searched[0] | |
if ( searched.length === 0 ) { | |
return "Couldn't find anything with that ID. " | |
} | |
if ( searched.length > 1) { | |
return "Seems like there's more than 1 message with that ID. Someone needs to fix that :( " | |
} | |
const pastDate = formatPastDate(new Date(searchedMsg.date)) | |
return utils.unping(`ID ${num} (created ${pastDate}): ${searchedMsg.text }`) | |
} else { | |
return "Sorry I don't understand what you're trying to do :( | Available commands: [add, remove, get, search]" | |
} | |
} | |
// ============================================================================================ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment