LennyPhoenix / py-discord-sdk

Discord Game SDK for Python
MIT License
26 stars 7 forks source link

The event on_network_message don't fire #8

Open ascpial opened 2 years ago

ascpial commented 2 years ago

I recently started to use this library to create a small video game using this librairie. After the setup of lobby_manager.get_lobby_activity_secret and lobby_transaction.connect_network, I can send messages using lobby_manager.send_network_message, it seems to work, but I never get the message back. Here is some parts of my code : self.lobby_manager.connect_lobby_with_activity_secret(secret, self.on_lobby_joined_callback)

def on_lobby_joined_callback(self, result, lobby):
    if result == discordsdk.Result.ok:
        print("Lobby successfully connected")
        self.lobby:discordsdk.Lobby = lobby
        self.setup_lobby()
        self.update_activity()
    else:
        print(f"Error : {result}")
for i in range(self.lobby_manager.member_count(self.lobby.id)):
    user_id = self.lobby_manager.get_member_user_id(self.lobby.id, i)
    print(user_id, data)
    self.lobby_manager.send_network_message(
        self.lobby.id,
        user_id,
        channel_id,
        data
    )

Any ideas to fix it ?

LennyPhoenix commented 2 years ago

Hey, are you running lobby_manager.flush() to send the queued up network messages?

ascpial commented 2 years ago

Yes, I have two loops, loop (called before the game processing) and end_loop, after the game processing :

def loop(self):
    self.app.run_callbacks()

def end_loop(self):
    self.network_manager.flush()
    self.lobby_manager.flush_network()
ascpial commented 2 years ago

Maybe I messed up with registering?...

self.app = discordsdk.Discord(APPLICATION_ID, discordsdk.CreateFlags.default)
self.lobby_manager = self.app.get_lobby_manager()
self.lobby_manager.on_network_message = self.on_network_message
def on_network_message(self, lobby_id:int, user_id:int, message:bytes):
    print(lobby_id, user_id, message)
LennyPhoenix commented 2 years ago

hang on lemme try replicating, i havent worked on this library in a while so im not sure what the issue could be exactly

ascpial commented 2 years ago

If you want I can send you my code, the part that manage discord

LennyPhoenix commented 2 years ago

That would help allot yeah

LennyPhoenix commented 2 years ago
def on_network_message(self, lobby_id:int, user_id:int, message:bytes):
    print(lobby_id, user_id, message)

I think it should be noted that on_network_message takes three arguments:

lobby_id: int
user_id: int
channel_id: int
data: bytes
ascpial commented 2 years ago

If needed I can also document, I made this code quickly

import discordsdk

from playloads import ChannelType

APPLICATION_ID = 894563716710957096

class Discord:
    route: str
    lobby: discordsdk.Lobby = None

    def __init__(self, parent):
        self.parent = parent

        self.app = discordsdk.Discord(APPLICATION_ID, discordsdk.CreateFlags.default)
        self.activity_manager = self.app.get_activity_manager()
        self.network_manager = self.app.get_network_manager()
        self.lobby_manager = self.app.get_lobby_manager()

        self.activity_manager.on_activity_join = self.on_activity_join

        self.lobby_manager.on_network_message = self.on_network_message
        self.lobby_manager.on_member_connect = self.on_member_connect
        self.lobby_manager.on_member_disconnect = self.on_member_disconnect

        self.create_lobby()

    def update_activity(self):
        activity = discordsdk.Activity()
        activity.state = "En développement"
        activity.assets.large_image = "player"

        if self.lobby is not None:
            # Set join for lobby
            activity.secrets.join = self.lobby_manager.get_lobby_activity_secret(self.lobby.id)
            activity.party.id = str(self.lobby.id)
            activity.party.size.max_size = self.lobby.capacity
            activity.party.size.current_size = self.lobby_manager.member_count(self.lobby.id)

        self.activity_manager.update_activity(activity, self.activity_callback)

    def loop(self):
        self.app.run_callbacks()

    def end_loop(self):
        self.network_manager.flush()
        self.lobby_manager.flush_network()

    def stop(self):
        self.disconnect()

    def disconnect(self):
        self.lobby_manager.disconnect_lobby(self.lobby.id, self.on_lobby_leaved_callback)

    def create_lobby(self):
        transaction = self.lobby_manager.get_lobby_create_transaction()
        transaction.set_type(1)
        transaction.set_capacity(5)
        self.lobby_manager.create_lobby(
            transaction,
            self.lobby_callback
        )

    def setup_lobby(self):
        self.lobby_manager.connect_network(int(self.lobby.id))
        self.lobby_manager.open_network_channel(
            self.lobby.id,
            ChannelType.pos,
            False
        )

    def send_message(self, channel_id:int, data:bytes):
        """Send a message to all the peoples connected in the lobby"""
        for i in range(self.lobby_manager.member_count(self.lobby.id)):
            user_id = self.lobby_manager.get_member_user_id(self.lobby.id, i)
            print(user_id, data)
            self.lobby_manager.send_network_message(
                self.lobby.id,
                user_id,
                channel_id,
                data
            )

    def connect_lobby_idsecret(self, secret):
        self.disconnect()
        self.lobby_manager.connect_lobby_with_activity_secret(secret, self.on_lobby_joined_callback)

    def on_member_connect(self, lobby_id:int, user_id:int):
        print(f"{user_id} logged in")
        self.update_activity()

    def on_member_disconnect(self, lobby_id:int, user_id:int):
        print(f"{user_id} logged out")
        self.update_activity()

    def on_network_message(self, lobby_id:int, user_id:int, message:bytes):
        print(lobby_id, user_id, message)

    def on_activity_join(self, secret:str):
        self.connect_lobby_idsecret(secret)

    def lobby_callback(self, result, lobby):
        if result == discordsdk.Result.ok:
            print("Lobby successfully created")
            self.lobby = lobby
            self.setup_lobby()
            self.update_activity()
        else:
            print(f"Error : {result}")

    def activity_callback(self, result):
        if result == discordsdk.Result.ok:
            print("Activity successfully updated")
        else:
            print(f"Error : {result}")

    def on_lobby_joined_callback(self, result, lobby):
        if result == discordsdk.Result.ok:
            print("Lobby successfully connected")
            self.lobby:discordsdk.Lobby = lobby
            self.setup_lobby()
            self.update_activity()
        else:
            print(f"Error : {result}")

    def on_lobby_leaved_callback(self, result):
        if result == discordsdk.Result.ok:
            print("Lobby successfully disconected")
        else:
            print(f"Error : {result}")
ascpial commented 2 years ago

I think it should be noted that on_network_message takes three arguments:

lobby_id: int
user_id: int
channel_id: int
data: bytes

Yes thanks, I change it, but the issue is still there...

LennyPhoenix commented 2 years ago

OK im getting the same issue, I'm going to try replicating in C to see if its an issue with the Python wrapper or the SDK itself.

ascpial commented 2 years ago

Okay, good luck and thanks for your quick support 👍