Skip to content

Commit

Permalink
Bugfix skip from last commit. Made queue output clearer. Install script.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmorley191 committed Mar 26, 2023
1 parent 2c6acff commit 57d28a1
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 32 deletions.
197 changes: 197 additions & 0 deletions install.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
import io
from multiprocessing.connection import wait
import os
import re
import requests
import shutil
import zipfile

hardcoded_version_tag = None

url_repo = "https://github.com/cmorley191/MusicBot"
url_releases = f"{url_repo}/releases"
url_latest = f"{url_releases}/latest"
url_release_tag = f"{url_releases}/tag"
url_download = f"{url_releases}/download"

def flatten(l):
return [item for sublist in l for item in sublist]

def waitForYesNo():
entry = input().lower()
if entry in ["y", "yes", "install"]:
return True
return False

def install(version_tag):
url_version = f"{url_release_tag}/{version_tag}"
print(f"Downloading {version_tag} from {url_version}")
resp_version = requests.get(f"{url_release_tag}/{version_tag}", allow_redirects=True)
assert resp_version.status_code == 200, f"Could not find release at {url_version}"

jar_filenames = [
f"jmusicbot-{version_tag.lower()}-all.jar",
f"jmusicbot-{version_tag.lower()}-snapshot-all.jar",
f"jmusicbot-{version_tag.lower()}.jar",
f"jmusicbot-all.jar",
f"jmusicbot-snapshot-all.jar",
]
jar_filenames = [
*[f"cmorley191-{x}" for x in jar_filenames],
*jar_filenames,
]

urls_version_download = [
f"{url_download}/{version_tag}/cmorley191_jmusicbot_{version_tag}.zip",
*[f"{url_download}/{version_tag}/{jar_filename}" for jar_filename in jar_filenames],
]
for url_version_download in urls_version_download:
resp_download = requests.get(url_version_download, allow_redirects=True)
if resp_download.status_code != 200:
continue
print(f"Downloaded {url_version_download}")

jar_content = resp_download.content
if (url_version_download.lower().endswith(".zip")):
zip_content = resp_download.content
with io.BytesIO(zip_content) as zip_content_io:
with zipfile.ZipFile(zip_content_io) as archive:
jar_content = archive.read("JMusicBot-Snapshot-All.jar")
print("Extracted zip.")

print(f"Enter install file or folder path: (input nothing to install at current working directory)")
path = input().strip()
if path == "":
path = os.getcwd()
if os.path.isfile(path) or os.path.isdir(path):
if os.path.isfile(path):
dirname = os.path.dirname(path)
else:
dirname = path

# check for config/serversettings files
path_config = os.path.join(dirname, "config.txt")
if os.path.isfile(path_config):
print(f"Backing up config.txt to config.txt.backup")
shutil.copy(path_config, f"{path_config}.backup")
else:
print(f"No config file found to backup.")
path_serversettings = os.path.join(dirname, "serversettings.json")
if os.path.isfile(path_serversettings):
print(f"Backing up serversettings.json to serversettings.json.backup")
shutil.copy(path_serversettings, f"{path_serversettings}.backup")
else:
print(f"No serversettings file found to backup.")

if os.path.isfile(path):
filename = os.path.basename(path)
file_exists = True
else:
files = os.listdir(path)
files_lower = [x.lower() for x in files]
for jar_filename in jar_filenames:
if jar_filename in files_lower:
filename = files[files_lower.index(jar_filename)]
file_exists = True
break
else:
print(f"Executable jar not found in folder {path}")
filename = jar_filenames[0]
file_exists = False
path = os.path.join(path, filename)
print(f"{'Overwrite' if file_exists else 'Install'} executable jar at {path} ? (y/n)")
if not waitForYesNo():
print("Aborting")
return

if filename.lower() not in jar_filenames:
print(f"Supplied path does not match a recognized cmorley191-JMusicBot.jar filename: {filename}")
print(f"Are you sure you want to install the executable jar at this filename? (y/n)")
if not waitForYesNo():
print("Aborting")
return

else:
print(f"Path does not exist: {path}")
if path.lower().endswith(".jar"):
print(f"Install executable jar at that filepath? (y/n)")
if not waitForYesNo():
print("Aborting")
return
os.makedirs(os.path.dirname(path))
else:
print(f"Create a new installation folder there? (y/n)")
if not waitForYesNo():
print("Aborting")
return
os.makedirs(path)
path = os.path.join(path, jar_filenames[0])

# path is now the executable filename to install at

print(f"Backing up {os.path.basename(path)} to {os.path.basename(path)}.backup")
shutil.copy(path, f"{path}.backup")

print("Ready to install? (y/n)")
if not waitForYesNo():
print("Aborting")
return
with open(path, 'wb') as f:
f.write(jar_content)
print(f"Executable jar installed successfully.")
break

