Gradi02 commited on
Commit
d5f9903
·
unverified ·
2 Parent(s): 504a673b13ede6

Merge pull request #6 from Tobkubos/backend-setup

Browse files
Files changed (1) hide show
  1. index.js +27 -34
index.js CHANGED
@@ -51,7 +51,7 @@ client.once(Events.ClientReady, async () => {
51
  type: ApplicationCommandType.ChatInput
52
  },
53
  {
54
- name: "Przeanalizuj tekst",
55
  type: ApplicationCommandType.Message
56
  }
57
  ]);
@@ -77,46 +77,36 @@ async function fetchAvailableModels() {
77
  return null;
78
  }
79
 
80
- function preparePayload(input) {
81
  const trimmed = input.trim();
 
 
 
 
 
 
 
 
 
 
 
82
  const isUrl = trimmed.startsWith("http://") || trimmed.startsWith("https://");
83
 
84
  if (isUrl) {
85
- const lowerUrl = trimmed.toLowerCase();
86
 
87
- if (lowerUrl.endsWith(".png") || lowerUrl.endsWith(".jpg") || lowerUrl.endsWith(".jpeg") || lowerUrl.endsWith(".webp") || lowerUrl.endsWith(".gif")) {
88
- return {
89
- type: "image",
90
- payload: {
91
- image_url: trimmed,
92
- content_type: "image"
93
- }
94
- };
95
- } else if (lowerUrl.endsWith(".mp4") || lowerUrl.endsWith(".webm") || lowerUrl.endsWith(".mov") || lowerUrl.endsWith(".avi")) {
96
- return {
97
- type: "video",
98
- payload: {
99
- video_url: trimmed,
100
- content_type: "video"
101
- }
102
- };
103
  } else {
104
- return {
105
- type: "file",
106
- payload: {
107
- file_url: trimmed,
108
- content_type: "file"
109
- }
110
- };
111
  }
112
  }
113
 
114
  return {
115
  type: "text",
116
- payload: {
117
- text: trimmed,
118
- content_type: "text"
119
- }
120
  };
121
  }
122
 
@@ -217,13 +207,13 @@ async function sendLogToDiscord(guild, embedToSend) {
217
  }
218
  }
219
 
220
- async function handleAnalysis(interaction, userContent, targetMessage = null) {
221
  await interaction.deferReply({ flags: [MessageFlags.Ephemeral] });
222
 
223
  const serverConfig = loadConfig(interaction.guildId);
224
 
225
  try {
226
- const { type, payload } = preparePayload(userContent);
227
 
228
  // DYNAMICZNE POBIERANIE MODELU Z PLIKU KONFIGURACYJNEGO DLA DANEGO FORMATU (np. text, image, video)
229
  const chosenModel = serverConfig.models[type];
@@ -398,14 +388,17 @@ client.on(Events.InteractionCreate, async (interaction) => {
398
  }
399
 
400
  if (interaction.isMessageContextMenuCommand()) {
401
- if (interaction.commandName === "Przeanalizuj tekst") {
402
  const targetMessage = interaction.targetMessage;
403
 
404
  let contentToAnalyze = targetMessage.content;
 
 
405
  const attachment = targetMessage.attachments.first();
406
 
407
  if (attachment) {
408
  contentToAnalyze = attachment.url;
 
409
  }
410
 
411
  if (!contentToAnalyze || contentToAnalyze.trim().length === 0) {
@@ -415,7 +408,7 @@ client.on(Events.InteractionCreate, async (interaction) => {
415
  });
416
  }
417
 
418
- await handleAnalysis(interaction, contentToAnalyze, targetMessage);
419
  }
420
  }
421
 
 
51
  type: ApplicationCommandType.ChatInput
52
  },
53
  {
54
+ name: "Wykryj deepfake",
55
  type: ApplicationCommandType.Message
56
  }
57
  ]);
 
77
  return null;
78
  }
79
 
80
+ function preparePayload(input, explicitContentType = null) {
81
  const trimmed = input.trim();
82
+
83
+ if (explicitContentType) {
84
+ if (explicitContentType.startsWith("image/")) {
85
+ return { type: "image", payload: { image_url: trimmed, content_type: "image" } };
86
+ } else if (explicitContentType.startsWith("video/")) {
87
+ return { type: "video", payload: { video_url: trimmed, content_type: "video" } };
88
+ } else {
89
+ return { type: "file", payload: { file_url: trimmed, content_type: "file" } };
90
+ }
91
+ }
92
+
93
  const isUrl = trimmed.startsWith("http://") || trimmed.startsWith("https://");
94
 
95
  if (isUrl) {
96
+ const cleanUrl = trimmed.toLowerCase().split('?')[0];
97
 
98
+ if (cleanUrl.match(/\.(png|jpg|jpeg|webp|gif)$/)) {
99
+ return { type: "image", payload: { image_url: trimmed, content_type: "image" } };
100
+ } else if (cleanUrl.match(/\.(mp4|webm|mov|avi)$/)) {
101
+ return { type: "video", payload: { video_url: trimmed, content_type: "video" } };
 
 
 
 
 
 
 
 
 
 
 
 
102
  } else {
103
+ return { type: "file", payload: { file_url: trimmed, content_type: "file" } };
 
 
 
 
 
 
104
  }
105
  }
106
 
107
  return {
108
  type: "text",
109
+ payload: { text: trimmed, content_type: "text" }
 
 
 
110
  };
111
  }
112
 
 
207
  }
208
  }
209
 
210
+ async function handleAnalysis(interaction, userContent, targetMessage = null, explicitContentType = null) {
211
  await interaction.deferReply({ flags: [MessageFlags.Ephemeral] });
212
 
213
  const serverConfig = loadConfig(interaction.guildId);
214
 
215
  try {
216
+ const { type, payload } = preparePayload(userContent, explicitContentType);
217
 
218
  // DYNAMICZNE POBIERANIE MODELU Z PLIKU KONFIGURACYJNEGO DLA DANEGO FORMATU (np. text, image, video)
219
  const chosenModel = serverConfig.models[type];
 
388
  }
389
 
390
  if (interaction.isMessageContextMenuCommand()) {
391
+ if (interaction.commandName === "Wykryj deepfake") {
392
  const targetMessage = interaction.targetMessage;
393
 
394
  let contentToAnalyze = targetMessage.content;
395
+ let explicitContentType = null;
396
+
397
  const attachment = targetMessage.attachments.first();
398
 
399
  if (attachment) {
400
  contentToAnalyze = attachment.url;
401
+ explicitContentType = attachment.contentType;
402
  }
403
 
404
  if (!contentToAnalyze || contentToAnalyze.trim().length === 0) {
 
408
  });
409
  }
410
 
411
+ await handleAnalysis(interaction, contentToAnalyze, targetMessage, explicitContentType);
412
  }
413
  }
414