index.ts 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. import { connectUpstream } from './joystream/ws'
  2. import { ApiPromise } from '@polkadot/api'
  3. import { Header } from '@polkadot/types/interfaces'
  4. import Discord, { Intents } from 'discord.js'
  5. import {processBlock} from './joystream/discord'
  6. import { workingGroups } from './config'
  7. const discordBotToken = process.env.TOKEN || undefined // environment variable TOKEN must be set
  8. ;(async () => {
  9. const client = new Discord.Client({ intents: [Intents.FLAGS.GUILDS] });
  10. client.once("ready", async () => {
  11. console.log('Discord.js client ready');
  12. Object.values(workingGroups).forEach( async (mappedChannel: string) => {
  13. await client.channels.fetch(mappedChannel);
  14. })
  15. });
  16. await client.login(discordBotToken);
  17. console.log('Bot logged in successfully');
  18. connectUpstream().then( async (api: ApiPromise) => {
  19. api.rpc.chain.subscribeNewHeads(async (header: Header) => {
  20. const id = +header.number;
  21. await processBlock(api, client, id);
  22. })
  23. })
  24. })()