else:
assert False, f"Could not find any automatically-installable release artifacts at {url_version}"


def main(hardcoded_version_tag):
print("Checking latest version...")
resp_latest = requests.get(url_latest, allow_redirects=True)
assert resp_latest.history, f"Could not check latest release version at {url_latest}"
resp_latest_tag = resp_latest
url_latest_tag = resp_latest.url
assert resp_latest_tag.status_code == 200, f"Could not check latest release version at {url_latest} -> {url_latest_tag}\n{resp_latest.history}"

latest_version_tag_match = re.fullmatch(rf'{re.escape(f"{url_release_tag}/")}(.*)', url_latest_tag)
assert latest_version_tag_match is not None and len(latest_version_tag_match.groups()) == 1, f"Could not find a release version tag at {url_latest} -> {url_latest_tag}"
latest_version_tag = latest_version_tag_match.group(1)
assert latest_version_tag.strip() != "", f"Could not find a valid release version tag at {url_latest} -> {url_latest_tag}"

if hardcoded_version_tag is None or latest_version_tag == hardcoded_version_tag:
print(f"Installer version is latest version: {latest_version_tag}")
else:
print(f"Installer version {hardcoded_version_tag} does not match latest version {latest_version_tag}")
print(f"What would you like to do? Options:")
if hardcoded_version_tag is not None and latest_version_tag != hardcoded_version_tag:
print(f"Install installer version {hardcoded_version_tag} (I)")
print(f"Install latest version {latest_version_tag} (L)")
print(f"Enter other version to install (O)")
print(f"Abort without installing (A)")
entry = input().lower()
if hardcoded_version_tag is not None and latest_version_tag != hardcoded_version_tag and (re.split(r'\s+', entry.lower()) in [
["i"],
*flatten([[*x, *y, *z] for x in [[], ["instal"], ["install"]] for y in [["installer"], ["instaler"]] for z in [[], ["version"]]]),
]):
install(hardcoded_version_tag)
elif (re.split(r'\s+', entry.lower()) in [
["l"],
*flatten([[*x, *y, *z] for x in [[], ["instal"], ["install"]] for y in [["latest"]] for z in [[], ["version"]]]),
]):
install(latest_version_tag)
elif (re.split(r'\s+', entry.lower()) in [
["o"], ["enter"], ["enter", "version"],
*flatten([[*x, *y, *z] for x in [[], ["enter"], ["install"], ["instal"]] for y in [["other"], ["another"]] for z in [[], ["version"]]]),
]):
print("Enter version to install:")
entry = input()
if entry in ['1', '1.0', '1.0.0', '1.1', '1.1.0', '1.2', '1.2.0', '1.3_beta1', '1.3.0_beta1']:
print(f"Entered version {entry} is not supported for automated install, sorry.")
else:
install(entry)
else:
print("Aborting")


if __name__ == '__main__':
main(hardcoded_version_tag)
34 changes: 20 additions & 14 deletions src/main/java/com/jagrosh/jmusicbot/audio/AudioHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ public FairQueue<QueuedTrack> getBackgroundQueue()
return backgroundQueue;
}

public AudioTrack getLatestActiveBackgroundTrack() {
return latestActiveBackgroundTrack;
}

public boolean playingFromBackgroundQueue()
{
return audioPlayer.getPlayingTrack() != null && latestActiveBackgroundTrack != null && audioPlayer.getPlayingTrack() == latestActiveBackgroundTrack;
Expand Down Expand Up @@ -225,22 +229,24 @@ public void onTrackEnd(AudioPlayer player, AudioTrack track, AudioTrackEndReason
// note that REPLACED is *also* used when the track is skipped, but skipTrack() makes sure we don't keep it around in that case.
}

if(queue.isEmpty() && endReason!=AudioTrackEndReason.REPLACED)
{
if(!playFromBackground() && !playFromDefault())
if (endReason!=AudioTrackEndReason.REPLACED) {
if(queue.isEmpty())
{
manager.getBot().getNowplayingHandler().onTrackUpdate(guildId, null, this);
if(!manager.getBot().getConfig().getStay())
manager.getBot().closeAudioConnection(guildId);
// unpause, in the case when the player was paused and the track has been skipped.
// this is to prevent the player being paused next time it's being used.
player.setPaused(false);
if(!playFromBackground() && !playFromDefault())
{
manager.getBot().getNowplayingHandler().onTrackUpdate(guildId, null, this);
if(!manager.getBot().getConfig().getStay())
manager.getBot().closeAudioConnection(guildId);
// unpause, in the case when the player was paused and the track has been skipped.
// this is to prevent the player being paused next time it's being used.
player.setPaused(false);
}
}
else
{
QueuedTrack qt = queue.pull();
player.playTrack(qt.getTrack());
}
}
else if (endReason.mayStartNext)
{
QueuedTrack qt = queue.pull();
player.playTrack(qt.getTrack());
}
}

Expand Down
51 changes: 33 additions & 18 deletions src/main/java/com/jagrosh/jmusicbot/commands/music/QueueCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,11 @@ public void doCommand(CommandEvent event)
// TEMPORARY -- can only show one queue right now, because JDA-Utilities paginator has bug preventing multiple paginators
AudioHandler ah = (AudioHandler)event.getGuild().getAudioManager().getSendingHandler();
if (ah.playingFromBackgroundQueue() || (ah.getQueue().isEmpty() && !ah.getBackgroundQueue().isEmpty())) {
showQueue(event, false, false);
showQueue(event, true, true);
} else {
showQueue(event, false, true);
if (!ah.getBackgroundQueue().isEmpty() || ah.getQueue().isEmpty()) {
showQueue(event, true, false);
}
showQueue(event, true, false);
}
}

