Created
January 21, 2022 06:41
-
-
Save denzuko/5f2c0a960cf134186d32cf7386e41342 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Eris from "eris"; | |
const dataFactory = (error, value) => ({ | |
error: Boolean(error), | |
data: value | |
}); | |
export default class Radio extends Eris { | |
constructor(props) { | |
super(props.token || ""); | |
this.state = { | |
json: dataFactory(false, ""), | |
connections: [], | |
streamUri: "http://lofi.jakeoid.com:8000/stream", | |
xmlrpc: "http://lofi.jakeoid.com:8000/status-json.xsl", | |
token: "", | |
defaultVC: "", | |
developers: [] | |
}; | |
this.on("ready", () => { | |
setInterval(this.render, 10000); | |
this.render(); | |
}); | |
this.on("messageCreate", (message) => { | |
let self = this; | |
if (message.author.bot) return; | |
if (message.content.startsWith("🎵setvolume ")) { | |
let conn = null; | |
try { | |
conn = self.state.connections.filter( | |
(a) => a.channelID === message.member.voiceState.channelID | |
); | |
try { | |
let volume = parseFloat(message.content.slice(12)); | |
if (conn != null) { | |
let msg = `🎵 **Volume set to ${volume}**`; | |
if (volume < 2 || volume > 0) { | |
conn.setVolume(volume); | |
} else { | |
msg = "🎵 **That volume is too high!**"; | |
} | |
self.createMessage(message.channel.id, msg); | |
} else { | |
self.createMessage( | |
message.channel.id, | |
"🎵 **Make sure you and I are both in the same voice channel first!**" | |
); | |
} | |
} catch (Exception) { | |
self.createMessage( | |
"🎵 **Invalid volume! Choose a number between 0.1 and 2.0**" | |
); | |
} | |
} catch (Exception) { | |
console.error(Exception); | |
} | |
let msg = ""; | |
switch (message.content) { | |
case "🎵ping": | |
msg = self | |
.createMessage(message.channel.id, "🎵") | |
.then((msg) => | |
msg.edit(`🎵 \`${msg.timestamp - message.timestamp}ms\``) | |
); | |
break; | |
case "🎵help": | |
msg = [ | |
"🎵 **A bot created by PikaDude for Jakeoid's Lo-Fi Online Radio Station.** 🎵", | |
"I display stats about the radio station and have the ability to play the radio in voice channels.", | |
"**Commands:**", | |
"**🎵ping**", | |
"**🎵help**", | |
"**🎵nowplaying**", | |
"**Discord Server**: https://discord.gg/jYRnUnJ", | |
null, | |
"**Listen Now**: http://lofi.jakeoid.com/" | |
].join("\n"); | |
this.createMessage(message.channel.id, msg); | |
break; | |
case "🎵nowplaying": | |
case "🎵np": | |
let _data = this.state.json.data.icestats.source; | |
this.createMessage(message.channel.id, { | |
embed: { | |
title: "Now Playing", | |
description: `[Listen Now!](http://lofi.jakeoid.com/)`, | |
fields: [ | |
{ | |
name: "Title", | |
value: _data.title, | |
inline: true | |
}, | |
{ | |
name: "Artist", | |
value: _data.artist, | |
inline: true | |
}, | |
{ | |
name: "Current Listeners", | |
value: _data.listeners, | |
inline: true | |
} | |
], | |
footer: { | |
text: this.state.json.error | |
? "We're having some issues connecting to the API, so these stats may not be accurate." | |
: null | |
} | |
} | |
}); | |
break; | |
default: | |
break; | |
} | |
console.log(message.author.username + ": " + message.content); | |
} | |
}); | |
if (Boolean(props.autoConnect)) this.connect(); | |
} | |
setState({ props }) { | |
this.state = props; | |
} | |
didComponentMount() { | |
let self = this; | |
this.joinVoiceChannel(this.state.defaultVC) | |
.catch((err) => console.error(err)) | |
.then((conn) => { | |
self.state.connections.push(conn); | |
conn.play(self.state.streamUri, { inlineVolume: true }); | |
conn.once("end", () => { | |
let index = self.state.connections.indexOf(conn); | |
if (index !== -1) conn.splice(index, 1); | |
self.didComponentMount(); | |
}); | |
}); | |
} | |
render() { | |
let self = this; | |
fetch(this.state.xmlrpc) | |
.then((res) => { | |
if (!res.ok && res.statusCode !== 200) | |
throw new Error( | |
`Failed to fetch JSON. Got HTTP response: ${res.statusCode}` | |
); | |
return res.json(); | |
}) | |
.then((body) => { | |
if (self.state["json"].data !== body) { | |
try { | |
self.setState({ | |
json: dataFactory(false, body) | |
}); | |
let source = body.icestats.source; | |
self.editStatus("online", { | |
name: `${source.artist} - ${source.title}` | |
}); | |
} catch (Exception) { | |
throw new Error(Exception); | |
} | |
} | |
}) | |
.catch((error) => { | |
self.setState({ json: dataFactory(true, error) }); | |
console.error(error); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment