Skip to content

Commit

Permalink
Add DiscordController.cs file, currently the presence (should) work…
Browse files Browse the repository at this point in the history
… on Intel but not M1
  • Loading branch information
sjalkote committed Jun 9, 2023
1 parent 230b4ca commit a10b9ad
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 1 deletion.
51 changes: 50 additions & 1 deletion Assets/Scenes/MainLevel/MainLevel.unity
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 705507994}
m_IndirectSpecularColor: {r: 0.18028378, g: 0.22571412, b: 0.30692285, a: 1}
m_IndirectSpecularColor: {r: 0.18028326, g: 0.22571333, b: 0.30692202, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down Expand Up @@ -2167,6 +2167,55 @@ Transform:
m_Father: {fileID: 1171578205}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1842752240
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1842752242}
- component: {fileID: 1842752241}
m_Layer: 0
m_Name: DiscordController
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1842752241
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1842752240}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 989c21f7179ce4bd299236345e2bd236, type: 3}
m_Name:
m_EditorClassIdentifier:
applicationID: 1115659409360703538
details: Monkeying Around
state: 'Current velocity: '
largeImage: banana_pile
largeText: MonkeBusiness
--- !u!4 &1842752242
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1842752240}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -20.413609, y: -5.7558146, z: 7.80624}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1894156808
GameObject:
m_ObjectHideFlags: 0
Expand Down
83 changes: 83 additions & 0 deletions Assets/Scripts/DiscordController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using UnityEngine;

public class DiscordController : MonoBehaviour
{
public long applicationID = 1115659409360703538;
[Space]
// TODO: Add status when in Main Menu, make sure to move the GameObject to that scene instead of this one
public string details = "Monkeying Around";
public string state = "Current velocity: ";
[Space]
public string largeImage = "banana_pile";
public string largeText = "Defending Bananas";

private CharacterController _character;
private long _time;

private static bool _instanceExists;
private Discord.Discord _discord;

void Awake()
{
// Transition the GameObject between scenes, destroy any duplicates
if (!_instanceExists)
{
_instanceExists = true;
DontDestroyOnLoad(gameObject);
}
else if (FindObjectsOfType(GetType()).Length > 1)
{
Destroy(gameObject);
}
}

void Start()
{
// Log in with the Application ID
_discord = new Discord.Discord(applicationID, (System.UInt64)Discord.CreateFlags.NoRequireDiscord);

_character = GameObject.FindWithTag("Player").GetComponent<CharacterController>();
_time = System.DateTimeOffset.Now.ToUnixTimeMilliseconds();

UpdateStatus();
}

void Update()
{
// Destroy the GameObject if Discord isn't running
try { _discord.RunCallbacks(); }
catch { Destroy(gameObject); }
}

void LateUpdate()
{
UpdateStatus();
}

void UpdateStatus()
{
// Update Status every frame
try
{
var activityManager = _discord.GetActivityManager();
var activity = new Discord.Activity
{
Details = details,
State = state + _character.velocity,
Assets = { LargeImage = largeImage, LargeText = largeText },
Timestamps = { Start = _time }
};

activityManager.UpdateActivity(activity, (res) =>
{
if (res != Discord.Result.Ok) Debug.LogWarning("Failed to connect to Discord: " + res);
});
}
catch { // If updating the status fails, Destroy the GameObject
Destroy(gameObject);
}
}

private void OnApplicationQuit() { _discord.Dispose(); }
}
11 changes: 11 additions & 0 deletions Assets/Scripts/DiscordController.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a10b9ad

Please sign in to comment.