Selaa lähdekoodia

Fixed index out of range error when new list of announcements is smaller than the old list

allens 4 päivää sitten
vanhempi
commit
b7edb0017b
1 muutettua tiedostoa jossa 8 lisäystä ja 3 poistoa
  1. 8 3
      ownchatbot/__init__.py

+ 8 - 3
ownchatbot/__init__.py

@@ -47,9 +47,14 @@ def create_app(test_config=None):
         global current_index
         app.config.from_pyfile('announce.py', silent=True)
         announcements = app.config['ANNOUNCEMENTS']
-        message = announcements[current_index]
-        send_system_chat(message)
-        current_index = (current_index + 1) % len(announcements)
+        if current_index >= len(announcements):  # If reached the last announement, reset index
+            current_index = 0
+        try:
+            message = announcements[current_index]
+            send_system_chat(message)
+            current_index = (current_index + 1) % len(announcements)
+        except Exception as a_error:
+            app.logger.error(f'Couldn\'t make announcement: {a_error.args[0]}')
                 
     def award_job():
         with app.app_context():