Selaa lähdekoodia

refactoring to remove some duplicated code

singulart 2 vuotta sitten
vanhempi
commit
7281389106

+ 7 - 2
scripts/wg-bot/README.md

@@ -4,6 +4,13 @@ Discord Bot which informs Joystream DAO about the important events related to wo
 
 Supports new Working Groups [introduced in Giza network](https://github.com/Joystream/helpdesk/tree/master/roles). 
 
+# Roadmap
+
+How would this product evolve depends chiefly on the demand from the DAO. However, it seems reasonable to add the following features:
+
+1. Show "before/after" state for Salary and Stake events
+2. Add a clickable link to apply on an opening
+
 
 # Configuration
 
@@ -14,5 +21,3 @@ The main configuration file is `src/config.ts`. Here, the mapping between the Wo
 Build with `yarn`
 
 Run using `TOKEN=<BOT TOKEN> RPC_ENDPOINT=wss://rome-rpc-endpoint.joystream.org:9944 ts-node src/index.ts`
-
-# Troubleshooting

+ 0 - 4
scripts/wg-bot/src/config.ts

@@ -13,7 +13,6 @@ export const workingGroups = {
 
 
 export const wgEvents = [
-    // 'AcceptedApplications', internal event ?
     'ApplicationTerminated',
     'ApplicationWithdrawn',
     'AppliedOnOpening',
@@ -29,10 +28,7 @@ export const wgEvents = [
     'TerminatedLeader', 
     'TerminatedWorker', 
     'WorkerExited', 
-    'WorkerRewardAccountUpdated',
     'WorkerRewardAmountUpdated', 
-    'WorkerRoleAccountUpdated', 
-    'WorkerStorageUpdated'
 ]
 
 export const joystreamBlue = '#4038FF' // official joystream blue, see https://www.joystream.org/brand/guides/

+ 29 - 69
scripts/wg-bot/src/joystream/embeds.ts

@@ -8,21 +8,16 @@ import { ApplicationId, ApplicationOf, Membership, OpeningOf, RewardRelationship
 
 export const getMintCapacityChangedEmbed = (minted: number, mint: u128, blockNumber: number, event: EventRecord): Discord.MessageEmbed => {
 
-    return new Discord.MessageEmbed()
-        .setColor(joystreamBlue)
+    return addCommonProperties(new Discord.MessageEmbed()
         .setTitle(`💰 💵 💸 💴 💶 ${formatBalance(minted, { withUnit: 'JOY' })} minted to the Treasury 💰 💵 💸 💴 💶 `)
         .addFields(
             { name: 'Balance', value: formatBalance(mint, { withUnit: 'JOY' }), inline: true },
-            { name: 'Block', value: blockNumber + "", inline: true },
-            { name: 'Tx', value: event.hash.toString(), inline: true },
-        )
-        .setTimestamp();
+        ), blockNumber, event );
 }
 
 export const getOpeningAddedEmbed = (opening: any, openingObject: OpeningOf, blockNumber: number, event: EventRecord): Discord.MessageEmbed => {
 
-    return new Discord.MessageEmbed()
-        .setColor(joystreamBlue)
+    return addCommonProperties(new Discord.MessageEmbed()
         .setTitle(`⛩ ${opening.headline} ⛩`)
         .setDescription(opening.job.description)
         .addFields(
@@ -31,79 +26,47 @@ export const getOpeningAddedEmbed = (opening: any, openingObject: OpeningOf, blo
             { name: 'Application Stake', value: openingObject.policy_commitment.application_staking_policy.unwrap().amount.toString() || 'Not Set', inline: true },
             { name: 'Role Stake', value: openingObject.policy_commitment.role_staking_policy.unwrap().amount.toString() || 'Not Set', inline: true },
             { name: 'Created By', value: opening.creator.membership.handle, inline: true },
-            { name: 'Block', value: blockNumber + "", inline: true },
-            { name: 'Tx', value: event.hash.toString(), inline: true },
-        )
-        .setTimestamp();
+        ), blockNumber, event );
 }
 
 export const getOpeningFilledEmbed = (opening: any, member: Membership, blockNumber: number, event: EventRecord): Discord.MessageEmbed => {
 
-    return new Discord.MessageEmbed()
-        .setColor(joystreamBlue)
-        .setTitle(`🎉 🥳 👏🏻 ${member.handle} was hired as ${opening.job.title} 🎉 🥳 👏🏻`)
-        .addFields(
-            { name: 'Block', value: blockNumber + "", inline: true },
-            { name: 'Tx', value: event.hash.toString(), inline: true },
-        )
-        .setTimestamp();
+    return addCommonProperties(new Discord.MessageEmbed().setTitle(`🎉 🥳 👏🏻 ${member.handle} was hired as ${opening.job.title} 🎉 🥳 👏🏻`), blockNumber, event );
 }
 
 export const getAppliedOnOpeningEmbed = (applicationId: ApplicationId, application: ApplicationOf, 
     openingText: any, hiringApplicationText: any, applicant: Membership, blockNumber: number, event: EventRecord): Discord.MessageEmbed => {
 
-    return new Discord.MessageEmbed()
-        .setColor(joystreamBlue)
+    return addCommonProperties(new Discord.MessageEmbed()
         .setTitle(`🏛 ${applicant.handle} applied to opening ${openingText.job.title}`)
         .setDescription(hiringApplicationText['About you']['What makes you a good fit for the job?'])
         .addFields(
             { name: 'Application ID', value: applicationId.toString(), inline: true},
             { name: 'Opening', value:  openingText.headline, inline: true},
             { name: 'Applicant', value: `[${application.member_id}] ${hiringApplicationText['About you']['Your name']}`, inline: true},
-            { name: 'Block', value: blockNumber + "", inline: true },
-            { name: 'Tx', value: event.hash.toString(), inline: true },
-        )
-        .setTimestamp();
+        ), blockNumber, event );
 }
 
 
 export const getWorkerRewardAmountUpdatedEmbed = (reward: RewardRelationship, member: Membership, 
     blockNumber: number, event: EventRecord): Discord.MessageEmbed => {
 
-    return new Discord.MessageEmbed()
-        .setColor(joystreamBlue)
+    return addCommonProperties(new Discord.MessageEmbed()
         .setTitle(`💰💰💰 Salary of ${member.handle} updated`)
         .addFields(
             { name: 'Salary', value: formatBalance(reward.amount_per_payout, { withUnit: 'JOY' }), inline: true },
             { name: 'Payout Frequency', value: reward.payout_interval + "", inline: true },
-            { name: 'Block', value: blockNumber + "", inline: true },
-            { name: 'Tx', value: event.hash.toString(), inline: true },
-        )
-        .setTimestamp();
+        ), blockNumber, event );
 }
 
 export const getLeaderSetEmbed = (member: Membership, blockNumber: number, event: EventRecord): Discord.MessageEmbed => {
 
-    return new Discord.MessageEmbed()
-        .setColor(joystreamBlue)
-        .setTitle(`🏛 ${member.handle} is a new Lead`)
-        .addFields(
-            { name: 'Block', value: blockNumber + "", inline: true },
-            { name: 'Tx', value: event.hash.toString(), inline: true },
-        )
-        .setTimestamp();
+    return addCommonProperties(new Discord.MessageEmbed().setTitle(`🏛 ${member.handle} is a new Lead`), blockNumber, event );
 }
 
 export const getLeaderUnsetEmbed = (blockNumber: number, event: EventRecord): Discord.MessageEmbed => {
 
-    return new Discord.MessageEmbed()
-        .setColor(joystreamBlue)
-        .setTitle(`🏛 Leader was unset`)
-        .addFields(
-            { name: 'Block', value: blockNumber + "", inline: true },
-            { name: 'Tx', value: event.hash.toString(), inline: true },
-        )
-        .setTimestamp();
+    return addCommonProperties(new Discord.MessageEmbed().setTitle(`🏛 Leader was unset`), blockNumber, event );
 }
 
 export const getWorkerTerminatedEmbed = (member: Membership, reason: string,
@@ -119,15 +82,11 @@ export const getWorkerExitedEmbed = (member: Membership, reason: string,
 export const getWorkerExitedOrTerminatedEmbed = (action: string, member: Membership, reason: string,
     blockNumber: number, event: EventRecord): Discord.MessageEmbed => {
 
-    return new Discord.MessageEmbed()
-        .setColor(joystreamBlue)
+    return addCommonProperties(new Discord.MessageEmbed()
         .setTitle(`🏛 Worker ${member.handle} has ${action}`)
         .addFields(
             { name: 'Reason', value: reason, inline: true },
-            { name: 'Block', value: blockNumber + "", inline: true },
-            { name: 'Tx', value: event.hash.toString(), inline: true },
-        )
-        .setTimestamp();
+        ), blockNumber, event );
 }
 
 export const getApplicationTerminatedEmbed = (applicationId: ApplicationId, application: ApplicationOf, member: Membership,
@@ -145,27 +104,28 @@ export const getApplicationWithdrawnEmbed = (applicationId: ApplicationId, appli
 export const getApplicationTerminatedOrWithdrawEmbed = (action: string, applicationId: ApplicationId, application: ApplicationOf, member: Membership,
     blockNumber: number, event: EventRecord): Discord.MessageEmbed => {
 
-    return new Discord.MessageEmbed()
-        .setColor(joystreamBlue)
+    return addCommonProperties(new Discord.MessageEmbed()
         .setTitle(`🏛 Application of ${member.handle} ${action}`)
         .addFields(
             { name: 'Application ID', value: applicationId.toString(), inline: true },
             { name: 'Opening ID', value: application.opening_id.toString(), inline: true },
-            { name: 'Block', value: blockNumber + "", inline: true },
-            { name: 'Tx', value: event.hash.toString(), inline: true },
-        )
-        .setTimestamp();
+        ), blockNumber, event );
 }
 
 export const getStakeUpdatedEmbed = (stake: Stake, member: Membership, action: string, blockNumber: number, event: EventRecord): Discord.MessageEmbed => {
-
-    return new Discord.MessageEmbed()
-        .setColor(joystreamBlue)
-        .setTitle(`💰💰💰 ${member.handle} stake has been ${action} `)
+    
+    return addCommonProperties(new Discord.MessageEmbed()
+        .setTitle(`💰💰💰 ${member.handle}'s stake has been ${action} `)
         .addFields(
-            { name: 'Stake', value: formatBalance(stake.value.toString(), { withUnit: 'JOY' }), inline: true },
-            { name: 'Block', value: blockNumber + "", inline: true },
-            { name: 'Tx', value: event.hash.toString(), inline: true },
-        )
-        .setTimestamp();
+            { name: 'Stake', value: formatBalance(stake.value.toString(), { withUnit: 'JOY' }), inline: true }
+        ), blockNumber, event );
 }
+
+const addCommonProperties = (embed: Discord.MessageEmbed, blockNumber: number, event: EventRecord) => {
+    return embed.addFields(
+        { name: 'Block', value: blockNumber + "", inline: true },
+        { name: 'Tx', value: event.hash.toString(), inline: true },
+    )
+    .setColor(joystreamBlue)
+    .setTimestamp();
+}

+ 2 - 0
scripts/wg-bot/src/simulate.ts

@@ -10,6 +10,8 @@ const eventsMapping = {
     'OpeningAdded': 4224720,
     'WorkerRewardAmountUpdated': 4222426,
     'AppliedOnOpening': 4264168,
+    'StakeIncreased': 4264798,
+    'StakeDecreased': 4264862,
 }