Skip to content

Commit

Permalink
adding defensive checks - trying to avoid crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniocosentino committed Jan 20, 2024
1 parent 5911224 commit c688bd7
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 108 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The goal is to be able to secretly store all the participants' words while the g

![Flaviobot](avatar/flaviobot-logo.jpg)

The name Flaviobot is inspired by **Flavio Insinna**, the host of the TV show.
The name Flaviobot is inspired by **Flavio Insinna**, the former host of the TV show (I should probably call it Liornibot now, but it doesn't sound that cool).

## How does it work?

Expand Down
225 changes: 118 additions & 107 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,141 +88,152 @@ const updateScores = async (winners): Promise<boolean> => {
};

app.event('message', async ({ event, say, client }) => {
const triggerWord = wordCleaner(event.text);
if (event && event.text) {
const triggerWord = wordCleaner(event.text);

if (isGameRunning) {
participantsWords[event.user] = {
word: triggerWord,
sentAt: Math.floor(Date.now() / 1000),
};
saySomething(say, `Ok, ho memorizzato la tua parola: ${triggerWord}`);
if (isGameRunning) {
participantsWords[event.user] = {
word: triggerWord,
sentAt: Math.floor(Date.now() / 1000),
};
saySomething(say, `Ok, ho memorizzato la tua parola: ${triggerWord}`);

logger.log({
level: 'info',
message: `Received word from ${event.user}: ${triggerWord}`,
});
logger.log({
level: 'info',
message: `Received word from ${event.user}: ${triggerWord}`,
});

const friendly_name = getFriendlyNameFromId(event.user);
const friendly_name = getFriendlyNameFromId(event.user);

try {
await client.chat.postMessage({
channel: channelId,
text: `*${friendly_name}* ha scritto la sua parola`,
});
} catch (error) {
console.error(error);
try {
await client.chat.postMessage({
channel: channelId,
text: `*${friendly_name}* ha scritto la sua parola`,
});
} catch (error) {
console.error(error);
}
} else {
saySomething(say, 'Non puoi inviarmi la parola se il gioco non è in corso');
}
} else {
saySomething(say, 'Non puoi inviarmi la parola se il gioco non è in corso');
console.error('Invalid message event:', event);
}
});

app.event('app_mention', async ({ event, say }) => {
logger.log({
level: 'info',
message: `Bot was mentioned. Here is the full mention: "${event.text}"`,
});

const triggerWord = wordCleaner(removeMentionFromString(event.text));

switch (triggerWord) {
case 'vai!':
if (!isGameRunning) {
saySomething(say, 'Inizia il gioco. Attendo le vostre risposte in DM');
isGameRunning = true;
// resetting the words object
participantsWords = {};
channelId = event.channel;

logger.log({
level: 'info',
message: `Game was started - Session scores: ${JSON.stringify(sessionScores)}`,
});
} else {
saySomething(say, 'Il gioco è già stato avviato');
}
break;

case 'stop!':
if (isGameRunning) {
const allWords = constructResponse(participantsWords);
if (event && event.text) {
logger.log({
level: 'info',
message: `Bot was mentioned. Here is the full mention: "${event.text}"`,
});

saySomething(say, `Il gioco è chiuso. Ecco le parole: \n ${allWords}`);
isGameRunning = false;
isWaitingForWord = true;
const triggerWord = wordCleaner(removeMentionFromString(event.text));

logger.log({
level: 'info',
message: `Game was stopped - Words: ${JSON.stringify(participantsWords)}`,
});
} else {
saySomething(say, 'Nessun gioco in corso');
}
break;
switch (triggerWord) {
case 'vai!':
if (!isGameRunning) {
saySomething(say, 'Inizia il gioco. Attendo le vostre risposte in DM');
isGameRunning = true;
// resetting the words object
participantsWords = {};
channelId = event.channel;

case 'classifica!':
if (!SCORES_API) {
saySomething(say, 'Questa funzionalità non è supportata.');
} else {
const readableScores = constructScores(sessionScores);
saySomething(say, `Ecco la classifica: \n ${readableScores}`);
logger.log({
level: 'info',
message: `Game was started - Session scores: ${JSON.stringify(sessionScores)}`,
});
} else {
saySomething(say, 'Il gioco è già stato avviato');
}
break;

logger.log({
level: 'info',
message: `${event.user} asked for scores. Current scores: ${JSON.stringify(sessionScores)}`,
});
}
case 'stop!':
if (isGameRunning) {
const allWords = constructResponse(participantsWords);

break;
}
saySomething(say, `Il gioco è chiuso. Ecco le parole: \n ${allWords}`);
isGameRunning = false;
isWaitingForWord = true;

// The switch only covered the exact matches
// in the case where we communicate the correct word to the bot, we need to match a sentence pattern
logger.log({
level: 'info',
message: `Game was stopped - Words: ${JSON.stringify(participantsWords)}`,
});
} else {
saySomething(say, 'Nessun gioco in corso');
}
break;

if (triggerWord.startsWith('era ')) {
if (!isWaitingForWord) {
saySomething(say, 'Non puoi comunicarmi la parola vincente in questa fase del gioco.');
} else if (!SCORES_API) {
saySomething(say, 'Questa funzionalità non è supportata.');
} else {
isWaitingForWord = false;
case 'classifica!':
if (!SCORES_API) {
saySomething(say, 'Questa funzionalità non è supportata.');
} else {
const readableScores = constructScores(sessionScores);
saySomething(say, `Ecco la classifica: \n ${readableScores}`);

const finalWord = extractWordFromSentence(triggerWord);
logger.log({
level: 'info',
message: `${event.user} asked for scores. Current scores: ${JSON.stringify(sessionScores)}`,
});
}

const winners = getWinners(finalWord, participantsWords);
break;
}

logger.log({
level: 'info',
message: `Received final word: ${finalWord} - Winners: ${JSON.stringify(winners)} `,
});
// The switch only covered the exact matches
// in the case where we communicate the correct word to the bot, we need to match a sentence pattern

if (winners.length < 1) {
saySomething(say, `La parola era ${finalWord}. Non ci sono stati vincitori.`);
if (triggerWord.startsWith('era ')) {
if (!isWaitingForWord) {
saySomething(say, 'Non puoi comunicarmi la parola vincente in questa fase del gioco.');
} else if (!SCORES_API) {
saySomething(say, 'Questa funzionalità non è supportata.');
} else {
// in case there was only one player, who played by himself
// we will not assign the point
if (!shouldAssignThePoints(participantsWords)) {
saySomething(
say,
`La parola era ${finalWord}. Essendoci stato un solo giocatore il punto non verrà assegnato.`,
);

return;
}
isWaitingForWord = false;

const updateScoresAction = await updateScores(winners);
const finalWord = extractWordFromSentence(triggerWord);

if (updateScoresAction) {
logger.log({
level: 'info',
message: `Session scores after update: ${JSON.stringify(sessionScores)}`,
});
const winners = getWinners(finalWord, participantsWords);

logger.log({
level: 'info',
message: `Received final word: ${finalWord} - Winners: ${JSON.stringify(winners)} `,
});

const readableChart = constructScores(sessionScores);
saySomething(say, `La parola era ${finalWord}. Ecco la classifica aggiornata: \n${readableChart}`);
if (winners.length < 1) {
saySomething(say, `La parola era ${finalWord}. Non ci sono stati vincitori.`);
} else {
// in case there was only one player, who played by himself
// we will not assign the point
if (!shouldAssignThePoints(participantsWords)) {
saySomething(
say,
`La parola era ${finalWord}. Essendoci stato un solo giocatore il punto non verrà assegnato.`,
);

return;
}

const updateScoresAction = await updateScores(winners);

if (updateScoresAction) {
logger.log({
level: 'info',
message: `Session scores after update: ${JSON.stringify(sessionScores)}`,
});

const readableChart = constructScores(sessionScores);
saySomething(
say,
`La parola era ${finalWord}. Ecco la classifica aggiornata: \n${readableChart}`,
);
}
}
}
}
} else {
console.error('Invalid app_mention event:', event);
}
});

Expand Down

0 comments on commit c688bd7

Please sign in to comment.