Bläddra i källkod

extracted block processing function

singulart 2 år sedan
förälder
incheckning
a128c77975
3 ändrade filer med 39 tillägg och 34 borttagningar
  1. 5 33
      scripts/wg-bot/src/index.ts
  2. 33 0
      scripts/wg-bot/src/joystream/discord.ts
  3. 1 1
      scripts/wg-bot/src/lib

+ 5 - 33
scripts/wg-bot/src/index.ts

@@ -1,15 +1,11 @@
 import { connectUpstream } from './joystream/ws'
 import { ApiPromise } from '@polkadot/api'
-import { BlockHash, Event, EventRecord, Header } from '@polkadot/types/interfaces'
-import { wgEvents, workingGroups } from './config'
-import Discord, { Intents } from 'discord.js';
-import { getWorker, getMember, getEvents, getBlockHash } from './lib/api';
-import { Vec } from '@polkadot/types';
+import { Header } from '@polkadot/types/interfaces'
+import Discord, { Intents } from 'discord.js'
+import {processBlock} from './joystream/discord'
+import { workingGroups } from './config'
 
 
-const TYPES_AVAILABLE = [] as const
-type ApiType = typeof TYPES_AVAILABLE[number]
-
 const discordBotToken = process.env.TOKEN || undefined // environment variable TOKEN must be set
 
 ;(async () => {
@@ -29,31 +25,7 @@ const discordBotToken = process.env.TOKEN || undefined // environment variable T
     connectUpstream().then( async (api: ApiPromise) => {
         api.rpc.chain.subscribeNewHeads(async (header: Header) => {
           const id = +header.number;
-          const hash = await getBlockHash(api, id);
-          const blockEvents = await getEvents(api, hash);
-          blockEvents.forEach(async (value: EventRecord, index: number, array: EventRecord[]) => {
-            let { section, method, data } = value.event;
-            if (wgEvents.includes(method) && Object.keys(workingGroups).includes(section)) {
-                console.log(section);
-                console.log(method);
-                console.log(data.toJSON());
-
-                const channel: Discord.TextChannel = client.channels.cache.get(workingGroups[section]) as Discord.TextChannel;
-                if(channel) {
-                    const exampleEmbed = new Discord.MessageEmbed()
-                    .setColor('#4038FF') // official joystream blue, see https://www.joystream.org/brand/guides/
-                    .setTitle('Working Group Update')
-                    .setDescription('//TODO') 
-                    .addFields(
-                      { name: 'Data', value: data.toString(), inline: true },
-                    )
-                    .setTimestamp();
-                    channel.send({ embeds: [exampleEmbed] });    
-                } else {
-                    console.log(`Channel not configured for ${section}`);
-                }
-            }
-          });
+          await processBlock(api, client, id);
         })  
       })
 })()

+ 33 - 0
scripts/wg-bot/src/joystream/discord.ts

@@ -0,0 +1,33 @@
+import { EventRecord} from '@polkadot/types/interfaces'
+import { getWorker, getMember, getEvents, getBlockHash } from '../lib/api';
+import { wgEvents, workingGroups } from '../config'
+import Discord from 'discord.js';
+import { ApiPromise } from '@polkadot/api';
+
+export const processBlock = async (api: ApiPromise, client: Discord.Client, blockNumber: number) => {
+    const hash = await getBlockHash(api, blockNumber);
+    const blockEvents = await getEvents(api, hash);
+    blockEvents.forEach(async (value: EventRecord, index: number, array: EventRecord[]) => {
+      let { section, method, data } = value.event;
+      if (wgEvents.includes(method) && Object.keys(workingGroups).includes(section)) {
+          console.log(section);
+          console.log(method);
+          console.log(data.toJSON());
+
+          const channel: Discord.TextChannel = client.channels.cache.get(workingGroups[section]) as Discord.TextChannel;
+          if(channel) {
+              const exampleEmbed = new Discord.MessageEmbed()
+              .setColor('#4038FF') // official joystream blue, see https://www.joystream.org/brand/guides/
+              .setTitle('Working Group Update')
+              .setDescription('//TODO') 
+              .addFields(
+                { name: 'Data', value: data.toString(), inline: true },
+              )
+              .setTimestamp();
+              channel.send({ embeds: [exampleEmbed] });    
+          } else {
+              console.log(`Channel not configured for ${section}`);
+          }
+      }
+    });
+}

+ 1 - 1
scripts/wg-bot/src/lib

@@ -1 +1 @@
-Subproject commit 0d5b8b74eb353a9f5f2d7c71730a1800bb2a8042
+Subproject commit a6974a9ae485c68d99a6ebd913df3624780913f0