local Discord = {} Discord.opcodes = { auth = 0, frame = 1, closed = 2, } -- Discord RPC Subscription events -- https://discord.com/developers/docs/topics/rpc#commands-and-events-rpc-events -- Ready: https://discord.com/developers/docs/topics/rpc#ready -- Error: https://discord.com/developers/docs/topics/rpc#error Discord.events = { READY = "READY", ERROR = "ERROR", } local struct = require("deps.struct") -- Initialize a new Discord RPC client function Discord:init(options) self.log = options.logger self.client_id = options.client_id self.ipc_socket = options.ipc_socket self.pipe = vim.loop.new_pipe(false) return self end -- Connect to the local Discord RPC socket -- TODO Might need to check for pipes ranging from discord-ipc-0 to discord-ipc-9: -- https://github.com/discord/discord-rpc/blob/master/documentation/hard-mode.md#notes function Discord:connect(on_connect) if self.pipe:is_closing() then self.pipe = vim.loop.new_pipe(false) end self.pipe:connect(self.ipc_socket, on_connect) end function Discord:is_connected() return self.pipe:is_active() end -- Disconnect from the local Discord RPC socket function Discord:disconnect(on_close) self.pipe:shutdown() if not self.pipe:is_closing() then self.pipe:close(on_close) end end -- Make a remote procedure call to Discord -- Callback argument in format: on_response(error[, response_table]) function Discord:call(opcode, payload, on_response) self.encode_json(payload, function(success, body) if not success then self.log:warn(string.format("Failed to encode payload: %s", vim.inspect(body))) return end -- Start reading for the response self.pipe:read_start(function(...) self:read_message(payload.nonce, on_response, ...) end) -- Construct message denoting little endian, auth opcode, msg length local message = struct.pack("