genius-troll/detectors/withPronounDetector.js

105 lines
3.2 KiB
JavaScript
Raw Normal View History

2025-11-20 18:48:49 +01:00
import Detector from "./Detector.js";
import compromise from 'fr-compromise';
import conj from 'conjugation-fr';
import { toInfinitive } from "../data.js"; // check path ??
import { cleanMessageContent } from "../utils/strings.js";
2025-11-20 19:01:28 +01:00
/*check the pronoun / subject of the question to answer properly by conjugating
exemple : tu fais quoi ? je fais feur
*/
2025-11-20 18:48:49 +01:00
const suffixes = [
', mon gars',
', mec',
", je crois",
'',
'',
''
];
export default class withPronounDetector extends Detector {
async createSpecificReply(message) {
const reference = await message.fetchReference().catch(() => null);
const isSelfTarget = (reference && reference.author.id === message.client.user?.id) ?? false;
const compromiseMatch = compromise(cleanMessageContent(message).toLowerCase()).match('(#Pronoun|ça|ca|tu) (#Verb|fait) #Infinitive? quoi [<suffix>!Verb{0,3}]$');
if (compromiseMatch == null || compromiseMatch.length === 0) {
return null;
}
return this.conjugateResponse(compromiseMatch, isSelfTarget);
}
conjugateResponse(compromiseMatch, isSelfTarget = false) {
const parsed = compromiseMatch.json();
const result = parsed[0];
const replySuffix = compromiseMatch.groups('suffix')?.text().trim() ?? '';
if (result == null) {
return '';
}
// warning : result.terms[1] can't exist if the compromise structure change
const verbTerm = result.terms[1];
if (!verbTerm) return '';
const verbInfinitive = toInfinitive[verbTerm.text];
if (!verbInfinitive) return ''; // safety check if the verb is in the list
let conjugated = '';
const tenseTag = verbTerm.tags.find(t => t.endsWith('Tense')) ?? 'Present';
const getConjugation = (pronoun) => {
const tenseData = conj.findTense(verbInfinitive, tenseTag);
return tenseData.find(c => c.pronoun === pronoun)?.verb;
};
switch (result.terms[0].text.toLowerCase()) {
case 'je':
case "j'":
conjugated = `tu ${getConjugation('tu')}`;
break;
case 'tu':
case "t'":
if (isSelfTarget) {
conjugated = `je ${getConjugation('je')}`;
} else {
conjugated = `il ${getConjugation('il')}`;
}
break;
case 'il':
conjugated = `il ${getConjugation('il')}`;
break;
case 'elle':
conjugated = `elle ${getConjugation('il')}`; // 'elle' utilise la conjugaison 'il' dans cette lib souvent
break;
case 'on':
conjugated = `on ${getConjugation('il')}`;
break;
case 'ça':
conjugated = `ça ${getConjugation('il')}`;
break;
case 'nous':
conjugated = `nous ${getConjugation('nous')}`;
break;
case 'vous':
conjugated = `on ${getConjugation('il')}`;
break;
case 'ils':
conjugated = `ils ${getConjugation('ils')}`;
break;
case 'elles':
conjugated = `elles ${getConjugation('ils')}`;
break;
default:
conjugated = '';
break;
}
const infinitiveVerb = result.terms.find(t => t.tags.includes('Infinitive'))?.text ?? '';
return (`${conjugated} ${infinitiveVerb} feur ${replySuffix}${suffixes[Math.floor(Math.random() * suffixes.length)]}`).trim().replaceAll(' ', ' ').replace(' ,', ',');
}
}