Skip to content

Commit

Permalink
Add condition when both reply and mention is used (#95)
Browse files Browse the repository at this point in the history
Co-authored-by: Ahmad Syafiq <[email protected]>
  • Loading branch information
ahmadsyafiqruhazat and Ahmad Syafiq authored Oct 31, 2021
1 parent a712d1e commit 63c5b70
Show file tree
Hide file tree
Showing 2 changed files with 343 additions and 2 deletions.
281 changes: 281 additions & 0 deletions src/bots/Reputation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,85 @@ describe('ReputationBot', () => {
},
});
});

it('when user replies with "thank you" and also mentions the same user', async () => {
await createUserInDb();
const mainMessage = createTgTextMessage('Is it your new PC?', {
chat: thisChat,
from: recipientUser,
reply_to_message: undefined,
});
const triggerMessage = createTgTextMessage('@omar_alfaruq thank you', {
chat: thisChat,
from: senderUser,
entities: [
{
type: 'mention',
offset: 0,
length: 13,
},
],
reply_to_message: mainMessage,
});

const messages = await runBot([ScriberBot, ReputationBot], ({ sendMessage }) => {
sendMessage(mainMessage);
sendMessage(triggerMessage);
});

assertBotSaid(messages, /increased reputation/);
await assert({
...triggerMessage,
reply_to_message: {
from: {
id: 2,
username: 'omar_alfaruq',
},
},
});
});

it('when user replies with "thank you" and also mentions the same user (text_mention)', async () => {
const mainMessage = createTgTextMessage('Is it your new PC?', {
chat: thisChat,
from: recipientUser,
reply_to_message: undefined,
});
const triggerMessage = createTgTextMessage('@omar_alfaruq thank you', {
chat: thisChat,
from: senderUser,
entities: [
{
type: 'text_mention',
offset: 0,
length: 13,
user: {
username: 'omar_alfaruq',
id: 2,
is_bot: false,
first_name: 'Omar Alfaruq',
},
},
],
reply_to_message: mainMessage,
});

const messages = await runBot([ScriberBot, ReputationBot], ({ sendMessage }) => {
sendMessage(mainMessage);
sendMessage(triggerMessage);
});

assertBotSaid(messages, /increased reputation/);
await assert({
...triggerMessage,
reply_to_message: {
from: {
id: 2,
username: 'omar_alfaruq',
},
},
});
});
});

describe('decreases reputation', () => {
Expand Down Expand Up @@ -661,6 +740,85 @@ describe('ReputationBot', () => {
},
});
});

it('when user replies with "boo" and also mentions the same user', async () => {
await createUserInDb();
const mainMessage = createTgTextMessage('Is it your new PC?', {
chat: thisChat,
from: recipientUser,
reply_to_message: undefined,
});
const triggerMessage = createTgTextMessage('@omar_alfaruq boo', {
chat: thisChat,
from: senderUser,
entities: [
{
type: 'mention',
offset: 0,
length: 13,
},
],
reply_to_message: mainMessage,
});

const messages = await runBot([ScriberBot, ReputationBot], ({ sendMessage }) => {
sendMessage(mainMessage);
sendMessage(triggerMessage);
});

assertBotSaid(messages, /decreased reputation/);
await assert({
...triggerMessage,
reply_to_message: {
from: {
id: 2,
username: 'omar_alfaruq',
},
},
});
});

it('when user replies with "boo" and also mentions the same user (text_mention)', async () => {
const mainMessage = createTgTextMessage('Is it your new PC?', {
chat: thisChat,
from: recipientUser,
reply_to_message: undefined,
});
const triggerMessage = createTgTextMessage('@omar_alfaruq boo', {
chat: thisChat,
from: senderUser,
entities: [
{
type: 'text_mention',
offset: 0,
length: 13,
user: {
username: 'omar_alfaruq',
id: 2,
is_bot: false,
first_name: 'Omar Alfaruq',
},
},
],
reply_to_message: mainMessage,
});

const messages = await runBot([ScriberBot, ReputationBot], ({ sendMessage }) => {
sendMessage(mainMessage);
sendMessage(triggerMessage);
});

assertBotSaid(messages, /decreased reputation/);
await assert({
...triggerMessage,
reply_to_message: {
from: {
id: 2,
username: 'omar_alfaruq',
},
},
});
});
});

