Skip to content

Commit

Permalink
error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ilbertt committed Jun 25, 2022
1 parent b8345ff commit bc4393e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 42 deletions.
104 changes: 63 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const fileHandler = async (ctx, fileType) => {
ctx,
currentPath
);
await ctx.replyWithMarkdown(
return await ctx.replyWithMarkdown(
`${constants.currentPathMessage}\`${currentPath}\`\n${constants.saveFileMessage}`,
{
parse_mode: 'Markdown',
Expand All @@ -28,7 +28,6 @@ const fileHandler = async (ctx, fileType) => {
},
}
);
return;
};

const getGenericErrorWithCommands = async (ctx) => {
Expand Down Expand Up @@ -69,58 +68,70 @@ bot.start(async (ctx) => {
}
const botHelp = helpers.getHelpMessage();
msg = msg + `\n-----\nHere's some help to start:\n${botHelp}`;
ctx.replyWithMarkdown(msg);
await ctx.replyWithMarkdown(msg);
return await filesystem.initializeFileSystem(ctx);
});
bot.help((ctx) => {
return ctx.replyWithMarkdown(helpers.getHelpMessage());;

bot.help(async (ctx) => {
return await ctx.replyWithMarkdown(helpers.getHelpMessage());
});
bot.command(constants.COMMANDS.info, (ctx) => {
return ctx.replyWithMarkdown(`${constants.botInfo}\n\n_Version: ${helpers.getBotVersion()}_`);

bot.command(constants.COMMANDS.info, async (ctx) => {
return await ctx.replyWithMarkdown(`${constants.botInfo}\n\n_Version: ${helpers.getBotVersion()}_`);
});

/* FILE HANDLERS */
bot.on('animation', async (ctx) => {
return await fileHandler(ctx, 'animation');
});

bot.on('audio', async (ctx) => {
return await fileHandler(ctx, 'audio');
});

bot.on('contact', async (ctx) => {
return await fileHandler(ctx, 'contact');
});

bot.on('document', async (ctx) => {
if (ctx.session.action === constants.RESTORE_FILESYSTEM_ACTION) {
try {
await filesystem.restoreFilesystem(ctx);
return ctx.reply(constants.fileSystemRestoredSuccess, {
return await ctx.reply(constants.fileSystemRestoredSuccess, {
reply_to_message_id: ctx.message.message_id,
});
} catch (e) {
console.log(e);
return ctx.reply(constants.fileSystemRestoredError);
return await ctx.reply(constants.fileSystemRestoredError);
}
}
return await fileHandler(ctx, 'document');
});

bot.on('location', async (ctx) => {
return await fileHandler(ctx, 'location');
});

bot.on('photo', async (ctx) => {
return await fileHandler(ctx, 'photo');
});

bot.on('poll', async (ctx) => {
return await fileHandler(ctx, 'poll');
});

bot.on('sticker', async (ctx) => {
return await fileHandler(ctx, 'sticker');
});

bot.on('video', async (ctx) => {
return await fileHandler(ctx, 'video');
});

bot.on('video_note', async (ctx) => {
return await fileHandler(ctx, 'video_note');
});

bot.on('voice', async (ctx) => {
return await fileHandler(ctx, 'voice');
});
Expand All @@ -133,7 +144,7 @@ bot.command(constants.COMMANDS.mkdir, async (ctx) => {
ctx,
currentPath
);
return ctx.reply(
return await ctx.reply(
`${constants.currentPathMessage}\`${currentPath}\`\n${constants.createDirMessage}`,
{
parse_mode: 'Markdown',
Expand All @@ -143,6 +154,7 @@ bot.command(constants.COMMANDS.mkdir, async (ctx) => {
}
);
});

bot.command(constants.COMMANDS.explorer, async (ctx) => {
const currentPath = helpers.getCurrentPath(ctx);
ctx.session.action = constants.EXPLORER_ACTION;
Expand All @@ -153,13 +165,12 @@ bot.command(constants.COMMANDS.explorer, async (ctx) => {
true
);
// inlineKeyboardButtons.push(mkdirInlineButton);
ctx.replyWithMarkdown(`${constants.currentPathMessage}\`${currentPath}\``, {
return await ctx.replyWithMarkdown(`${constants.currentPathMessage}\`${currentPath}\``, {
parse_mode: 'Markdown',
reply_markup: {
...Markup.inlineKeyboard(inlineKeyboardButtons).reply_markup,
},
});
return;
});

bot.command(constants.COMMANDS.rename_file, async (ctx) => {
Expand All @@ -172,13 +183,12 @@ bot.command(constants.COMMANDS.rename_file, async (ctx) => {
true
);
// inlineKeyboardButtons.push(mkdirInlineButton);
ctx.replyWithMarkdown(`${constants.currentPathMessage}\`${currentPath}\`\n${constants.renameFileMessage}`, {
return await ctx.replyWithMarkdown(`${constants.currentPathMessage}\`${currentPath}\`\n${constants.renameFileMessage}`, {
parse_mode: 'Markdown',
reply_markup: {
...Markup.inlineKeyboard(inlineKeyboardButtons).reply_markup,
},
});
return;
});

bot.command(constants.COMMANDS.move_file, async (ctx) => {
Expand All @@ -191,13 +201,12 @@ bot.command(constants.COMMANDS.move_file, async (ctx) => {
true
);
// inlineKeyboardButtons.push(mkdirInlineButton);
ctx.replyWithMarkdown(`${constants.currentPathMessage}\`${currentPath}\`\n${constants.selectMoveFileMessage}`, {
return await ctx.replyWithMarkdown(`${constants.currentPathMessage}\`${currentPath}\`\n${constants.selectMoveFileMessage}`, {
parse_mode: 'Markdown',
reply_markup: {
...Markup.inlineKeyboard(inlineKeyboardButtons).reply_markup,
},
});
return;
});

bot.command(constants.COMMANDS.delete_dir, async (ctx) => {
Expand All @@ -210,7 +219,7 @@ bot.command(constants.COMMANDS.delete_dir, async (ctx) => {
true,
true
);
ctx.replyWithMarkdown(
return await ctx.replyWithMarkdown(
`${constants.currentPathMessage}\`${currentPath}\`\n${constants.deleteDirMessage}`,
{
parse_mode: 'Markdown',
Expand All @@ -219,8 +228,8 @@ bot.command(constants.COMMANDS.delete_dir, async (ctx) => {
},
}
);
return;
});

bot.command(constants.COMMANDS.delete_file, async (ctx) => {
const currentPath = helpers.getCurrentPath(ctx);
ctx.session.action = constants.DELETE_FILE_ACTION;
Expand All @@ -230,7 +239,7 @@ bot.command(constants.COMMANDS.delete_file, async (ctx) => {
true,
true
);
ctx.replyWithMarkdown(
return await ctx.replyWithMarkdown(
`${constants.currentPathMessage}\`${currentPath}\`\n${constants.deleteFileMessage}`,
{
parse_mode: 'Markdown',
Expand All @@ -239,24 +248,26 @@ bot.command(constants.COMMANDS.delete_file, async (ctx) => {
},
}
);
return;
});

bot.command(constants.COMMANDS.filesystem, async (ctx) => {
const chat = await ctx.getChat();
const fileSystemMessage = chat.pinned_message;
if (fileSystemMessage) {
return ctx.replyWithMarkdown(constants.filesystemInfoMessage(chat.id, fileSystemMessage.message_id),
return await ctx.replyWithMarkdown(constants.filesystemInfoMessage(chat.id, fileSystemMessage.message_id),
{
reply_to_message_id: fileSystemMessage.message_id,
}
);
}
return ctx.reply(constants.fileSystemNotFound);
return await ctx.reply(constants.fileSystemNotFound);
});

bot.command(constants.COMMANDS.restore_filesystem, async (ctx) => {
ctx.session.action = constants.RESTORE_FILESYSTEM_ACTION;
return ctx.reply(constants.restoreFilesystemMessage);
return await ctx.reply(constants.restoreFilesystemMessage);
});

bot.action(constants.thisDirAction, async (ctx) => {
const currentPath = helpers.getCurrentPath(ctx);
const action = ctx.session.action;
Expand All @@ -275,8 +286,9 @@ bot.action(constants.thisDirAction, async (ctx) => {
ctx.session.fileToHandle = null;
ctx.session.oldPath = null;
ctx.session.currentPath = null;
return ctx.replyWithMarkdown(constants.movedFileSuccess(fileName, sourcePath, currentPath));
return await ctx.replyWithMarkdown(constants.movedFileSuccess(fileName, sourcePath, currentPath));
}

if (message) {
return ctx.editMessageText(
`${constants.currentPathMessage}\`${currentPath}\`\n${message}`,
Expand All @@ -290,9 +302,10 @@ bot.action(constants.thisDirAction, async (ctx) => {
}
);
} else {
return ctx.reply(await getGenericErrorWithCommands(ctx));
return await ctx.reply(await getGenericErrorWithCommands(ctx));
}
});

bot.action(constants.parentDirAction, async (ctx) => {
const currentPath = helpers.getCurrentPath(ctx);
const action = ctx.session.action;
Expand All @@ -318,9 +331,10 @@ bot.action(constants.parentDirAction, async (ctx) => {
}
);
} else {
return ctx.reply(await getGenericErrorWithCommands(ctx));
return await ctx.reply(await getGenericErrorWithCommands(ctx));
}
});

bot.action(constants.backAction, async (ctx) => {
const action = ctx.session.action;

Expand All @@ -344,9 +358,10 @@ bot.action(constants.backAction, async (ctx) => {
}
);
} else {
return ctx.reply(await getGenericErrorWithCommands(ctx));
return await ctx.reply(await getGenericErrorWithCommands(ctx));
}
});

bot.action(constants.deleteDirAction, async (ctx) => {
const currentPath = helpers.getCurrentPath(ctx); // format /parentDir/.../childDir/currentDir/
const directoryName = currentPath.split('/').slice(0, -1).pop();
Expand All @@ -356,8 +371,9 @@ bot.action(constants.deleteDirAction, async (ctx) => {
await filesystem.deleteDirectory(ctx, targetPath, directoryName);
ctx.session.action = null;
ctx.session.currentPath = null;
return ctx.replyWithMarkdown(constants.deletedDirectorySuccess(directoryName, targetPath));
return await ctx.replyWithMarkdown(constants.deletedDirectorySuccess(directoryName, targetPath));
});

bot.action(/^(.?$|[^\/].+)/, async (ctx) => {
// DIRECTORY action
const action = ctx.session.action;
Expand All @@ -369,13 +385,13 @@ bot.action(/^(.?$|[^\/].+)/, async (ctx) => {
ctx,
newCurrentPath,
action === constants.EXPLORER_ACTION ||
action === constants.DELETE_DIR_ACTION ||
action === constants.RENAME_FILE_ACTION ||
action === constants.SELECT_MOVE_FILE_ACTION,
action === constants.DELETE_DIR_ACTION ||
action === constants.RENAME_FILE_ACTION ||
action === constants.SELECT_MOVE_FILE_ACTION,
action === constants.EXPLORER_ACTION ||
action === constants.DELETE_FILE_ACTION ||
action === constants.RENAME_FILE_ACTION ||
action === constants.SELECT_MOVE_FILE_ACTION,
action === constants.DELETE_FILE_ACTION ||
action === constants.RENAME_FILE_ACTION ||
action === constants.SELECT_MOVE_FILE_ACTION,
action === constants.DELETE_DIR_ACTION
);

Expand All @@ -391,6 +407,7 @@ bot.action(/^(.?$|[^\/].+)/, async (ctx) => {
}
);
});

bot.action(/^\//, async (ctx) => {
// FILE action
const action = ctx.session.action;
Expand All @@ -404,18 +421,18 @@ bot.action(/^\//, async (ctx) => {
ctx.session.currentPath = null;
const message = constants.deletedFileSuccess(fileName, currentPath);
if (currentPath.startsWith('/Trash')) {
return ctx.replyWithMarkdown(
return await ctx.replyWithMarkdown(
`${message}\n\n${constants.deletedFileFromTrash}`,
{
reply_to_message_id: deletedFile.messageId,
}
);
}
return ctx.replyWithMarkdown(message);
return await ctx.replyWithMarkdown(message);
} else if (action === constants.RENAME_FILE_ACTION) {
ctx.session.waitReply = constants.WAIT_FILE_NAME;
ctx.session.fileToHandle = fileName;
return ctx.replyWithMarkdown(constants.askRenameFileName(fileName, currentPath));
return await ctx.replyWithMarkdown(constants.askRenameFileName(fileName, currentPath));
} else if (action === constants.SELECT_MOVE_FILE_ACTION) {
ctx.session.fileToHandle = fileName;
ctx.session.oldPath = currentPath;
Expand Down Expand Up @@ -450,13 +467,14 @@ bot.action(/^\//, async (ctx) => {
}
}
});

bot.on('text', async (ctx) => {
const currentPath = helpers.getCurrentPath(ctx);
const waitReply = ctx.session.waitReply;
const reply = ctx.message.text;
const action = ctx.session.action;

if(waitReply) {
if (waitReply) {
let message = 'Error';
if (reply.includes(constants.fileActionPrefix)) {
message = constants.forbiddenCharacterAlert;
Expand Down Expand Up @@ -493,12 +511,16 @@ bot.on('text', async (ctx) => {

bot.on('pinned_message', async (ctx) => {
if (ctx.message.from.id !== ctx.botInfo.id) {
return ctx.replyWithMarkdown(constants.pinnedMessageAlert(ctx.chat.id));
return await ctx.replyWithMarkdown(constants.pinnedMessageAlert(ctx.chat.id));
}
});

bot.catch((err, ctx) => {
return ctx.reply(`Ooops, encountered an error for ${ctx.updateType}`, err);
bot.catch(async (err, ctx) => {
try {
return await ctx.reply(`Ooops, encountered an error for ${ctx.updateType}`, err);
} catch (err) {
console.error('ERROR: ', err);
}
});

// Enable graceful stop
Expand Down
4 changes: 3 additions & 1 deletion src/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ const unpinOldFilesystem = async (ctx) => {
if (rootMessage) {
try {
await ctx.unpinChatMessage(rootMessage.message_id);
} catch (err) {}
} catch (err) {
console.error('ERROR: Cannot unpin message', err);
}
}
};

Expand Down

0 comments on commit bc4393e

Please sign in to comment.