Skip to content

Commit edc315e

Browse files
committed
Merge branch 'development'
2 parents f12a196 + d9a0278 commit edc315e

File tree

1 file changed

+86
-59
lines changed

1 file changed

+86
-59
lines changed

src/modules/Eggcart.js

+86-59
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,36 @@ class EggCart {
1717
constructor() {
1818
this.listController = new EggoListController();
1919
this.bot = new Telegraf(config.telegram.token);
20+
21+
this.bot.telegram.getMe().then((botInfo) => {
22+
this.botName = botInfo.username;
23+
});
2024
}
2125

2226
/**
2327
* Add an item to the shopping list via the bot command.
2428
*/
2529
addItem() {
2630
this.bot.command('add', async (ctx) => {
27-
let messageText = ctx.update.message.text;
28-
let itemsToAdd = messageText.slice(messageText.indexOf(" ") + 1).split(",");
29-
let response = 'Okay! \n';
31+
const messageText = ctx.update.message.text;
32+
const chatType = ctx.update.message.chat.type;
3033

31-
for (let itemText of itemsToAdd) {
32-
try {
33-
await this.listController.addItem(itemText.trim());
34-
response += `${itemText.trim()}, `;
35-
} catch (error) {
36-
console.error(error);
34+
if (messageText.includes(`@${this.botName}`) || chatType === 'private' || chatType === 'group') {
35+
let itemsToAdd = messageText.slice(messageText.indexOf(" ") + 1).split(",");
36+
let response = 'Okay! \n';
37+
38+
39+
for (let itemText of itemsToAdd) {
40+
try {
41+
await this.listController.addItem(itemText.trim());
42+
response += `${itemText.trim()}, `;
43+
} catch (error) {
44+
console.error(error);
45+
}
3746
}
47+
response = response.slice(0, -2) + ' are on the shopping list!';
48+
ctx.reply(response);
3849
}
39-
response = response.slice(0, -2) + ' are on the shopping list!';
40-
ctx.reply(response);
4150
});
4251
}
4352

@@ -46,31 +55,35 @@ class EggCart {
4655
*/
4756
deleteItem() {
4857
this.bot.command('remove', async (ctx) => {
49-
let messageText = ctx.update.message.text;
50-
let itemsToRemove = messageText.slice(messageText.indexOf(" ") + 1).split(",");
51-
let response = '';
58+
const messageText = ctx.update.message.text;
59+
const chatType = ctx.update.message.chat.type;
5260

53-
for (let itemName of itemsToRemove) {
54-
let escapedItemName = escapeMarkdownV2Characters(itemName.trim());
55-
try {
56-
const item = await this.listController.findItemByName(itemName.trim());
57-
if (item) {
58-
await this.listController.removeItem(item.id);
59-
response += `Okay\\!\n*${escapedItemName}* removed from the shopping list\\.\n`;
60-
} else {
61-
response += `Oh\\!\n*${escapedItemName}* not found in the shopping list\\.\n`;
61+
if (messageText.includes(`@${this.botName}`) || chatType === 'private' || chatType === 'group') {
62+
let itemsToRemove = messageText.slice(messageText.indexOf(" ") + 1).split(",");
63+
let response = '';
64+
65+
for (let itemName of itemsToRemove) {
66+
let escapedItemName = escapeMarkdownV2Characters(itemName.trim());
67+
try {
68+
const item = await this.listController.findItemByName(itemName.trim());
69+
if (item) {
70+
await this.listController.removeItem(item.id);
71+
response += `Okay\\!\n*${escapedItemName}* removed from the shopping list\\.\n`;
72+
} else {
73+
response += `Oh\\!\n*${escapedItemName}* not found in the shopping list\\.\n`;
74+
}
75+
} catch (error) {
76+
console.error(error);
77+
response += `Oh\\!\nError removing *${escapedItemName}*\\.\n`;
6278
}
63-
} catch (error) {
64-
console.error(error);
65-
response += `Oh\\!\nError removing *${escapedItemName}*\\.\n`;
6679
}
67-
}
68-
69-
if (response === '') {
70-
response = "No items specified for removal\\.";
71-
}
72-
73-
ctx.replyWithMarkdownV2(response);
80+
81+
if (response === '') {
82+
response = "No items specified for removal\\.";
83+
}
84+
85+
ctx.replyWithMarkdownV2(response);
86+
}
7487
});
7588
}
7689

@@ -79,19 +92,24 @@ class EggCart {
7992
*/
8093
getList() {
8194
this.bot.command('list', async (ctx) => {
82-
try {
83-
let items = await this.listController.getItems();
84-
let response = 'Grocery List\n';
85-
items.forEach((item, index) => {
86-
response += `${index + 1}. ${item.item}\n`;
87-
});
88-
if (items.length === 0) {
89-
response = "Nothing to shop for :o - try adding eggs";
95+
const messageText = ctx.update.message.text;
96+
const chatType = ctx.update.message.chat.type;
97+
98+
if (messageText.includes(`@${this.botName}`) || chatType === 'private' || chatType === 'group') {
99+
try {
100+
let items = await this.listController.getItems();
101+
let response = 'Grocery List\n';
102+
items.forEach((item, index) => {
103+
response += `${index + 1}. ${item.item}\n`;
104+
});
105+
if (items.length === 0) {
106+
response = "Nothing to shop for :o - try adding eggs";
107+
}
108+
ctx.reply(response);
109+
} catch (error) {
110+
console.error(error);
111+
ctx.reply("An error occurred while getting the list.");
90112
}
91-
ctx.reply(response);
92-
} catch (error) {
93-
console.error(error);
94-
ctx.reply("An error occurred while getting the list.");
95113
}
96114
});
97115
}
@@ -101,15 +119,20 @@ class EggCart {
101119
*/
102120
clearList() {
103121
this.bot.command('clear', async (ctx) => {
104-
try {
105-
let items = await this.listController.getItems();
106-
for (const item of items) {
107-
await this.listController.removeItem(item.id);
122+
const messageText = ctx.update.message.text;
123+
const chatType = ctx.update.message.chat.type;
124+
125+
if (messageText.includes(`@${this.botName}`) || chatType === 'private' || chatType === 'group') {
126+
try {
127+
let items = await this.listController.getItems();
128+
for (const item of items) {
129+
await this.listController.removeItem(item.id);
130+
}
131+
ctx.reply("The shopping list has been cleared!");
132+
} catch (error) {
133+
console.error(error);
134+
ctx.reply("An error occurred while clearing the list.");
108135
}
109-
ctx.reply("The shopping list has been cleared!");
110-
} catch (error) {
111-
console.error(error);
112-
ctx.reply("An error occurred while clearing the list.");
113136
}
114137
});
115138
}
@@ -119,13 +142,17 @@ class EggCart {
119142
*/
120143
help() {
121144
this.bot.help((ctx) => {
122-
ctx.reply(
123-
"Add an item: /add eggs, milk\n" +
124-
"Remove an item: /remove eggs, milk\n" +
125-
"Show the list: /list\n" +
126-
"Clear the list: /clear"
145+
const messageText = ctx.update.message.text;
146+
const chatType = ctx.update.message.chat.type;
127147

128-
);
148+
if (messageText.includes(`@${this.botName}`) || chatType === 'private' || chatType === 'group') {
149+
ctx.reply(
150+
"Add an item: /add eggs, milk\n" +
151+
"Remove an item: /remove eggs, milk\n" +
152+
"Show the list: /list\n" +
153+
"Clear the list: /clear"
154+
);
155+
}
129156
});
130157
}
131158

0 commit comments

Comments
 (0)