Skip to content

Instantly share code, notes, and snippets.

@timcole
Last active May 3, 2022 17:12
Show Gist options
  • Select an option

  • Save timcole/98c762447cfad487cc334295ddd49203 to your computer and use it in GitHub Desktop.

Select an option

Save timcole/98c762447cfad487cc334295ddd49203 to your computer and use it in GitHub Desktop.
Spotify now playing via Phineas/lanyard with ComputerCraft

Spotify now playing via Phineas/lanyard with ComputerCraft

There was no reason for me to waste time making this by friends and I were playing Create: Above and Beyond and I was bored.

local monitor = peripheral.find("monitor")
local speaker = peripheral.find("speaker")
monitor.clear()
term.clear()
local ws = http.websocket("wss://api.lanyard.rest/socket")
local discord_id = "83281345949728768"
monitor.write("Connected to lanyard")
local spotify = nil
local w, h = monitor.getSize()
local color = colors.green
-- Spotify Green
term.setPaletteColour(colors.green, 0x1DB954)
local function messages()
while true do
event, url, message = os.pullEvent("websocket_message")
local data = textutils.unserializeJSON(message)
local op = data["op"]
print("> Got op", op)
if op == 1 then
ws.send(textutils.serializeJSON({
op = 2,
d = {
subscribe_to_id = discord_id
}
}))
print("< Sent op 2")
end
if op == 0 then
if data["d"]["spotify"] ~= nil and data["d"]["spotify"]["track_id"] ~= spotify then
color = colors.green
spotify = data["d"]["spotify"]["track_id"]
monitor.setBackgroundColour(color)
monitor.clear()
monitor.setCursorPos(2, math.floor(h / 2))
monitor.write("I'm currently listening to:")
monitor.setCursorPos(2, math.floor(h / 2) + 1)
monitor.write(data["d"]["spotify"]["song"])
monitor.setCursorPos(2, math.floor(h / 2) + 2)
monitor.write(data["d"]["spotify"]["artist"])
if speaker ~= nil then
speaker.playSound("entity.generic.explode")
end
end
if data["d"]["spotify"] == nil then
spotify = nil
color = colors.black
monitor.setBackgroundColour(color)
monitor.clear()
monitor.setCursorPos(2, math.floor(h / 2))
monitor.write("Not listening to anything at the moment.")
end
end
end
end
local function ping()
while true do
sleep(30)
if ws ~= nil then
ws.send(textutils.serializeJSON({
op = 3
}))
print("< Sent op 3")
end
end
end
parallel.waitForAny(messages, ping)
@Hexality

Hexality commented May 2, 2022

Copy link
Copy Markdown

cool but
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment