Browse Source

Improve time tracking of messages

Kirk Trombley 3 years ago
parent
commit
7dfd77ba58
2 changed files with 8 additions and 2 deletions
  1. 4 2
      drivers/groupme_driver.py
  2. 4 0
      rollbot/rollbot/types.py

+ 4 - 2
drivers/groupme_driver.py

@@ -6,6 +6,7 @@ import traceback
 import asyncio
 import asyncio
 import logging
 import logging
 import os
 import os
+import time
 
 
 import toml
 import toml
 from fastapi import FastAPI, BackgroundTasks, Response
 from fastapi import FastAPI, BackgroundTasks, Response
@@ -92,8 +93,9 @@ class GroupMeBot(rollbot.Rollbot[GroupMeMessage]):
         self.context.logger.info(f"Received: {result.status} - {await result.text()}")
         self.context.logger.info(f"Received: {result.status} - {await result.text()}")
 
 
     async def respond(self, res: rollbot.Response):
     async def respond(self, res: rollbot.Response):
-        # sleep for a moment to make groupme not misorder messages
-        await asyncio.sleep(0.5)
+        if res.cause is not None and (proc_time := time.time() - res.cause.received_at) < 1:
+            # sleep for a moment to make groupme not misorder messages
+            await asyncio.sleep(1 - proc_time)
 
 
         if res.origin_id != "GROUPME":
         if res.origin_id != "GROUPME":
             self.context.logger.error(f"Unable to respond to {res.origin_id}")
             self.context.logger.error(f"Unable to respond to {res.origin_id}")

+ 4 - 0
rollbot/rollbot/types.py

@@ -5,6 +5,7 @@ from dataclasses import dataclass, field
 from datetime import datetime
 from datetime import datetime
 from collections.abc import Callable, Coroutine, Container
 from collections.abc import Callable, Coroutine, Container
 from typing import Union, Any, Optional
 from typing import Union, Any, Optional
+import time
 
 
 from aiosqlite import Connection
 from aiosqlite import Connection
 from aiohttp import ClientSession
 from aiohttp import ClientSession
@@ -39,6 +40,7 @@ class Message:
     text: Optional[str] = None
     text: Optional[str] = None
     attachments: list[Attachment] = field(default_factory=list)
     attachments: list[Attachment] = field(default_factory=list)
     message_id: Optional[str] = None
     message_id: Optional[str] = None
+    received_at: float = field(default_factory=time.time)
 
 
     def __post_init__(self):
     def __post_init__(self):
         self.command = None
         self.command = None
@@ -90,6 +92,7 @@ class Response:
     channel_id: str
     channel_id: str
     text: Optional[str] = None
     text: Optional[str] = None
     attachments: Optional[list[Attachment]] = None
     attachments: Optional[list[Attachment]] = None
+    cause: Optional[Message] = None
 
 
     @staticmethod
     @staticmethod
     def from_message(
     def from_message(
@@ -100,6 +103,7 @@ class Response:
             channel_id=msg.channel_id,
             channel_id=msg.channel_id,
             text=text,
             text=text,
             attachments=attachments or [],
             attachments=attachments or [],
+            cause=msg,
         )
         )