SHARE
TWEET

Discord RPC for Morrowind

a guest Apr 5th, 2018 107 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local discordRPC = require("libs.discordRPC")
  2.  
  3. -- All script overwrites need to create and return one of these.
  4. local this = LuaScript.new()
  5.  
  6. --[[
  7.  
  8.     This is a proof of concept of how to provide DLL-style extensions to Morrowind.
  9.     We do this by using LuaJIT's FFI to connect to a precompiled DLL file.
  10.  
  11.     In this example we will be interfacing with Discord's Rich Presence API to
  12.     show the player's name, race, class, level, and current location to people who
  13.     click on the user's name in Discord.
  14.  
  15.     The interface to the discord-rpc.dll library is pfirsich's MIT-licensed
  16.     lua-discord-RPC libary, which comes bundled with this download. Visit his
  17.     project here: https://github.com/pfirsich/lua-discordRPC
  18.  
  19. --]]
  20.  
  21. local presence = {
  22.     details = "In main menu",
  23.     largeImageKey = "default",
  24.     startTimestamp = 0,
  25. }
  26.  
  27. local updateClock
  28.  
  29. function updatePresenceDetails()
  30.     -- Set the details to the player's name/class/level.
  31.     local playerRef = tes3.getPlayerRef()
  32.     presence.details = string.format( "%s (%s, %s %d)", playerRef.object.name, playerRef.object.race.name, playerRef.object.class.name, playerRef.object.level )
  33.  
  34.     -- Set the state to the player cell.
  35.     local playerCell = tes3.getPlayerCell()
  36.     if (playerCell) then
  37.         presence.state = playerCell.id
  38.     else
  39.         presence.state = "Exploring the unknown!"
  40.     end
  41. end
  42.  
  43. -- This function is called once the script has been recognized, when esm/esp files
  44. -- are being loaded by the game. We will use this to initialize the connection and
  45. -- set start timestamps.
  46. function this:initialized()
  47.     discordRPC.initialize("428347126184017920", true)
  48.    
  49.     presence.startTimestamp = os.time(os.date("*t"))
  50.  
  51.     updateClock = os.clock()
  52. end
  53.  
  54. -- This function is called every time that the script would normally execute,
  55. -- instead of the mwscript that should be run. Here we will update presence info
  56. -- assuming that more than 30 seconds have passed since the last update.
  57. function this:execute()
  58.     -- Only update presence once every 30 seconds, or if we haven't updated it yet.
  59.     local timeDelta = os.clock() - updateClock
  60.     if ( timeDelta > 30 or presence.state == nil ) then
  61.         updatePresenceDetails()
  62.         discordRPC.updatePresence(presence)
  63.         updateClock = os.clock()
  64.     end
  65.    
  66.     discordRPC.runCallbacks()
  67. end
  68.  
  69. -- TODO: When we have our own quit event, we can provide a shutdown call.
  70. -- But, the discordRPC API handles this for us anyway on garbage collection.
  71. -- function this:someFutureQuitEvent()
  72. --     discordRPC.shutdown()
  73. -- end
  74.  
  75. function discordRPC.ready()
  76.     print("[discord_rpc]: Discord ready.")
  77.  
  78.     -- When Discord first reports that it is ready, update presence. We very likely
  79.     -- won't have interesting data at this point.
  80.     discordRPC.updatePresence(presence)
  81. end
  82.  
  83. function discordRPC.disconnected(errorCode, message)
  84.     print(string.format("[discord_rpc]: Discord disconnected (%d: %s)", errorCode, message))
  85. end
  86.  
  87. function discordRPC.errored(errorCode, message)
  88.     print(string.format("[discord_rpc]: Discord error (%d: %s)", errorCode, message))
  89. end
  90.  
  91. return this
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top