Skip to content

Commit

Permalink
Merge pull request #38 from grunch/issue_23-ban-user-command
Browse files Browse the repository at this point in the history
Adds ban user command for admin
  • Loading branch information
grunch committed Aug 24, 2021
2 parents c335a0a + 94c0d0b commit 200af2a
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 180 deletions.
72 changes: 27 additions & 45 deletions bot/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ const beginTakeBuyMessage = async (bot, sellerUser, request, order) => {

const onGoingTakeBuyMessage = async (bot, sellerUser, buyerUser, order) => {
try {
await bot.telegram.sendMessage(sellerUser.tg_id, `Pago recibido!\n\nPonte en contacto con el usuario @${buyerUser.username} para darle los detalles de metodo de pago fiat que te hara. Una vez confirmes su pago debes liberar los fondos con el comando:`);
await bot.telegram.sendMessage(sellerUser.tg_id, `¡Pago recibido!\n\nPonte en contacto con el usuario @${buyerUser.username} para darle los detalles de metodo de pago fiat que te hara. Una vez confirmes su pago debes liberar los fondos con el comando:`);
await bot.telegram.sendMessage(sellerUser.tg_id, `/release ${order._id}`);
await bot.telegram.sendMessage(buyerUser.tg_id, `El usuario @${sellerUser.username} ha tomado tu compra y te quiere vender sats. Comunicate con el para que le hagas el pago por fiat y te libere sus sats. `);
} catch (error) {
Expand Down Expand Up @@ -263,14 +263,6 @@ const doneTakeSellMessage = async (bot, orderUser, buyerUser) => {
}
};

const releaseCorrectFormatMessage = async (bot, user) => {
try {
await bot.telegram.sendMessage(user.tg_id, `/release <order_id>`);
} catch (error) {
console.log(error);
}
};

const notActiveOrderMessage = async (bot, user) => {
try {
await bot.telegram.sendMessage(user.tg_id, `No tienes esa orden activa`);
Expand Down Expand Up @@ -337,14 +329,6 @@ const publishSellOrderMessage = async (ctx, bot, order) => {
}
};

const disputeCorrectFormatMessage = async (bot, user) => {
try {
await bot.telegram.sendMessage(user.tg_id, `/dispute <order_id>`);
} catch (error) {
console.log(error);
}
};

const beginDisputeMessage = async (bot, buyer, seller, order, initiator) => {
try {

Expand Down Expand Up @@ -378,22 +362,6 @@ buyer payment request: ${order.buyer_invoice}
}
};

const cancelCorrectFormatMessage = async (bot, user) => {
try {
await bot.telegram.sendMessage(user.tg_id, `/cancel <order_id>`);
} catch (error) {
console.log(error);
}
};

const cooperativeCancelCorrectFormatMessage = async (bot, user) => {
try {
await bot.telegram.sendMessage(user.tg_id, `/cooperativecancel <order_id>`);
} catch (error) {
console.log(error);
}
};

const customMessage = async (bot, user, message) => {
try {
await bot.telegram.sendMessage(user.tg_id, message);
Expand Down Expand Up @@ -475,27 +443,43 @@ const bannedUserErrorMessage = async (ctx) => {
}
};

const fiatSentCorrectFormatMessage = async (bot, user) => {
const fiatSentMessages = async (bot, buyer, seller) => {
try {
await bot.telegram.sendMessage(user.tg_id, `/fiatsent <order_id>`);
await bot.telegram.sendMessage(buyer.tg_id, `Le hemos avisado al vendedor que has enviado el dinero fiat, en lo que el vendedor confirme que recibió tu dinero deberá liberar los fondos`);
await bot.telegram.sendMessage(seller.tg_id, `El comprador me ha indicado que ya te envió el dinero fiat, esperamos que una vez confirmes la recepción del dinero liberes los fondos con el comando release, debes saber que hasta que no liberes los fondos no podrás crear o tomar otra orden`);
} catch (error) {
console.log(error);
}
};

const fiatSentMessages = async (bot, buyer, seller) => {

const orderOnfiatSentStatusMessages = async (bot, user) => {
try {
await bot.telegram.sendMessage(buyer.tg_id, `Le hemos avisado al vendedor que has enviado el dinero fiat, en lo que el vendedor confirme que recibió tu dinero deberá liberar los fondos`);
await bot.telegram.sendMessage(seller.tg_id, `El comprador me ha indicado que ya te envió el dinero fiat, esperamos que una vez confirmes la recepción del dinero liberes los fondos con el comando release, debes saber que hasta que no liberes los fondos no podrás crear o tomar otra orden`);
await bot.telegram.sendMessage(user.tg_id, `Tienes una o más ordenes en las que el comprador indicó que te envió el dinero fiat pero no has liberado los fondos, no puedes seguir operando hasta completar esa(s) orden(es)`);
} catch (error) {
console.log(error);
}
};

const userBannedMessage = async (bot, user) => {
try {
await bot.telegram.sendMessage(user.tg_id, `¡Usuario baneado!`);
} catch (error) {
console.log(error);
}
};

const orderOnfiatSentStatusMessages = async (bot, user) => {
const notFoundUserMessage = async (bot, user) => {
try {
await bot.telegram.sendMessage(user.tg_id, `Tienes una o más ordenes en las que el comprador indicó que te envió el dinero fiat pero no has liberado los fondos, no puedes seguir operando hasta completar esa(s) orden(es)`);
await bot.telegram.sendMessage(user.tg_id, `¡Usuario no encontrado en base de datos!`);
} catch (error) {
console.log(error);
}
};

const errorParsingInvoiceMessage = async (bot, user) => {
try {
await bot.telegram.sendMessage(user.tg_id, `Error parseando la invoice`);
} catch (error) {
console.log(error);
}
Expand All @@ -522,7 +506,6 @@ module.exports = {
takeBuyCorrectFormatMessage,
takeSellCorrectFormatMessage,
beginTakeBuyMessage,
releaseCorrectFormatMessage,
notActiveOrderMessage,
publishSellOrderMessage,
onGoingTakeBuyMessage,
Expand All @@ -532,9 +515,7 @@ module.exports = {
pendingBuyMessage,
repeatedInvoiceMessage,
mustBeIntMessage,
disputeCorrectFormatMessage,
beginDisputeMessage,
cancelCorrectFormatMessage,
notOrderMessage,
customMessage,
nonHandleErrorMessage,
Expand All @@ -545,9 +526,10 @@ module.exports = {
helpMessage,
mustBeGreatherEqThan,
bannedUserErrorMessage,
fiatSentCorrectFormatMessage,
fiatSentMessages,
orderOnfiatSentStatusMessages,
cooperativeCancelCorrectFormatMessage,
takeSellWaitingSellerToPayMessage,
userBannedMessage,
notFoundUserMessage,
errorParsingInvoiceMessage,
};
65 changes: 40 additions & 25 deletions bot/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,12 @@ const {
validateTakeSell,
validateTakeBuyOrder,
validateReleaseOrder,
validateTakeBuy,
validateTakeSellOrder,
validateRelease,
validateDispute,
validateDisputeOrder,
validateCancel,
validateCancelAdmin,
validateAdmin,
validateSettleAdmin,
validateFiatSent,
validateFiatSentOrder,
validateSeller,
validateCooperativeCancel,
validateParams,
} = require('./validations');
const messages = require('./messages');

Expand Down Expand Up @@ -113,12 +106,12 @@ const start = () => {
try {
const user = await validateUser(ctx, false);
if (!user) return;

const takeSellParams = await validateTakeSell(ctx, bot, user);
if (!takeSellParams) return;

const { orderId, lnInvoice } = takeSellParams;

if (!orderId) return;

try {
const order = await Order.findOne({ _id: orderId });
if (!(await validateTakeSellOrder(bot, user, lnInvoice, order))) return;
Expand Down Expand Up @@ -163,7 +156,7 @@ const start = () => {

if (!isOnFiatSentStatus) return;

const orderId = await validateTakeBuy(ctx, bot, user);
const [orderId] = await validateParams(ctx, bot, user, 2, '<order_id>');

if (!orderId) return;

Expand Down Expand Up @@ -197,7 +190,7 @@ const start = () => {
const user = await validateUser(ctx, false);
if (!user) return;

const orderId = await validateRelease(ctx, bot, user);
const [orderId] = await validateParams(ctx, bot, user, 2, '<order_id>');

if (!orderId) return;

Expand All @@ -217,7 +210,7 @@ const start = () => {

if (!user) return;

const orderId = await validateDispute(ctx, bot, user);
const [orderId] = await validateParams(ctx, bot, user, 2, '<order_id>');

if (!orderId) return;

Expand Down Expand Up @@ -260,7 +253,7 @@ const start = () => {
const user = await validateUser(ctx, false);
if (!user) return;

const orderId = await validateCancel(ctx, bot, user);
const [orderId] = await validateParams(ctx, bot, user, 2, '<order_id>');

if (!orderId) return;

Expand Down Expand Up @@ -295,10 +288,10 @@ const start = () => {

bot.command('cancelorder', async (ctx) => {
try {
const user = await validateAdmin(ctx);
const user = await validateAdmin(ctx, bot);
if (!user) return;

const orderId = await validateCancelAdmin(ctx, bot, user);
const [orderId] = await validateParams(ctx, bot, user, 2, '<order_id>');

if (!orderId) return;

Expand Down Expand Up @@ -334,10 +327,10 @@ const start = () => {

bot.command('settleorder', async (ctx) => {
try {
const user = await validateAdmin(ctx);
const user = await validateAdmin(ctx, bot);
if (!user) return;

const orderId = await validateSettleAdmin(ctx, bot, user);
const [orderId] = await validateParams(ctx, bot, user, 2, '<order_id>');

if (!orderId) return;

Expand Down Expand Up @@ -372,10 +365,10 @@ const start = () => {

bot.command('checkorder', async (ctx) => {
try {
const user = await validateAdmin(ctx);
const user = await validateAdmin(ctx, bot);
if (!user) return;

const orderId = await validateCancelAdmin(ctx, bot, user);
const [orderId] = await validateParams(ctx, bot, user, 2, '<order_id>');

if (!orderId) return;

Expand All @@ -387,14 +380,13 @@ const start = () => {
const buyer = await User.findOne({ _id: order.buyer_id });
const seller = await User.findOne({ _id: order.seller_id });

await messages.checkOrderMessage(ctx,order,creator.username,buyer.username,seller.username);
await messages.checkOrderMessage(ctx, order, creator.username, buyer.username, seller.username);

} catch (error) {
console.log(error);
}
});


bot.command('help', async (ctx) => {
try {
const user = await validateUser(ctx, false);
Expand All @@ -412,8 +404,7 @@ const start = () => {
const user = await validateUser(ctx, false);

if (!user) return;

const orderId = await validateFiatSent(ctx, bot, user);
const [orderId] = await validateParams(ctx, bot, user, 2, '<order_id>');

if (!orderId) return;

Expand All @@ -438,7 +429,7 @@ const start = () => {

if (!user) return;

const orderId = await validateCooperativeCancel(ctx, bot, user);
const [orderId] = await validateParams(ctx, bot, user, 2, '<order_id>');

if (!orderId) return;

Expand Down Expand Up @@ -499,6 +490,30 @@ const start = () => {
}
});

bot.command('ban', async (ctx) => {
try {
const adminUser = await validateAdmin(ctx, bot);

if (!adminUser) return;

const params = await validateParams(ctx, bot, adminUser, 2, '<username>');

if (!params) return;

const user = await User.findOne({ username: params[0] });

if (!user) {
await messages.notFoundUserMessage(bot, adminUser);
return;
}
user.banned = true;
await user.save();
await messages.userBannedMessage(bot, adminUser);
} catch (error) {
console.log(error);
}
});

bot.launch();

// Enable graceful stop
Expand Down
Loading

0 comments on commit 200af2a

Please sign in to comment.