describe('does not change reputation', () => {
Expand Down Expand Up @@ -913,6 +1071,129 @@ describe('ReputationBot', () => {
assertBotSaid(messages, 'Tag only one user at a time to increase rep!');
await assert(0);
});
it('when user replies with "thank you" and also mentions a different user', async () => {
const mainMessage = createTgTextMessage('Is it your new PC?', {
chat: thisChat,
from: recipientUser,
reply_to_message: undefined,
});
const triggerMessage = createTgTextMessage('@abu_bakr thank you', {
chat: thisChat,
from: senderUser,
entities: [
{
type: 'mention',
offset: 0,
length: 9,
},
],
reply_to_message: mainMessage,
});

const messages = await runBot([ScriberBot, ReputationBot], ({ sendMessage }) => {
sendMessage(mainMessage);
sendMessage(triggerMessage);
});

assertBotSaid(messages, 'Reply or Tag only one user at a time to increase rep!');
await assert(0);
});

it('when user replies with "thank you" and also mentions a different user (text_mention)', async () => {
const mainMessage = createTgTextMessage('Is it your new PC?', {
chat: thisChat,
from: recipientUser,
reply_to_message: undefined,
});
const triggerMessage = createTgTextMessage('@abu_bakr thank you', {
chat: thisChat,
from: senderUser,
entities: [
{
type: 'text_mention',
offset: 0,
length: 9,
user: {
username: 'abu_bakr',
id: 3,
is_bot: false,
first_name: 'Abu Bakr',
},
},
],
reply_to_message: mainMessage,
});

const messages = await runBot([ScriberBot, ReputationBot], ({ sendMessage }) => {
sendMessage(mainMessage);
sendMessage(triggerMessage);
});

assertBotSaid(messages, 'Reply or Tag only one user at a time to increase rep!');
await assert(0);
});

it('when user replies with "boo" and also mentions a different user', async () => {
const mainMessage = createTgTextMessage('Is it your new PC?', {
chat: thisChat,
from: recipientUser,
reply_to_message: undefined,
});
const triggerMessage = createTgTextMessage('@abu_bakr boo', {
chat: thisChat,
from: senderUser,
entities: [
{
type: 'mention',
offset: 0,
length: 9,
},
],
reply_to_message: mainMessage,
});

const messages = await runBot([ScriberBot, ReputationBot], ({ sendMessage }) => {
sendMessage(mainMessage);
sendMessage(triggerMessage);
});

assertBotSaid(messages, 'Reply or Tag only one user at a time to increase rep!');
await assert(0);
});

it('when user replies with "boo" and also mentions a different user (text_mention)', async () => {
const mainMessage = createTgTextMessage('Is it your new PC?', {
chat: thisChat,
from: recipientUser,
reply_to_message: undefined,
});
const triggerMessage = createTgTextMessage('@abu_bakr boo', {
chat: thisChat,
from: senderUser,
entities: [
{
type: 'text_mention',
offset: 0,
length: 9,
user: {
username: 'abu_bakr',
id: 3,
is_bot: false,
first_name: 'Abu Bakr',
},
},
],
reply_to_message: mainMessage,
});

const messages = await runBot([ScriberBot, ReputationBot], ({ sendMessage }) => {
sendMessage(mainMessage);
sendMessage(triggerMessage);
});

assertBotSaid(messages, 'Reply or Tag only one user at a time to increase rep!');
await assert(0);
});
});
});

Expand Down
64 changes: 62 additions & 2 deletions src/bots/Reputation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,37 @@ async function downvote(ctx: MsocietyBotContext, sender: TelegramUser, recipient
bot.hears(/thank you|thanks|πŸ‘|πŸ’―|πŸ‘†|πŸ†™|πŸ”₯/i, async ctx => {
const mentionEntities =
ctx.message?.entities?.filter(entity => ['mention', 'text_mention'].includes(entity.type)) || [];
if (ctx.message.reply_to_message !== undefined) {
if (ctx.message.reply_to_message !== undefined && mentionEntities.length) {
if (mentionEntities.length > 1) {
await ctx.reply('Tag only one user at a time to increase rep!', {
reply_to_message_id: ctx.message.message_id,
});
return;
}
// get the mention metadata
const entity = mentionEntities[0];
if (entity.type === 'mention') {
//get username from text using metadata
const username = ctx.message.text.substr(entity.offset + 1, entity.length - 1);

// check if mentioned user is same as user being replied to
if (username !== ctx.message.reply_to_message.from.username) {
await ctx.reply('Reply or Tag only one user at a time to increase rep!', {
reply_to_message_id: ctx.message.message_id,
});
return;
}
await upvote(ctx, ctx.message.from, ctx.message.reply_to_message.from);
} else if (entity.type === 'text_mention') {
if (entity.user.username !== ctx.message.reply_to_message.from.username) {
await ctx.reply('Reply or Tag only one user at a time to increase rep!', {
reply_to_message_id: ctx.message.message_id,
});
return;
}
await upvote(ctx, ctx.message.from, entity.user);
}
} else if (ctx.message.reply_to_message !== undefined) {
await upvote(ctx, ctx.from, ctx.message.reply_to_message.from);
} else if (
// check if there is a mention
Expand Down Expand Up @@ -124,7 +154,37 @@ bot.hears(/thank you|thanks|πŸ‘|πŸ’―|πŸ‘†|πŸ†™|πŸ”₯/i, async ctx => {
bot.hears(/πŸ‘Ž|πŸ‘‡|πŸ”½|\bboo(o*)\b|\beww(w*)\b/i, async ctx => {
const mentionEntities =
ctx.message?.entities?.filter(entity => ['mention', 'text_mention'].includes(entity.type)) || [];
if (ctx.message.reply_to_message !== undefined) {
if (ctx.message.reply_to_message !== undefined && mentionEntities.length) {
if (mentionEntities.length > 1) {
await ctx.reply('Tag only one user at a time to increase rep!', {
reply_to_message_id: ctx.message.message_id,
});
return;
}
// get the mention metadata
const entity = mentionEntities[0];
if (entity.type === 'mention') {
//get username from text using metadata
const username = ctx.message.text.substr(entity.offset + 1, entity.length - 1);

// check if mentioned user is same as user being replied to
if (username !== ctx.message.reply_to_message.from.username) {
await ctx.reply('Reply or Tag only one user at a time to increase rep!', {
reply_to_message_id: ctx.message.message_id,
});
return;
}
await downvote(ctx, ctx.message.from, ctx.message.reply_to_message.from);
} else if (entity.type === 'text_mention') {
if (entity.user.username !== ctx.message.reply_to_message.from.username) {
await ctx.reply('Reply or Tag only one user at a time to increase rep!', {
reply_to_message_id: ctx.message.message_id,
});
return;
}
await downvote(ctx, ctx.message.from, entity.user);
}
} else if (ctx.message.reply_to_message !== undefined) {
await downvote(ctx, ctx.from, ctx.message.reply_to_message.from);
} else if (
// check if there is a mention
Expand Down

0 comments on commit 63c5b70

Please sign in to comment.