Last active
May 1, 2025 11:51
-
-
Save treuks/093e353a7c5b4926815d5d31a99885bb to your computer and use it in GitHub Desktop.
ooc.js
This file contains hidden or 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
// src/Supibot.res.mjs | |
var prefix = "OOC_MSGS"; | |
function getOoc() { | |
var data = channelCustomData.get(prefix); | |
if (data == null) { | |
return { | |
TAG: "Error", | |
_0: "NoData" | |
}; | |
} else if (data.messages.length === 0) { | |
return { | |
TAG: "Error", | |
_0: "NoMessages" | |
}; | |
} else { | |
return { | |
TAG: "Ok", | |
_0: data | |
}; | |
} | |
} | |
function initOoc() { | |
channelCustomData.set(prefix, { | |
currentId: 0, | |
messages: [] | |
}); | |
} | |
function oocErrorToStr(err) { | |
if (err === "NoMessages") { | |
return "It seems like there is initialised data but no messages. Try adding some messages with $$ooc add [\u2026]"; | |
} else { | |
return "Seems like you're running the command for the first time, I initialised some data, try adding a message with $$ooc add"; | |
} | |
} | |
var ChannelCustomData = { | |
getOoc, | |
initOoc, | |
oocErrorToStr | |
}; | |
// node_modules/.pnpm/[email protected]/node_modules/rescript/lib/es6/caml_option.js | |
function valFromOption(x) { | |
if (!(x !== null && x.BS_PRIVATE_NESTED_SOME_NONE !== void 0)) { | |
return x; | |
} | |
var depth = x.BS_PRIVATE_NESTED_SOME_NONE; | |
if (depth === 0) { | |
return; | |
} else { | |
return { | |
BS_PRIVATE_NESTED_SOME_NONE: depth - 1 | 0 | |
}; | |
} | |
} | |
// node_modules/.pnpm/@[email protected][email protected]/node_modules/@rescript/core/src/Core__Int.res.mjs | |
function fromString(x, radix) { | |
var maybeInt = radix !== void 0 ? parseInt(x, radix) : parseInt(x); | |
if (isNaN(maybeInt) || maybeInt > 2147483647 || maybeInt < -2147483648) { | |
return; | |
} else { | |
return maybeInt | 0; | |
} | |
} | |
// node_modules/.pnpm/[email protected]/node_modules/rescript/lib/es6/belt_Array.js | |
function concatMany(arrs) { | |
var lenArrs = arrs.length; | |
var totalLen = 0; | |
for (var i = 0; i < lenArrs; ++i) { | |
totalLen = totalLen + arrs[i].length | 0; | |
} | |
var result = new Array(totalLen); | |
totalLen = 0; | |
for (var j = 0; j < lenArrs; ++j) { | |
var cur = arrs[j]; | |
for (var k = 0, k_finish = cur.length; k < k_finish; ++k) { | |
result[totalLen] = cur[k]; | |
totalLen = totalLen + 1 | 0; | |
} | |
} | |
return result; | |
} | |
// node_modules/.pnpm/@[email protected][email protected]/node_modules/@rescript/core/src/Core__Error.res.mjs | |
function panic(msg) { | |
throw new Error("Panic! " + msg); | |
} | |
// node_modules/.pnpm/@[email protected][email protected]/node_modules/@rescript/core/src/Core__Option.res.mjs | |
function getExn(x, message) { | |
if (x !== void 0) { | |
return valFromOption(x); | |
} else { | |
return panic(message !== void 0 ? message : "Option.getExn called for None value"); | |
} | |
} | |
// node_modules/.pnpm/@[email protected][email protected]/node_modules/@rescript/core/src/Core__Result.res.mjs | |
function mapError(r, f) { | |
if (r.TAG === "Ok") { | |
return r; | |
} else { | |
return { | |
TAG: "Error", | |
_0: f(r._0) | |
}; | |
} | |
} | |
// node_modules/.pnpm/[email protected]/node_modules/rescript/lib/es6/caml_splice_call.js | |
var spliceApply = function(fn, args) { | |
var i, argLen; | |
argLen = args.length; | |
var applied = []; | |
for (i = 0; i < argLen - 1; ++i) { | |
applied.push(args[i]); | |
} | |
var lastOne = args[argLen - 1]; | |
for (i = 0; i < lastOne.length; ++i) { | |
applied.push(lastOne[i]); | |
} | |
return fn.apply(null, applied); | |
}; | |
// src/Main.res.mjs | |
function getRandomMessage(data) { | |
var messages = data.messages; | |
var randomNumber = utils.random(0, messages.length - 1 | 0); | |
return getExn(messages[randomNumber], "Couldn't get a random message from the messages"); | |
} | |
function formatMessageWithMsg(msg) { | |
var formattedDate = new Date(msg.date).toLocaleDateString("sv"); | |
return "(#" + msg.id.toString() + ") [" + formattedDate + "]: " + msg.text; | |
} | |
function formatRandomMessage(msg) { | |
return "\u{1F3B2} " + formatMessageWithMsg(msg); | |
} | |
function dataWithAddedMessage(data, msg, adder) { | |
var messageId = data.currentId + 1 | 0; | |
return { | |
currentId: messageId, | |
messages: concatMany([ | |
data.messages, | |
[{ | |
id: messageId, | |
text: msg, | |
date: (/* @__PURE__ */ new Date()).toISOString(), | |
addedBy: adder | |
}] | |
]) | |
}; | |
} | |
function dataWithRemovedMessageById(data, id) { | |
return { | |
currentId: data.currentId, | |
messages: concatMany([data.messages]).filter(function(m) { | |
return m.id !== id; | |
}) | |
}; | |
} | |
function updatePinnedDataWith(data) { | |
channelCustomData.set("OOC_MSGS", data); | |
} | |
function getMessageWithId(data, id) { | |
return data.messages.filter(function(d) { | |
return d.id === id; | |
}); | |
} | |
function getMaxIdInData(data) { | |
if (data.messages.at(data.currentId) !== void 0) { | |
return data.currentId; | |
} else { | |
return spliceApply(Math.max, [data.messages.map(function(m) { | |
return m.id; | |
})]) | 0; | |
} | |
} | |
function getLastMessage(data) { | |
var maxId = getMaxIdInData(data); | |
return getMessageWithId(data, maxId); | |
} | |
function isInMiddle(data, idx) { | |
var maxId = getMaxIdInData(data); | |
if (idx < maxId) { | |
return idx !== 0; | |
} else { | |
return false; | |
} | |
} | |
var getClosestId = (arr, target) => { | |
let left = 0; | |
let right = arr.length - 1; | |
let closest = arr[0]; | |
while (left <= right) { | |
const mid = Math.floor((left + right) / 2); | |
if (Math.abs(arr[mid] - target) < Math.abs(closest - target)) { | |
closest = arr[mid]; | |
} | |
if (arr[mid] === target) { | |
return arr[mid]; | |
} | |
if (arr[mid] < target) { | |
left = mid + 1; | |
} else { | |
right = mid - 1; | |
} | |
} | |
return closest; | |
}; | |
function getCloseSearchResults(data, needle) { | |
var haystack = data.messages.map(function(msg) { | |
return msg.text; | |
}); | |
var searchResults = utils.selectClosestString(needle, haystack, { ignoreCase: true, fullResult: true }); | |
if (searchResults == null) { | |
return; | |
} | |
var closestResults = searchResults.filter(function(res) { | |
return res.includes; | |
}); | |
var match = closestResults.length; | |
if (match !== 0) { | |
return closestResults; | |
} | |
} | |
function main(args) { | |
var data = ChannelCustomData.getOoc(); | |
var arg = args[0]; | |
var tmp; | |
if (arg !== void 0) { | |
var exit = 0; | |
switch (arg) { | |
case "get": | |
var res = mapError(data, function(oocError) { | |
return ChannelCustomData.oocErrorToStr(oocError); | |
}); | |
if (res.TAG === "Ok") { | |
if (args.length === 1) { | |
tmp = "You should provide an ID, like $$ooc get 1. Did you want to get a random message? Try doing $$ooc"; | |
} else { | |
var dat = res._0; | |
var xd = args[1]; | |
var exit$1 = 0; | |
switch (xd) { | |
case "last": | |
case "latest": | |
exit$1 = 3; | |
break; | |
default: | |
var num = fromString(xd, void 0); | |
if (num !== void 0) { | |
var msg = getMessageWithId(dat, num).at(0); | |
if (msg !== void 0) { | |
tmp = formatMessageWithMsg(msg); | |
} else { | |
var closestNumber = getClosestId(dat.messages.map(function(m) { | |
return m.id; | |
}), num); | |
tmp = isInMiddle(dat, num) ? "Looks like that message has been deleted. Did you mean #" + closestNumber.toString() + " ?" : "Couldn't find a message with that id. Did you mean #" + closestNumber.toString() + " ?"; | |
} | |
} else { | |
tmp = "Please provide a number instead of " + args[1]; | |
} | |
} | |
if (exit$1 === 3) { | |
var msg$1 = getLastMessage(dat).at(0); | |
tmp = msg$1 !== void 0 ? formatMessageWithMsg(msg$1) : "Tried to get a message that exists, but actually it doesn't exist. Please report this to @treuks"; | |
} | |
} | |
} else { | |
tmp = res._0; | |
} | |
break; | |
case "add": | |
case "pin": | |
exit = 1; | |
break; | |
case "search": | |
var res$1 = mapError(data, function(oocError) { | |
return ChannelCustomData.oocErrorToStr(oocError); | |
}); | |
if (res$1.TAG === "Ok") { | |
if (args.length === 1) { | |
tmp = "You need to put a string to search for after this."; | |
} else { | |
var dat$1 = res$1._0; | |
var messageText = args.slice(1).join(" "); | |
var searched = getCloseSearchResults(dat$1, messageText); | |
if (searched !== void 0) { | |
if (searched.length === 1) { | |
var searchMsg = getExn(searched[0], "Couldn't index into the searches array"); | |
var message = getExn(dat$1.messages[searchMsg.index], "Couldn't index into the messages array"); | |
tmp = formatMessageWithMsg(message); | |
} else { | |
var allChoices = searched.length - 1 | 0; | |
var randomIndex = utils.random(0, allChoices); | |
var leftNum = (randomIndex + 1 | 0).toString(); | |
var rightNum = searched.length.toString(); | |
var choiceThing = "[" + leftNum + "/" + rightNum + "]"; | |
var searchedMessage = getExn(searched[randomIndex], "Couldn't get a valid random search"); | |
var message$1 = getExn(dat$1.messages[searchedMessage.index], "Couldn't index into the array of messages"); | |
tmp = choiceThing + " " + formatMessageWithMsg(message$1); | |
} | |
} else { | |
tmp = "Couldn't find anything similar enough."; | |
} | |
} | |
} else { | |
tmp = res$1._0; | |
} | |
break; | |
case "delete": | |
case "remove": | |
case "unpin": | |
exit = 2; | |
break; | |
default: | |
tmp = "Sorry I don't understand what you're trying to do :( | Available commands: [add, remove, get, search]"; | |
} | |
switch (exit) { | |
case 1: | |
if (args.length === 1) { | |
tmp = "You didn't actually provide a message. Add some text after that"; | |
} else { | |
var messageText$1 = args.slice(1).join(" "); | |
if (data.TAG === "Ok") { | |
var newData = dataWithAddedMessage(data._0, messageText$1, executor); | |
channelCustomData.set("OOC_MSGS", newData); | |
tmp = "Pinned the message with ID: " + newData.currentId.toString(); | |
} else { | |
var emptyData_messages = []; | |
var emptyData = { | |
currentId: 0, | |
messages: emptyData_messages | |
}; | |
var newData$1 = dataWithAddedMessage(emptyData, messageText$1, executor); | |
channelCustomData.set("OOC_MSGS", newData$1); | |
tmp = "Pinned the message with ID: " + newData$1.currentId.toString(); | |
} | |
} | |
break; | |
case 2: | |
var res$2 = mapError(data, function(err) { | |
return ChannelCustomData.oocErrorToStr(err); | |
}); | |
if (res$2.TAG === "Ok") { | |
if (args.length === 1) { | |
tmp = "You should provide an ID, like $$ooc remove 1"; | |
} else { | |
var dat$2 = res$2._0; | |
var xd$1 = args[1]; | |
var exit$2 = 0; | |
switch (xd$1) { | |
case "last": | |
case "latest": | |
exit$2 = 3; | |
break; | |
default: | |
var num$1 = fromString(xd$1, void 0); | |
if (num$1 !== void 0) { | |
var msg$2 = getMessageWithId(dat$2, num$1).at(0); | |
if (msg$2 !== void 0) { | |
var messagesWithRemovedMessage = dataWithRemovedMessageById(dat$2, msg$2.id); | |
channelCustomData.set("OOC_MSGS", messagesWithRemovedMessage); | |
tmp = "Succesfully removed message with id " + msg$2.id.toString(); | |
} else { | |
var closestNumber$1 = getClosestId(dat$2.messages.map(function(m) { | |
return m.id; | |
}), num$1); | |
tmp = isInMiddle(dat$2, num$1) ? "Looks like that message has been deleted already. Did you mean #" + closestNumber$1.toString() + " ?" : "Couldn't find a message with that id. Did you mean #" + closestNumber$1.toString() + " ?"; | |
} | |
} else { | |
tmp = "Please provide a number instead of " + args[1]; | |
} | |
} | |
if (exit$2 === 3) { | |
var maxId = getMaxIdInData(dat$2); | |
var messagesWithRemovedMessage$1 = dataWithRemovedMessageById(dat$2, maxId); | |
if (messagesWithRemovedMessage$1.messages.length < dat$2.messages.length) { | |
channelCustomData.set("OOC_MSGS", messagesWithRemovedMessage$1); | |
tmp = "Succesfully removed last message (#" + maxId.toString() + ")"; | |
} else { | |
tmp = "Couldn't remove message with last ID. Report this to @treuks"; | |
} | |
} | |
} | |
} else { | |
tmp = res$2._0; | |
} | |
break; | |
} | |
} else { | |
var res$3 = mapError(data, function(oocError) { | |
return ChannelCustomData.oocErrorToStr(oocError); | |
}); | |
tmp = res$3.TAG === "Ok" ? formatRandomMessage(getRandomMessage(res$3._0)) : res$3._0; | |
} | |
return utils.unping(tmp); | |
} | |
var noPinnedMessages = "There aren't any pinned messages yet. You should try pinning something with $$ooc add [\u2026]"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment