Skip to content

Instantly share code, notes, and snippets.

@msciotti
Created November 26, 2019 20:12
Show Gist options
  • Save msciotti/e3aa75937db68aa8c968f18c2ff4a092 to your computer and use it in GitHub Desktop.
Save msciotti/e3aa75937db68aa8c968f18c2ff4a092 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using Discord;
using UnityEngine;
public class DiscordController : MonoBehaviour
{
public Discord.Discord discord;
public Discord.LobbyManager lobbyManager;
public System.Int64 lobbyId;
private bool created = false;
private bool updated = false;
public void OnClick()
{
for (int i = 0; i < lobbyManager.MemberCount(lobbyId); i++)
{
var userId = lobbyManager.GetMemberUserId(lobbyId, i);
lobbyManager.SendNetworkMessage(lobbyId, userId, 1, System.Text.Encoding.UTF8.GetBytes("Hello!"));
Debug.Log("Sent message to " + userId);
}
}
// Use this for initialization
void Start()
{
discord = new Discord.Discord(MY_CLIENT_ID, (System.UInt64)Discord.CreateFlags.Default);
discord.SetLogHook(Discord.LogLevel.Debug, (Discord.LogLevel level, string message) =>
{
Debug.Log(message);
});
lobbyManager = discord.GetLobbyManager();
lobbyManager.OnNetworkMessage += (lobbyId, userId, channelId, data) =>
{
Debug.Log(string.Format("{0} - from {1} on channel {2}: {3}", lobbyId, userId, channelId, System.Text.Encoding.UTF8.GetString(data)));
};
lobbyManager.OnMemberUpdate += (lobby_Id, userId) =>
{
var route = lobbyManager.GetMemberMetadataValue(lobby_Id, userId, "$route");
Debug.Log(route);
if (route != null && updated == false)
{
updated = true;
Debug.Log("here");
lobbyManager.OpenNetworkChannel(lobbyId, 1, false);
Debug.Log("opened channel");
}
};
if (!created)
{
var txn = lobbyManager.GetLobbyCreateTransaction();
txn.SetCapacity(5);
txn.SetType(Discord.LobbyType.Public);
lobbyManager.CreateLobby(txn, (Discord.Result res, ref Discord.Lobby lobby) =>
{
if (res == Discord.Result.Ok)
{
Debug.Log(lobbyManager.GetLobbyActivitySecret(lobby.Id));
lobbyId = lobby.Id;
created = true;
lobbyManager.ConnectNetwork(lobbyId);
}
});
}
}
// Update is called once per frame
void Update()
{
discord.RunCallbacks();
}
void LateUpdate()
{
lobbyManager.FlushNetwork();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment