Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ludopatico #63

Merged
merged 2 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"tabWidth": 2,
"arrowParens": "always",
"bracketSameLine": false,
"bracketSpacing": true,
"endOfLine": "crlf",
"htmlWhitespaceSensitivity": "ignore",
"semi": true,
"singleQuote": false,
"trailingComma": "none",
"useTabs": false,
"requirePragma": false
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ asked to "send a list of commands for your bot". Consider using this one:
> noncercogruppo - Smetti di cercare un gruppo di progetto
>
> registrate - Lezioni registrate su OneDrive

> ludopatico - Tenta la fortuna
>
> scelta - Elenchi esami a scelta
>
Expand Down Expand Up @@ -144,6 +146,10 @@ in the `template` attribute with the elements of a different array from the
The bot replies listing each command-description pair. If a command has no
description, it is not listed.

#### `luck`

Tests your luck.

#### `alias`

These commands are just aliases for others. The `command` attribute specifies
Expand Down
4 changes: 4 additions & 0 deletions json/actions.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@
"chatError": "Questo comando è riservato alle chat dei gruppi degli insegnamenti.",
"notFoundError": "Neanche prima cercavi gruppi in \"{0}\"."
},
"ludopatico": {
"type": "luck",
"description": "Tenta la fortuna"
},
"registrate": {
"type": "message",
"description": "Lezioni registrate su OneDrive",
Expand Down
6 changes: 3 additions & 3 deletions src/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ function onMessage(bot, msg) {
if (!msg.text) return; // no text
const text = msg.text.toString();
if (text[0] !== "/") {
Object.entries(data.autoreply).forEach(([regexp,value]) => {
const indexOfAt = text.search(new RegExp(regexp,"i")); //case insensitive search
if(indexOfAt != -1) message(bot, msg, value);
Object.entries(data.autoreply).forEach(([regexp, value]) => {
const indexOfAt = text.search(new RegExp(regexp, "i")); //case insensitive search
if (indexOfAt != -1) message(bot, msg, value);
});
return; // no command
}
Expand Down
25 changes: 24 additions & 1 deletion src/commands/basics.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { data } = require("../jsons"),
{ format } = require("../util"),
{ randomInt } = require("crypto"),
TelegramBot = require("node-telegram-bot-api");

/**
Expand All @@ -13,9 +14,31 @@ function message(bot, msg, text) {
.sendMessage(msg.chat.id, text, data.settings.messageOptions)
.catch((e) => console.error(e.stack));
}

module.exports.message = message;

/**
* Sends a dice message.
* @param {TelegramBot} bot The bot that should send the message.
* @param {TelegramBot.Message} msg The message that triggered this action.
* @param {'🎲'|'🎯'|'🏀'|'⚽'|'🎳'|'🎰'} [emoji] The emoji that should be sent, must be one of:
*/
function dice(bot, msg, emoji) {
if (!emoji) {
const emojis = ["🎲", "🎯", "🏀", "⚽", "🎳", "🎰"];
// Don't send last sent emoji
while (emoji === this.last) emoji = emojis[randomInt(emojis.length)];
}
this.last = emoji;
bot
.sendDice(msg.chat.id, {
allow_sending_without_reply: true,
disable_notification: true,
emoji
})
.catch((e) => console.error(e.stack));
}
module.exports.dice = dice;

/**
* Sends a help message.
* @param {TelegramBot} bot The bot that should send the message.
Expand Down
7 changes: 5 additions & 2 deletions src/router.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { data } = require("./jsons"),
{ tomorrowDate } = require("./util"),
{ message, giveHelp, list } = require("./commands/basics"),
{ message, giveHelp, list, dice } = require("./commands/basics"),
{ lookingFor, notLookingFor } = require("./commands/looking-for"),
{ timetable, course } = require("./commands/uni"),
{ considerUpdating } = require("./commands/update"),
Expand Down Expand Up @@ -34,6 +34,9 @@ function act(bot, msg, action) {
case "help":
giveHelp(bot, msg);
break;
case "luck":
dice(bot, msg);
break;
case "lookingFor":
lookingFor(
bot,
Expand Down Expand Up @@ -95,7 +98,7 @@ function act(bot, msg, action) {
default:
console.error(`Unknown action type "${action.type}"`);
}
};
}

/**
* Picks a different command based on the year of the current group.
Expand Down