helper.ts 750 B

123456789101112131415161718192021222324
  1. interface ICommand {
  2. cmd: string;
  3. args: string[];
  4. }
  5. export default function (message: string): ICommand {
  6. message = message.trim();
  7. message = message.replace('\\\\', '\\0x5c');
  8. message = message.replace('\\\"', '\\0x22');
  9. message = message.replace('\\\'', '\\0x27');
  10. const strs = message.match(/'[\s\S]*?'|"[\s\S]*?"|\S*\[CQ:[\s\S]*?\]\S*|\S+/mg);
  11. const cmd = strs?.length ? strs[0].length ? strs[0].substring(0, 1) === '/' ? strs[0].substring(1) : '' : '' : '';
  12. const args = strs?.slice(1).map(arg => {
  13. arg = arg.replace(/^["']+|["']+$/g, '');
  14. arg = arg.replace('\\0x27', '\\\'');
  15. arg = arg.replace('\\0x22', '\\\"');
  16. arg = arg.replace('\\0x5c', '\\\\');
  17. return arg;
  18. });
  19. return {
  20. cmd,
  21. args,
  22. };
  23. }