From b1e7ca3c74a3a6a135bf61100eb9360759c3cde5 Mon Sep 17 00:00:00 2001 From: git-bruh Date: Thu, 12 Nov 2020 20:12:23 +0530 Subject: [PATCH] Update main.py --- main.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index d4cf1ac..d5af5d4 100644 --- a/main.py +++ b/main.py @@ -30,7 +30,8 @@ def config_gen(config_file): config = config_gen("config.json") discord_client = discord.Client() -logger = logging.getLogger(__name__) + +logging.basicConfig(level=logging.INFO) @discord_client.event @@ -94,14 +95,21 @@ async def create_matrix_client(): username = config["username"] password = config["password"] + timeout = 30000 + global matrix_client matrix_client = nio.AsyncClient(homeserver, username) - matrix_client.add_event_callback(message_callback, nio.RoomMessageText) - print(await matrix_client.login(password)) - await matrix_client.sync_forever(timeout=30000) + # Sync once before adding callback to avoid acting on old messages + await matrix_client.sync(timeout) + + matrix_client.add_event_callback(message_callback, nio.RoomMessageText) + + # Sync forever + await matrix_client.sync_forever(timeout=timeout) + await matrix_client.close() @@ -121,12 +129,11 @@ async def message_callback(room, event): if not message: return - print( - f"Bot message received for room {room.display_name} | " - f"{room.user_name(event.sender)}: {message}" - ) + # Don't reply to ourselves + if event.sender == matrix_client.user: + return - # await webhook_send(event.sender, message) + await webhook_send(event.sender, message) def main(): @@ -134,4 +141,5 @@ def main(): discord_client.run(config["token"]) -main() +if __name__ == "__main__": + main()