فهرست منبع

Discord client logging in

singulart 2 سال پیش
والد
کامیت
41f7d11c72
2فایلهای تغییر یافته به همراه32 افزوده شده و 3 حذف شده
  1. 31 2
      scripts/wg-bot/src/index.ts
  2. 1 1
      scripts/wg-bot/src/joystream/ws.ts

+ 31 - 2
scripts/wg-bot/src/index.ts

@@ -2,13 +2,27 @@ import { connectUpstream } from './joystream/ws'
 import { ApiPromise } from '@polkadot/api'
 import { BlockHash, EventRecord, Header } from '@polkadot/types/interfaces'
 import { wgEvents, workingGroups } from './config'
-import { AugmentedEvents } from '@polkadot/api/types/events'
+import Discord, { Intents } from 'discord.js';
 
 const TYPES_AVAILABLE = [] as const
 type ApiType = typeof TYPES_AVAILABLE[number]
 
+const discordBotToken = process.env.TOKEN || undefined // environment variable TOKEN must be set
+
 ;(async () => {
   
+    const client = new Discord.Client({ intents: [Intents.FLAGS.GUILDS] });
+
+    client.once("ready", async () => {
+      console.log('Discord.js client ready');
+      Object.values(workingGroups).forEach( async (mappedChannel: string) => {
+        await client.channels.fetch(mappedChannel);
+      })
+    });
+    
+    await client.login(discordBotToken); 
+    console.log('Bot logged in successfully');    
+    
     connectUpstream().then( async (api: any) => {
         api.rpc.chain.subscribeNewHeads(async (header: Header) => {
           const id = +header.number
@@ -19,7 +33,22 @@ type ApiType = typeof TYPES_AVAILABLE[number]
             if (wgEvents.includes(method) && Object.keys(workingGroups).includes(section)) {
                 console.log(section);
                 console.log(method);
-                console.log(data);
+                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/joystream/ws.ts

@@ -9,7 +9,7 @@ const wsLocation =
 
 export const connectUpstream = async (): Promise<ApiPromise> => {
     try {
-        //console.debug(`[Joystream] Connecting to ${wsLocation}`)
+        console.debug(`[Joystream] Opening websocket ${wsLocation}`)
         const provider = new WsProvider(wsLocation)
         const api = await ApiPromise.create({ provider, types })
         await api.isReady