Formatting changes + Update version to 1.0.0

This commit is contained in:
2025-05-11 22:49:14 -05:00
parent e728666332
commit 27c72c53eb
12 changed files with 3372 additions and 149 deletions

View File

@@ -1,33 +1,35 @@
const { createAudioResource, createAudioPlayer, NoSubscriberBehavior } = require('@discordjs/voice');
const {
createAudioResource,
createAudioPlayer,
NoSubscriberBehavior,
} = require("@discordjs/voice");
class RadioPlayer {
static audioPlayer = null;
static audioPlayer = null;
static async getAudioPlayer(url) {
if(this.audioPlayer == null) {
let data = await fetch(url, {
headers: {
'User-Agent': 'O-Bot'
}}
);
if(data.status == 200) {
console.log('Creating audio resource');
var fileStream = data.body;
var res = createAudioResource(fileStream, {inlineVolume: true});
//res.volume.setVolume(2);
this.audioPlayer = createAudioPlayer({
behaviors: {noSubscriber: NoSubscriberBehavior.Play},
});
this.audioPlayer.play(res);
return this.audioPlayer;
}
else {
throw Error('Radio is down!');
}
}
else {
return this.audioPlayer;
}
}
static async getAudioPlayer(url) {
if (this.audioPlayer == null) {
let data = await fetch(url, {
headers: {
"User-Agent": "O-Bot",
},
});
if (data.status == 200) {
console.log("Creating audio resource");
var fileStream = data.body;
var res = createAudioResource(fileStream, {
inlineVolume: true,
});
this.audioPlayer = createAudioPlayer({
behaviors: { noSubscriber: NoSubscriberBehavior.Play },
});
this.audioPlayer.play(res);
return this.audioPlayer;
} else {
throw Error("Radio is down!");
}
} else {
return this.audioPlayer;
}
}
}
module.exports = RadioPlayer;
module.exports = RadioPlayer;

View File

@@ -1,15 +1,14 @@
function randomHex(){
let hex="";
let randNum = 0;
randNum = (Math.floor(Math.random()*Math.floor(41)));
if(randNum<16){
hex = "0" + randNum.toString(16);
}
else{
hex = randNum.toString(16);
}
function randomHex() {
let hex = "";
let randNum = 0;
randNum = Math.floor(Math.random() * Math.floor(41));
if (randNum < 16) {
hex = "0" + randNum.toString(16);
} else {
hex = randNum.toString(16);
}
return hex;
return hex;
}
module.exports = { randomHex };

View File

@@ -1,67 +1,59 @@
const { MessageFlags } = require('discord.js');
const { createAudioPlayer, createAudioResource, getVoiceConnection, joinVoiceChannel } = require('@discordjs/voice');
const RadioPlayer = require('./RadioPlayer.js');
async function play(interaction, sound){
try{
const { MessageFlags } = require("discord.js");
const {
createAudioPlayer,
createAudioResource,
getVoiceConnection,
joinVoiceChannel,
} = require("@discordjs/voice");
const RadioPlayer = require("./RadioPlayer.js");
async function play(interaction, sound) {
try {
const member = interaction.member;
const VC = member.voice.channel;
if(!VC){
return(
{
content: 'Join a voice channel and try again!',
flags: MessageFlags.Ephemeral
}
);
}
else{
var connection = joinVoiceChannel({
channelId: VC.id,
guildId: VC.guild.id,
adapterCreator: VC.guild.voiceAdapterCreator,
});
if(sound.startsWith('http')) {
var player = await RadioPlayer.getAudioPlayer(sound);
connection.subscribe(player);
}
else {
console.log('Playing local file');
var resource = createAudioResource(sound);
var player = createAudioPlayer();
//console.log('Playing ' + sound);
connection.subscribe(player);
player.play(resource);
}
const VC = member.voice.channel;
if (!VC) {
return {
content: "Join a voice channel and try again!",
flags: MessageFlags.Ephemeral,
};
} else {
var connection = joinVoiceChannel({
channelId: VC.id,
guildId: VC.guild.id,
adapterCreator: VC.guild.voiceAdapterCreator,
});
var player = null;
if (sound.startsWith("http")) {
player = await RadioPlayer.getAudioPlayer(sound);
connection.subscribe(player);
} else {
console.log("Playing local file");
var resource = createAudioResource(sound);
player = createAudioPlayer();
connection.subscribe(player);
player.play(resource);
}
} catch(error){
console.error(error);
return(
{
content: 'Something went wrong! Do I have permission to join the voice channel and speak?',
flags: MessageFlags.Ephemeral
}
);
}
return('Now speaking in voice channel!');
}
} catch (error) {
console.error(error);
return {
content:
"Something went wrong! Do I have permission to join the voice channel and speak?",
flags: MessageFlags.Ephemeral,
};
}
return "Now speaking in voice channel!";
}
function disconnect(interaction) {
const connection = getVoiceConnection(interaction.guildId);
if(connection) {
if (connection) {
connection.destroy();
return("Bye!");
}
else {
return(
{
content: 'Can\'t disconnect me because I\'m not in a voice channel!',
flags: MessageFlags.Ephemeral
}
);
return "Bye!";
} else {
return {
content: "Can't disconnect me because I'm not in a voice channel!",
flags: MessageFlags.Ephemeral,
};
}
}
module.exports = { play, disconnect };