Skip to content

Lobbies

Warning

Prerequisites:

Any code from the prerequisites can be omitted to make it easier to read. If you do want the complete code, look at the repository examples.

Lobbies are groups of users that can communicate via text and voice.
They are not the text/voice channels that you see in your Discord server.

Lobby vs Channel
  • Lobby is a temporary group to be used for communication with people that you just matched
    • Represents a matchmaking lobby
  • Channel is a permanent group to be used for communication with people that you may know
    • Represents a place to hangout with your friends/community

Create/Join

It will create the lobby if doesn't exist and join if it does.

GDScript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
extends Node


var application_id: int = 123456789012345678

var client := DiscordClient.new()


func _ready() -> void:
    client.set_application_id(application_id)
    client.set_status_changed_callback(_on_status_changed)


func _process(_delta: float) -> void:
    Discord.run_callbacks()


func _on_status_changed(status: DiscordClientStatus.Enum, _error: DiscordClientError.Enum, _error_detail: int) -> void:
    var enum_str: String = Discord.enum_to_string(status, DiscordClientStatus.id)

    print("Status changed to %s" % enum_str)

    if status == DiscordClientStatus.READY:
        client.create_or_join_lobby("your-unique-lobby-secret", _on_joined_lobby)


func _on_joined_lobby(result: DiscordClientResult, lobby_id: int) -> void:
    if result.successful():
        print("🎮 Lobby created or joined successfully! Lobby Id: %s" % lobby_id)
    else:
        print("❌ Lobby creation/join failed")

Leave

Is highly recommended to leave the lobby after using (it will delete itself after some minutes without nobody using).

GDScript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
extends Node


var application_id: int = 123456789012345678

var client := DiscordClient.new()


func _ready() -> void:
    client.set_application_id(application_id)
    client.set_status_changed_callback(_on_status_changed)


func _process(_delta: float) -> void:
    Discord.run_callbacks()


func _on_status_changed(status: DiscordClientStatus.Enum, _error: DiscordClientError.Enum, _error_detail: int) -> void:
    var enum_str: String = Discord.enum_to_string(status, DiscordClientStatus.id)

    print("Status changed to %s" % enum_str)

    if status == DiscordClientStatus.READY:
        client.create_or_join_lobby("your-unique-lobby-secret", _on_joined_lobby)


func _on_joined_lobby(result: DiscordClientResult, lobby_id: int) -> void:
    if result.successful():
        print("🎮 Lobby created or joined successfully! Lobby Id: %s" % lobby_id)

        await get_tree().create_timer(60).timeout

        client.leave_lobby(lobby_id, _on_left_lobby)
    else:
        print("❌ Lobby creation/join failed")


func _on_left_lobby(result: DiscordClientResult) -> void:
    if result.successful():
        print("🎮 Left lobby successfully!")
    else:
        print("❌ Leaving lobby failed")

Send Message

GDScript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
extends Node


var application_id: int = 123456789012345678

var client := DiscordClient.new()


func _ready() -> void:
    client.set_application_id(application_id)
    client.set_status_changed_callback(_on_status_changed)


func _process(_delta: float) -> void:
    Discord.run_callbacks()


func _on_status_changed(status: DiscordClientStatus.Enum, _error: DiscordClientError.Enum, _error_detail: int) -> void:
    var enum_str: String = Discord.enum_to_string(status, DiscordClientStatus.id)

    print("Status changed to %s" % enum_str)

    if status == DiscordClientStatus.READY:
        client.create_or_join_lobby("your-unique-lobby-secret", _on_joined_lobby)


func _on_joined_lobby(result: DiscordClientResult, lobby_id: int) -> void:
    if result.successful():
        print("🎮 Lobby created or joined successfully! Lobby Id: %s" % lobby_id)

        client.send_lobby_message(lobby_id, "Hello", _on_lobby_message)

        await get_tree().create_timer(60).timeout

        client.leave_lobby(lobby_id, _on_left_lobby)
    else:
        print("❌ Lobby creation/join failed")


func _on_lobby_message(result: DiscordClientResult, message_id: int) -> void:
    if result.successful():
        print("📨 Message sent successfully! Message Id: %s" % message_id)
    else:
        print("❌ Message sending failed")


