Skip to main content

DiscordController

A class in Root.Township, the global namespace.

Client: Discord Rich Presence. Shows the server, what the player is doing and where (forest, caves, town), and lets Discord friends join the server through the join secret.

Constructors​

MemberWhat it is
DiscordController()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

14 private and internal fields
MemberWhat it is
private Activity activityThe Discord activity being built.
private ActivityManager activityManagerDiscord SDK activity manager.
private string applicationIdDiscord application ID.
private Chunk chunkLast chunk the player was in.
private PlayerController controllerLast local player controller seen.
private Discord discordThe Discord SDK instance.
private bool isActivityChangedSet when a presence field changes, so LateUpdate sends it.
private bool isFirstWarningLets the first not-running warning through once.
private DiscordLarge largeImageBacking field for LargeImage.
private static Logger loggerNLog logger.
private PlayerStateType oldStateLast player state seen.
private DiscordSmall smallImageBacking field for SmallImage.
private PlayerStateManager stateManagerThe local player's state manager.
private string steamIdSteam app ID; unused here.

Properties​

MemberWhat it is
string Details { get; set; }Second presence line, such as In Combat! or Exploring the Caves.
DiscordController Instance { get; private set; }Meant as the singleton, but it is an instance property, so the duplicate check in Start never fires.
string JoinSecret { get; set; }Join secret sent to Discord; the joined server's ID.
DiscordLarge LargeImage { get; set; }Large presence image (splash, town, forest or caves).
string LargeImageHover { get; set; }Hover text for the large image.
string Server { get; set; }Party ID sent to Discord; the joined server's name.
DiscordSmall SmallImage { get; set; }Small presence image.
string SmallImageHover { get; set; }Hover text for the small image.
string State { get; set; }First presence line, usually the server name.

Methods​

15 private and internal methods
MemberWhat it is
private void ActivityInvite(ActivityActionType type, ref User user, ref Activity activity)Empty handler for Discord invites.
private async void ActivityJoin(string secret)Joins the server whose ID is in the Discord join secret.
private void ActivityJoinRequest(ref User user)Accepts every Discord join request.
private void ActivitySpectate(string secret)Empty handler for spectate.
private void Initialize()Applies the current player or menu state and listens for the local player being set.
private void LateUpdate()Runs Discord callbacks, disables itself if Discord isn't installed, and pushes the activity when it changed.
private void Log(string log)Logs a message with a DISCORD tag.
private void LogCallback(LogLevel level, string message)Forwards Discord SDK log lines to NLog at the matching level.
private void OnDestroy()Sets the state to Quitting and disposes the Discord SDK.
private void PlayerUnset(NetworkEntity entity, bool isUnload = false)Resets presence to In Menu when the local player goes away.
private void ResultCallback(Result result)Logs any Discord result that isn't OK.
private void SetPlayer(IPlayer player)Hooks the player's state changes and fills in the server name and join secret.
private void SetState(bool isForced = false)Rebuilds the presence text and image when the player, state or chunk changed (also checked on every move).
private void Start()Starts the Discord SDK, registers the launch command and hooks activity events; disables itself on failure.
private void StateChanged(IPlayerState playerState)Calls SetState when the player's state changes.