Auto Bulk Remove Habitica Emails

== Description ==
This script will remove all Habitica emails from your Gmail box received within a specified timeframe (default below is 14 days). For this to work, ensure the Gmail account you are running the script from is the email linked to your Habitica Account.
At the daily time, the script will review all your emails, and remove them as specified from Habitica.
== Installation ==
* Go to script.google.com. If this is your first script, this will automatically create a new Google script for you and open an editor for it. Otherwise, edit an existing project by clicking the pencil icon next to it, or create another.
* Paste the code snippet below into the editor
** Adjust daysAgo on how many days you wish to keep in your Inbox for.
** Adjust onlyInbox, if you want to search all your email (archive too) for Habitica based emails.
** Remove any items in removeSubject Array that you do not wish to archive.
 function removeHabiticaMessages() {
 var daysAgo = 14
 var onlyInbox = true
 var removeSubject = ['You Received a Private Message!', 'Your Quest Has Started!', 'Help % battle the', 'Help % Complete the','You Won a Challenge on Habitica!', 'has gifted you some gems!', 'Fight Monsters (and Bad Habits)!', 'Removed from Party', 'Wants You to Join a Guild!', 'Removed from Guild']
 // This is the items in list - Received a PM, Quest Start, Request to Fight a Boss Quest, Request to Complete Collection Quest, Won a Challenge, Received Gems, Invited to Party, Remove From Party, Invited to Guild, Remove From Guild
 
 var now = new Date();
 var removalDate_Raw = new Date(now.getTime()-daysAgo*(24*3600*1000))
 var removalDate = Utilities.formatDate(removalDate_Raw, 'GMT', 'yyyy/MM/dd') 
 
 for (var i = 0; i < removeSubject.length; i++) {
 var criteria = 'subject:' + removeSubject[i] + ' from:messengers@habitica.com before:' + removalDate;
 if (onlyInbox) criteria += ' in:Inbox'
 var conversations = GmailApp.search(criteria);
 
 for (var idx = 0; idx < conversations.length; idx = idx + 1) { 
 var thread = conversations[idx];
 thread.moveToTrash()
 }
 }
 }
* Under Edit, select Current project's triggers. Then add a trigger that runs removeHabiticaMessages Time-driven on a Day timer. Suggested time is after your cron, early in the morning, like 3-4am.
* Save and you're done!