Compare commits
2 commits
19d121a572
...
2e27b99dbf
Author | SHA1 | Date | |
---|---|---|---|
|
2e27b99dbf | ||
|
cadec6cdfe |
3 changed files with 82 additions and 11 deletions
|
@ -12,11 +12,16 @@ export function makeBot() {
|
|||
client.on("MESSAGE_CREATE", (message) => {
|
||||
if (message.content.startsWith("$ ")) {
|
||||
const command = message.content.substring(2, message.content.length);
|
||||
handleCommand(command, (_streamName, streamContent) => {
|
||||
const result = handleCommand(command, (_streamName, streamContent) => {
|
||||
client.api("POST", `/channels/${message.channel_id}/messages`, {
|
||||
content: streamContent
|
||||
});
|
||||
});
|
||||
if (result.error) {
|
||||
client.api("POST", `/channels/${message.channel_id}/messages`, {
|
||||
content: `error: ${result.error}`
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -4,5 +4,65 @@ export default {
|
|||
},
|
||||
wine: function(ctx) {
|
||||
return `${ctx.streamInput + " " ?? ""}:wine_glass:`;
|
||||
},
|
||||
slot: function(ctx) {
|
||||
const template = ctx.argv.join(" ");
|
||||
const slot = ctx.streamInput ?? "";
|
||||
return template.replace("%%", slot);
|
||||
},
|
||||
times: function(ctx) {
|
||||
const times = parseInt(ctx.argv[0]);
|
||||
const text = ctx.streamInput;
|
||||
if (!times) {
|
||||
return "times: expected first argument to be the number of times to repeat text";
|
||||
}
|
||||
if (!text) {
|
||||
return "times: expected stream input";
|
||||
}
|
||||
|
||||
let out = "";
|
||||
for (let i = 0; i < times; i++) {
|
||||
out += text;
|
||||
}
|
||||
return out;
|
||||
},
|
||||
djson: function(ctx) {
|
||||
if (ctx.argc > 24) {
|
||||
return "djson: arbitrary nesting limit of 24 reached"
|
||||
}
|
||||
|
||||
if (ctx.argc < 1) {
|
||||
return "djson: expected one or more arguments";
|
||||
}
|
||||
|
||||
if (!ctx.streamInput) {
|
||||
return "djson: expected stream input";
|
||||
}
|
||||
|
||||
let json;
|
||||
try {
|
||||
json = JSON.parse(ctx.streamInput);
|
||||
} catch (e) {
|
||||
return "djson: failed to parse json, is it malformed?";
|
||||
}
|
||||
const fields = ctx.argv;
|
||||
|
||||
let val = json;
|
||||
for (let i = 0; i < fields.length; i++) {
|
||||
const field = fields[i];
|
||||
|
||||
if (field.includes("__")) {
|
||||
return "djson: security violation - field names that contain __ are disallowed";
|
||||
}
|
||||
|
||||
if (typeof val !== "object") {
|
||||
return "null";
|
||||
}
|
||||
val = val[field];
|
||||
}
|
||||
|
||||
val ??= "null";
|
||||
|
||||
return typeof val === "string" ? val : JSON.stringify(val);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -164,6 +164,7 @@ function interpretInstructions(instructions, displayFunc) {
|
|||
if (inst.inStream) {
|
||||
streamInput = streams.get(inst.inStream);
|
||||
}
|
||||
try {
|
||||
let streamOutput = commandFunc({
|
||||
argv: inst.args,
|
||||
argc: inst.args.length,
|
||||
|
@ -172,6 +173,12 @@ function interpretInstructions(instructions, displayFunc) {
|
|||
if (inst.outStream) {
|
||||
streams.set(inst.outStream, streamOutput);
|
||||
}
|
||||
} catch(e) {
|
||||
console.error("unhandled exception while running command", e);
|
||||
if (inst.outStream) {
|
||||
streams.set(inst.outStream, "error: unhandled internal exception");
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case InstructionType.DisplayStream: {
|
||||
|
@ -191,14 +198,13 @@ export function handleCommand(message, displayFunc) {
|
|||
const { error: tokError, tokens } = tokenize(message);
|
||||
if (tokError) {
|
||||
return {
|
||||
error: `error: tokenize: ${tokError}`
|
||||
error: `tokenize: ${tokError}`
|
||||
};
|
||||
}
|
||||
const { error: insError, instructions } = tokensToInstructions(tokens);
|
||||
console.log(instructions);
|
||||
if (insError) {
|
||||
return {
|
||||
error: `error: tokensToInstructions: ${insError}`
|
||||
error: `tokensToInstructions: ${insError}`
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue