소스 검색

LeaderSet, LeaderUnset, TerminateWorker added

singulart 2 년 전
부모
커밋
ec4b7ce34b
2개의 변경된 파일74개의 추가작업 그리고 2개의 파일을 삭제
  1. 26 2
      scripts/wg-bot/src/joystream/discord.ts
  2. 48 0
      scripts/wg-bot/src/joystream/embeds.ts

+ 26 - 2
scripts/wg-bot/src/joystream/discord.ts

@@ -1,14 +1,14 @@
 import { EventRecord } from '@polkadot/types/interfaces'
 import { getWorker, getMember, getMint, getEvents, getBlockHash, getWorkerReward } from '../lib/api';
 import { getHiringOpening, getOpening } from './api_extension';
-import { getMintCapacityChangedEmbed, getOpeningAddedEmbed, getOpeningFilledEmbed, getWorkerRewardAmountUpdatedEmbed } from './embeds';
+import { getLeaderSetEmbed, getLeaderUnsetEmbed, getMintCapacityChangedEmbed, getOpeningAddedEmbed, getOpeningFilledEmbed, getWorkerExitedEmbed, getWorkerRewardAmountUpdatedEmbed, getWorkerTerminatedEmbed } from './embeds';
 
 import { wgEvents, workingGroups } from '../config'
 import Discord from 'discord.js';
 import { ApiPromise } from '@polkadot/api';
 import { MintBalanceOf, MintId } from '@joystream/types/mint';
 import { OpeningId } from '@joystream/types/hiring';
-import { WorkerId } from '@joystream/types/working-group';
+import { RationaleText, WorkerId } from '@joystream/types/working-group';
 
 export const processBlock = async (api: ApiPromise, client: Discord.Client, blockNumber: number) => {
     const hash = await getBlockHash(api, blockNumber);
@@ -51,6 +51,30 @@ export const processBlock = async (api: ApiPromise, client: Discord.Client, bloc
                         const reward = await getWorkerReward(api, hash, worker.reward_relationship.unwrap());
                         channel.send({ embeds: [getWorkerRewardAmountUpdatedEmbed(reward, member, blockNumber, value)] });
                         break;
+                    case "TerminatedLeader":
+                    case "TerminatedWorker":
+                        const terminatedId = data[0] as WorkerId;
+                        const terminatedReason = (data[1] as RationaleText).toString();
+                        const terminatedIdWorker = await getWorker(api, section, hash, terminatedId.toNumber());
+                        const terminatedMember = await getMember(api, terminatedIdWorker.member_id);
+                        channel.send({ embeds: [getWorkerTerminatedEmbed(terminatedMember, terminatedReason, blockNumber, value)] });
+                        break;
+                    case "WorkerExited":
+                        const exitedId = data[0] as WorkerId;
+                        const exitedReason = (data[1] as RationaleText).toString();
+                        const exitedIdWorker = await getWorker(api, section, hash, exitedId.toNumber());
+                        const exitedMember = await getMember(api, exitedIdWorker.member_id);
+                        channel.send({ embeds: [getWorkerExitedEmbed(exitedMember, exitedReason, blockNumber, value)] });
+                        break;
+                    case "LeaderSet": 
+                        const leaderId = data[0] as WorkerId;
+                        const leaderIdWorker = await getWorker(api, section, hash, leaderId.toNumber());
+                        const leaderMember = await getMember(api, leaderIdWorker.member_id);
+                        channel.send({ embeds: [getLeaderSetEmbed(leaderMember, blockNumber, value)] });
+                        break;
+                    case "LeaderUnset": 
+                        channel.send({ embeds: [getLeaderUnsetEmbed(blockNumber, value)] });
+                        break;
                 }
             } else {
                 console.log(`Channel not configured for ${section}`);

+ 48 - 0
scripts/wg-bot/src/joystream/embeds.ts

@@ -62,4 +62,52 @@ export const getWorkerRewardAmountUpdatedEmbed = (reward: RewardRelationship, me
             { name: 'Tx', value: event.hash.toString(), inline: true },
         )
         .setTimestamp();
+}
+
+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();
+}
+
+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();
+}
+
+export const getWorkerTerminatedEmbed = (member: Membership, reason: string,
+    blockNumber: number, event: EventRecord): Discord.MessageEmbed => {
+    return getWorkerExitedOrTerminatedEmbed('been terminated', member, reason, blockNumber, event);
+}
+
+export const getWorkerExitedEmbed = (member: Membership, reason: string,
+    blockNumber: number, event: EventRecord): Discord.MessageEmbed => {
+    return getWorkerExitedOrTerminatedEmbed('exited', member, reason, blockNumber, event);
+}
+
+export const getWorkerExitedOrTerminatedEmbed = (action: string, member: Membership, reason: string,
+    blockNumber: number, event: EventRecord): Discord.MessageEmbed => {
+
+    return new Discord.MessageEmbed()
+        .setColor(joystreamBlue)
+        .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();
 }