Expand All @@ -89,18 +88,23 @@ private void showQueue(CommandEvent event, boolean backgroundQueue, boolean pagi
AudioHandler ah = (AudioHandler)event.getGuild().getAudioManager().getSendingHandler();
List<QueuedTrack> list = (backgroundQueue) ? ah.getBackgroundQueue().getList() : ah.getQueue().getList();
String queueName = (backgroundQueue) ? "the background queue" : "the queue";
if(list.isEmpty() && (backgroundQueue == ah.playingFromBackgroundQueue()))
if(list.isEmpty())
{
Message nowp = ah.getNowPlaying(event.getJDA());
Message nonowp = ah.getNoMusicPlaying(event.getJDA());
Message built = new MessageBuilder()
.setContent(bot.getWarning(event) + " There is no music in "+queueName+"!")
.setEmbeds((nowp==null ? nonowp : nowp).getEmbeds().get(0)).build();
event.reply(built, m ->
{
if(nowp!=null)
bot.getNowplayingHandler().setLastNPMessage(m);
});
String content = bot.getWarning(event) + " There is no music in "+queueName+"!";
if (backgroundQueue == !ah.playingFromBackgroundQueue()) {
event.reply(content);
} else {
Message nowp = ah.getNowPlaying(event.getJDA());
Message nonowp = ah.getNoMusicPlaying(event.getJDA());
Message built = new MessageBuilder()
.setContent(content)
.setEmbeds((nowp==null ? nonowp : nowp).getEmbeds().get(0)).build();
event.reply(built, m ->
{
if(nowp!=null)
bot.getNowplayingHandler().setLastNPMessage(m);
});
}
return;
}
String[] songs = new String[list.size()];
Expand Down Expand Up @@ -130,11 +134,22 @@ private void showQueue(CommandEvent event, boolean backgroundQueue, boolean pagi
private String getQueueTitle(AudioHandler ah, boolean backgroundQueue, String success, int songslength, long total, RepeatMode repeatmode)
{
StringBuilder sb = new StringBuilder();
if(ah.getPlayer().getPlayingTrack()!=null && (backgroundQueue == ah.playingFromBackgroundQueue()))
boolean playingTrackFromQueue = ah.getPlayer().getPlayingTrack()!=null && (backgroundQueue == ah.playingFromBackgroundQueue());
boolean backgroundQueuePaused = backgroundQueue && ah.getLatestActiveBackgroundTrack() != null;
if(playingTrackFromQueue || backgroundQueuePaused)
{
sb.append(ah.getStatusEmoji()).append(" **")
.append(ah.getPlayer().getPlayingTrack().getInfo().title).append("**\n");
}
sb.append(playingTrackFromQueue ? ah.getStatusEmoji() : AudioHandler.PAUSE_EMOJI);
if (backgroundQueue) sb.append("BG:");

AudioTrack trackPlaying = (playingTrackFromQueue) ? ah.getPlayer().getPlayingTrack() : ah.getLatestActiveBackgroundTrack();
sb.append(" **")
.append(trackPlaying.getInfo().title)
.append("**`[")
.append(SeekCmd.timeMsToFriendlyOutputTimestampString(trackPlaying.getPosition() / 1000 * 1000)) // trim milliseconds
.append("/")
.append(SeekCmd.timeMsToFriendlyOutputTimestampString(trackPlaying.getDuration() / 1000 * 1000))
.append("]`\n");
}
return FormatUtil.filter(sb.append(success).append(" Current "+(backgroundQueue ? "Background Queue" : "Queue")+" | ").append(songslength)
.append(" entries | `").append(FormatUtil.formatTime(total)).append("` ")
.append(repeatmode.getEmoji() != null ? "| "+repeatmode.getEmoji() : "").toString());
Expand Down

0 comments on commit 57d28a1

Please sign in to comment.