| 1 | export class SlashCommandExecutionError extends Error { |
| 2 | /**@type {string} */ commandName; |
| 3 | /**@type {number} */ start; |
| 4 | /**@type {number} */ end; |
| 5 | /**@type {string} */ commandText; |
| 6 | |
| 7 | /**@type {string} */ text; |
| 8 | get index() { return this.start; } |
| 9 | |
| 10 | get line() { |
| 11 | return this.text.slice(0, this.index).replace(/[^\n]/g, '').length; |
| 12 | } |
| 13 | get column() { |
| 14 | return this.text.slice(0, this.index).split('\n').pop().length; |
| 15 | } |
| 16 | get hint() { |
| 17 | let lineOffset = this.line.toString().length; |
| 18 | let lineStart = this.index; |
| 19 | let start = this.index; |
| 20 | let end = this.index; |
| 21 | let offset = 0; |
| 22 | let lineCount = 0; |
| 23 | while (offset < 10000 && lineCount < 3 && start >= 0) { |
| 24 | if (this.text[start] == '\n') lineCount++; |
| 25 | if (lineCount == 0) lineStart--; |
| 26 | offset++; |
| 27 | start--; |
| 28 | } |
| 29 | if (this.text[start + 1] == '\n') start++; |
| 30 | offset = 0; |
| 31 | while (offset < 10000 && this.text[end] != '\n') { |
| 32 | offset++; |
| 33 | end++; |
| 34 | } |
| 35 | let hint = []; |
| 36 | let lines = this.text.slice(start + 1, end - 1).split('\n'); |
| 37 | let lineNum = this.line - lines.length + 1; |
| 38 | let tabOffset = 0; |
| 39 | for (const line of lines) { |
| 40 | const num = `${' '.repeat(lineOffset - lineNum.toString().length)}${lineNum}`; |
| 41 | lineNum++; |
| 42 | const untabbedLine = line.replace(/\t/g, ' '.repeat(4)); |
| 43 | tabOffset = untabbedLine.length - line.length; |
| 44 | hint.push(`${num}: ${untabbedLine}`); |
| 45 | } |
| 46 | hint.push(`${' '.repeat(this.index - lineStart + lineOffset + 1 + tabOffset)}^^^^^`); |
| 47 | return hint.join('\n'); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | constructor(cause, message, commandName, start, end, commandText, fullText) { |
| 52 | super(message, { cause }); |
| 53 | this.commandName = commandName; |
| 54 | this.start = start; |
| 55 | this.end = end; |
| 56 | this.commandText = commandText; |
| 57 | this.text = fullText; |
| 58 | } |
| 59 | } |