You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.5 KiB

11 months ago
11 months ago
11 months ago
  1. const { v4 } = require("uuid");
  2. const prisma = require("../utils/prisma");
  3. const CMD_REGEX = new RegExp(/[^a-zA-Z0-9_-]/g);
  4. const SlashCommandPresets = {
  5. formatCommand: function (command = "") {
  6. if (!command || command.length < 2) return `/${v4().split("-")[0]}`;
  7. let adjustedCmd = command.toLowerCase(); // force lowercase、
  8. if (!adjustedCmd.startsWith("/")) adjustedCmd = `/${adjustedCmd}`; // Fix if no preceding / is found.
  9. return `${adjustedCmd.slice(1).toLowerCase()}`; // replace any invalid chars with '-'
  10. // return `/${adjustedCmd.slice(1).toLowerCase().replace(CMD_REGEX, "-")}`
  11. },
  12. get: async function (clause = {}) {
  13. try {
  14. const preset = await prisma.slash_command_presets.findFirst({
  15. where: clause,
  16. });
  17. return preset || null;
  18. } catch (error) {
  19. console.error(error.message);
  20. return null;
  21. }
  22. },
  23. where: async function (clause = {}, limit) {
  24. try {
  25. const presets = await prisma.slash_command_presets.findMany({
  26. where: clause,
  27. take: limit || undefined,
  28. });
  29. return presets;
  30. } catch (error) {
  31. console.error(error.message);
  32. return [];
  33. }
  34. },
  35. // Command + userId must be unique combination.
  36. create: async function (userId = null, presetData = {}) {
  37. try {
  38. const existingPreset = await this.get({
  39. userId: userId ? Number(userId) : null,
  40. command: String(presetData.command),
  41. });
  42. if (existingPreset) {
  43. console.log(
  44. "SlashCommandPresets.create - preset already exists - will not create"
  45. );
  46. return existingPreset;
  47. }
  48. const preset = await prisma.slash_command_presets.create({
  49. data: {
  50. ...presetData,
  51. // This field (uid) is either the user_id or 0 (for non-multi-user mode).
  52. // the UID field enforces the @@unique(userId, command) constraint since
  53. // the real relational field (userId) cannot be non-null so this 'dummy' field gives us something
  54. // to constrain against within the context of prisma and sqlite that works.
  55. uid: userId ? Number(userId) : 0,
  56. userId: userId ? Number(userId) : null,
  57. },
  58. });
  59. return preset;
  60. } catch (error) {
  61. console.error("Failed to create preset", error.message);
  62. return null;
  63. }
  64. },
  65. getUserPresets: async function (userId = null) {
  66. try {
  67. return (
  68. await prisma.slash_command_presets.findMany({
  69. where: { userId: !!userId ? Number(userId) : null },
  70. orderBy: { createdAt: "asc" },
  71. })
  72. )?.map((preset) => ({
  73. id: preset.id,
  74. command: preset.command,
  75. prompt: preset.prompt,
  76. description: preset.description,
  77. }));
  78. } catch (error) {
  79. console.error("Failed to get user presets", error.message);
  80. return [];
  81. }
  82. },
  83. update: async function (presetId = null, presetData = {}) {
  84. try {
  85. const preset = await prisma.slash_command_presets.update({
  86. where: { id: Number(presetId) },
  87. data: presetData,
  88. });
  89. return preset;
  90. } catch (error) {
  91. console.error("Failed to update preset", error.message);
  92. return null;
  93. }
  94. },
  95. delete: async function (presetId = null) {
  96. try {
  97. await prisma.slash_command_presets.delete({
  98. where: { id: Number(presetId) },
  99. });
  100. return true;
  101. } catch (error) {
  102. console.error("Failed to delete preset", error.message);
  103. return false;
  104. }
  105. },
  106. };
  107. module.exports.SlashCommandPresets = SlashCommandPresets;