Tuesday, February 1, 2022

Use Google Apps Script to post Gmail messages in a Discord channel

While helping a friend set up a Discord server and Patreon account we discovered that the Patreon Bot does not post any notifications about new patrons.  The only thing it does is assign Discord roles associated with Patreon tiers if configured by the creator AND the user has their Patreon account linked with their Discord account, which is rare.

Moderators need to receive a notification for each new patron so they can verify the member receives the roles and benefits they are entitled to even if their Patreon account is not linked to their Discord account.

We tried setting up a Scenario in Integromat but found no documentation on which Patreon fields contained the username - not the patron's full name or email address - as well as the pledge dollar amount.  Additionally, Integromat seemed to hit the limit of the free tier even though the scheduled job was set below stated limits.

On a large Discord there will be multiple moderators involved in daily operations who do not have access to the Patreon creator's account.  They don't need to know any of the patrons' personal information - just their username and the pledge amount.

It turns out Patreon sends an email notification to the creator's email address that contains this exact information in the subject line.  If only there was a way to have relevant emails translated into posts in a moderator channel.

Well, there is.  With Google Apps Script we can write a function that scans the emails in a Gmail inbox for messages that are from a particular address, are unread and have subject lines that match a regular expression.  The subject line can be extracted and used to construct a Discord embed post that looks pretty.  Then the message is marked as read so it isn't posted over and over.  We can then schedule this to run every half hour.

Implementation instructions are documented in the readme in the GitHub repository:

https://github.com/cgrote714/GoogleAppsPatreonNotify

Here's what the embed post looks like with some data redacted for privacy:


Currently the only Patreon email notification that is processed is one with a subject line that matches the ^New.*patron!.*$ regular expression.  We will want notifications for patrons who have cancelled their pledge as well so that moderators can make sure roles and benefits are removed.  Once one of those emails is received for the first time, an additional if code block can be added with the appropriate regular expression.

No comments:

Post a Comment