func _on_left_lobby(result: DiscordClientResult) -> void:
    if result.successful():
        print("🎮 Left lobby successfully!")
    else:
        print("❌ Leaving lobby failed")

Receive Message

Exactly the same for receving direct messages:

GDScript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
extends Node


var application_id: int = 123456789012345678

var client := DiscordClient.new()


func _ready() -> void:
    client.set_application_id(application_id)
    client.set_message_created_callback(_on_message_created)
    client.set_status_changed_callback(_on_status_changed)


func _process(_delta: float) -> void:
    Discord.run_callbacks()


func _on_log(message: String, severity: DiscordLoggingSeverity.Enum) -> void:
    var enum_str: String = Discord.enum_to_string(severity, DiscordLoggingSeverity.id)

    print("[%s] %s" % [enum_str, message])


func _on_status_changed(status: DiscordClientStatus.Enum, _error: DiscordClientError.Enum, _error_detail: int) -> void:
    var enum_str: String = Discord.enum_to_string(status, DiscordClientStatus.id)

    print("Status changed to %s" % enum_str)

    if status == DiscordClientStatus.READY:
        client.create_or_join_lobby("your-unique-lobby-secret", _on_joined_lobby)


func _on_joined_lobby(result: DiscordClientResult, lobby_id: int) -> void:
    if result.successful():
        print("🎮 Lobby created or joined successfully! Lobby Id: %s" % lobby_id)

        await get_tree().create_timer(60).timeout

        client.leave_lobby(lobby_id, _on_left_lobby)
    else:
        print("❌ Lobby creation/join failed")


func _on_left_lobby(result: DiscordClientResult) -> void:
    if result.successful():
        print("🎮 Left lobby successfully!")
    else:
        print("❌ Leaving lobby failed")


func _on_message_created(message_id: int) -> void:
    var message = client.get_message_handle(message_id)

    if message is DiscordMessageHandle:
        print("📨 New message received: " % message.content())

Message History

Retrive recent messages from the lobby:

GDScript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
extends Node


var application_id: int = 123456789012345678

var client := DiscordClient.new()


func _ready() -> void:
    client.set_application_id(application_id)
    client.set_message_created_callback(_on_message_created)
    client.set_status_changed_callback(_on_status_changed)


func _process(_delta: float) -> void:
    Discord.run_callbacks()


func _on_log(message: String, severity: DiscordLoggingSeverity.Enum) -> void:
    var enum_str: String = Discord.enum_to_string(severity, DiscordLoggingSeverity.id)

    print("[%s] %s" % [enum_str, message])


func _on_status_changed(status: DiscordClientStatus.Enum, _error: DiscordClientError.Enum, _error_detail: int) -> void:
    var enum_str: String = Discord.enum_to_string(status, DiscordClientStatus.id)

    print("Status changed to %s" % enum_str)

    if status == DiscordClientStatus.READY:
        client.create_or_join_lobby("your-unique-lobby-secret", _on_joined_lobby)


func _on_joined_lobby(result: DiscordClientResult, lobby_id: int) -> void:
    if result.successful():
        print("🎮 Lobby created or joined successfully! Lobby Id: %s" % lobby_id)

        client.get_lobby_messages_with_limit(lobby_id, 50, _on_lobby_history)

        await get_tree().create_timer(60).timeout

        client.leave_lobby(lobby_id, _on_left_lobby)
    else:
        print("❌ Lobby creation/join failed")


func _on_lobby_history(result: DiscordClientResult, messages: Array[DiscordMessageHandle]) -> void:
    if result.successful():
        print("🕰 Retrieved %s messages from lobby chat history!" % messages.size())

        for message in messages:
            print("Message: %s" % message.content())
    else:
        print("❌ Failed to retrieve lobby chat history")


func _on_left_lobby(result: DiscordClientResult) -> void:
    if result.successful():
        print("🎮 Left lobby successfully!")
    else:
        print("❌ Leaving lobby failed")


func _on_message_created(message_id: int) -> void:
    var message = client.get_message_handle(message_id)

    if message is DiscordMessageHandle:
        print("📨 New message received: " % message.content())

References