Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local discordRPC = require("libs.discordRPC")
- -- All script overwrites need to create and return one of these.
- local this = LuaScript.new()
- --[[
- This is a proof of concept of how to provide DLL-style extensions to Morrowind.
- We do this by using LuaJIT's FFI to connect to a precompiled DLL file.
- In this example we will be interfacing with Discord's Rich Presence API to
- show the player's name, race, class, level, and current location to people who
- click on the user's name in Discord.
- The interface to the discord-rpc.dll library is pfirsich's MIT-licensed
- lua-discord-RPC libary, which comes bundled with this download. Visit his
- project here: https://github.com/pfirsich/lua-discordRPC
- --]]
- local presence = {
- details = "In main menu",
- largeImageKey = "default",
- startTimestamp = 0,
- }
- local updateClock
- function updatePresenceDetails()
- -- Set the details to the player's name/class/level.
- local playerRef = tes3.getPlayerRef()
- presence.details = string.format( "%s (%s, %s %d)", playerRef.object.name, playerRef.object.race.name, playerRef.object.class.name, playerRef.object.level )
- -- Set the state to the player cell.
- local playerCell = tes3.getPlayerCell()
- if (playerCell) then
- presence.state = playerCell.id
- else
- presence.state = "Exploring the unknown!"
- end
- end
- -- This function is called once the script has been recognized, when esm/esp files
- -- are being loaded by the game. We will use this to initialize the connection and
- -- set start timestamps.
- function this:initialized()
- discordRPC.initialize("428347126184017920", true)
- presence.startTimestamp = os.time(os.date("*t"))
- updateClock = os.clock()
- end
- -- This function is called every time that the script would normally execute,
- -- instead of the mwscript that should be run. Here we will update presence info
- -- assuming that more than 30 seconds have passed since the last update.
- function this:execute()
- -- Only update presence once every 30 seconds, or if we haven't updated it yet.
- local timeDelta = os.clock() - updateClock
- if ( timeDelta > 30 or presence.state == nil ) then
- updatePresenceDetails()
- discordRPC.updatePresence(presence)
- updateClock = os.clock()
- end
- discordRPC.runCallbacks()
- end
- -- TODO: When we have our own quit event, we can provide a shutdown call.
- -- But, the discordRPC API handles this for us anyway on garbage collection.
- -- function this:someFutureQuitEvent()
- -- discordRPC.shutdown()
- -- end
- function discordRPC.ready()
- print("[discord_rpc]: Discord ready.")
- -- When Discord first reports that it is ready, update presence. We very likely
- -- won't have interesting data at this point.
- discordRPC.updatePresence(presence)
- end
- function discordRPC.disconnected(errorCode, message)
- print(string.format("[discord_rpc]: Discord disconnected (%d: %s)", errorCode, message))
- end
- function discordRPC.errored(errorCode, message)
- print(string.format("[discord_rpc]: Discord error (%d: %s)", errorCode, message))
- end
- return this
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement