Bläddra i källkod

fix double sending

  Note: composing messages took longer than a block for some
  notifications and they were sent multiple times.
Joystream Stats 3 år sedan
förälder
incheckning
3e8b6a8365

+ 11 - 11
community-contributions/joystreamtelegrambot/src/bot.ts

@@ -121,6 +121,7 @@ const main = async () => {
   let stake = 0;
   let vals = 0;
   let noms = 0;
+  let announced: { [key: string]: boolean } = {};
 
   const channels: number[] = [0, 0];
   const posts: number[] = [0, 0];
@@ -191,7 +192,7 @@ const main = async () => {
       // heartbeat
       if (timestamp > lastHeartbeat + heartbeat) {
         const time = passedTime(lastHeartbeat, timestamp);
-        blocks = await announce.heartbeat(
+        announce.heartbeat(
           api,
           blocks,
           time,
@@ -200,6 +201,7 @@ const main = async () => {
           discordChannels.tokenomics
         );
         lastHeartbeat = block.timestamp;
+        blocks = [];
       }
 
       // announcements
@@ -215,21 +217,23 @@ const main = async () => {
       if (opts.channel) {
         channels[1] = await get.currentChannelId(api);
         if (channels[1] > channels[0])
-          channels[0] = await announce.channels(
+          announce.channels(
             api,
             channels,
             sendMessage,
             discordChannels.channels
           );
+        channels[0] = channels[1];
       }
 
       if (opts.proposals) {
         proposals.current = await get.proposalCount(api);
+
         if (
-          proposals.current > proposals.last ||
-          (timestamp > lastProposalUpdate + 60000 * proposalDelay &&
-            (proposals.active || proposals.executing))
+          proposals.current > proposals.last &&
+          !announced[proposals.current]
         ) {
+          announced[`proposal${proposals.current}`] = true;
           proposals = await announce.proposals(
             api,
             proposals,
@@ -243,12 +247,8 @@ const main = async () => {
 
       if (opts.forum) {
         posts[1] = await get.currentPostId(api);
-        posts[0] = await announce.posts(
-          api,
-          posts,
-          sendMessage,
-          discordChannels.forum
-        );
+        announce.posts(api, posts, sendMessage, discordChannels.forum);
+        posts[0] = posts[1];
       }
 
       printStatus(opts, { block: id, chain, posts, proposals });

+ 2 - 4
community-contributions/joystreamtelegrambot/src/lib/announcements.ts

@@ -40,7 +40,7 @@ export const channels = async (
   channels: number[],
   sendMessage: Send,
   channel: any
-): Promise<number> => {
+): Promise<void> => {
   const [last, current] = channels;
   const messages: string[] = [];
 
@@ -56,7 +56,6 @@ export const channels = async (
     );
   }
   sendMessage(messages.join("\r\n\r\n"), channel);
-  return current;
 };
 
 // announce council change
@@ -247,7 +246,7 @@ export const heartbeat = async (
   proposals: Proposals,
   sendMessage: Send,
   channel: any
-): Promise<[]> => {
+): Promise<void> => {
   const price = await fetchTokenValue();
   const storageSize = await fetchStorageSize();
   const durations = blocks.map((b) => b.duration);
@@ -286,7 +285,6 @@ export const heartbeat = async (
   `;
 
   sendMessage(msg, channel);
-  return [];
 };
 
 export const formatProposalMessage = (data: string[]): string => {

+ 1 - 1
community-contributions/joystreamtelegrambot/src/lib/getters.ts

@@ -16,7 +16,7 @@ import { Proposal } from "@joystream/types/proposals";
 
 // channel
 
-export const currentChannelId = async (api: Api): Promise<number> => -1
+export const currentChannelId = async (api: Api): Promise<number> => -1;
 
 export const memberHandle = async (api: Api, id: MemberId): Promise<string> => {
   const membership: Membership = await api.query.members.membershipById(id);