Przeglądaj źródła

replaced sleep by timer from AnyEvent

alakiza 2 lat temu
rodzic
commit
cf097ab9ed
1 zmienionych plików z 13 dodań i 6 usunięć
  1. 13 6
      modules/commands.pm

+ 13 - 6
modules/commands.pm

@@ -1,5 +1,5 @@
 use Modern::Perl;
-use Time::HiRes qw( usleep );
+use AnyEvent;
 use utf8;
 
 use Mojo::AsyncAwait;
@@ -241,13 +241,20 @@ async command_purge_all_trees => sub :Alias("почистить_все_дере
 
     return notify($chatid, "$ip '$dev->{name}': $@") if $@;
 
-    foreach my $text (@{$res_ref}) {
-        await notify($chatid, "$ip '$dev->{name}'
+# Also note that your bot will not be able to send more than 20 messages per minute to the same group.
+    my $w;
+    $w = AnyEvent->timer (after => 1, interval => 3, cb => sub {
+        if (scalar @{$res_ref} == 0) {
+            undef $w;
+        }
+
+        my $text = shift @{$res_ref};
+        if ($text) {
+            notify($chatid, "$ip '$dev->{name}'
 <code>$text</code>");
-#Telegram не даёт отправлять боту больше 20-ти сообщений в секунду, выставляем некоторую задержку
-        sleep(1);
+        }
 
-    }
+    });
 
 };