Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically show "now playing" when a new track starts #1010

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/main/java/com/jagrosh/jmusicbot/BotConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class BotConfig
private Path path = null;
private String token, prefix, altprefix, helpWord, playlistsFolder,
successEmoji, warningEmoji, errorEmoji, loadingEmoji, searchingEmoji;
private boolean stayInChannel, songInGame, npImages, updatealerts, useEval, dbots;
private boolean stayInChannel, songInGame, npImages, updatealerts, useEval, dbots, autoNowPlaying;
private long owner, maxSeconds, aloneTimeUntilStop;
private OnlineStatus status;
private Activity game;
Expand Down Expand Up @@ -90,6 +90,7 @@ public void load()
status = OtherUtil.parseStatus(config.getString("status"));
stayInChannel = config.getBoolean("stayinchannel");
songInGame = config.getBoolean("songinstatus");
autoNowPlaying = config.getBoolean("autonowplaying");
npImages = config.getBoolean("npimages");
updatealerts = config.getBoolean("updatealerts");
useEval = config.getBoolean("eval");
Expand Down Expand Up @@ -266,6 +267,11 @@ public boolean getSongInStatus()
return songInGame;
}

public boolean getAutoNowPlaying()
{
return autoNowPlaying;
}

public String getPlaylistsFolder()
{
return playlistsFolder;
Expand Down
23 changes: 17 additions & 6 deletions src/main/java/com/jagrosh/jmusicbot/audio/AudioHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public void onTrackStart(AudioPlayer player, AudioTrack track)


// Formatting
public Message getNowPlaying(JDA jda)
public Message getNowPlaying(JDA jda, boolean includeProgress)
{
if(isMusicPlaying(jda))
{
Expand Down Expand Up @@ -232,17 +232,28 @@ public Message getNowPlaying(JDA jda)
if(track.getInfo().author != null && !track.getInfo().author.isEmpty())
eb.setFooter("Source: " + track.getInfo().author, null);

double progress = (double)audioPlayer.getPlayingTrack().getPosition()/track.getDuration();
eb.setDescription((audioPlayer.isPaused() ? JMusicBot.PAUSE_EMOJI : JMusicBot.PLAY_EMOJI)
+ " "+FormatUtil.progressBar(progress)
+ " `[" + FormatUtil.formatTime(track.getPosition()) + "/" + FormatUtil.formatTime(track.getDuration()) + "]` "
+ FormatUtil.volumeIcon(audioPlayer.getVolume()));
if(includeProgress)
{
double progress = (double)audioPlayer.getPlayingTrack().getPosition()/track.getDuration();
eb.setDescription((audioPlayer.isPaused() ? JMusicBot.PAUSE_EMOJI : JMusicBot.PLAY_EMOJI)
+ " "+FormatUtil.progressBar(progress)
+ " `[" + FormatUtil.formatTime(track.getPosition()) + "/" + FormatUtil.formatTime(track.getDuration()) + "]` "
+ FormatUtil.volumeIcon(audioPlayer.getVolume()));
}
else
eb.setDescription(" `[" + FormatUtil.formatTime(track.getDuration()) + "]` "
+ FormatUtil.volumeIcon(audioPlayer.getVolume()));

return mb.setEmbed(eb.build()).build();
}
else return null;
}

public Message getNowPlaying(JDA jda)
{
return getNowPlaying(jda, true);
}

public Message getNoMusicPlaying(JDA jda)
{
Guild guild = guild(jda);
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/jagrosh/jmusicbot/audio/NowplayingHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,28 @@ else if(topic.contains("\u200B"))
}
}

public void sendNowPlayingMessageForNewTrack(long guildId)
{
Guild guild = bot.getJDA().getGuildById(guildId);
if(guild==null)
return;

Settings settings = bot.getSettingsManager().getSettings(guildId);
TextChannel tchan = settings.getTextChannel(guild);
if (tchan == null)
return;

AudioHandler handler = (AudioHandler)guild.getAudioManager().getSendingHandler();
// Because useNPImages prevents now playing messages from automatically updating,
// there's no point in including the track progress because it will always be
// right at the beginning. Instead show a message just containing the track length:
Message msg = handler.getNowPlaying(bot.getJDA(), !bot.getConfig().useNPImages());
if(msg==null)
return;

tchan.sendMessage(msg).queue((m) -> setLastNPMessage(m));
}

// "event"-based methods
public void onTrackUpdate(long guildId, AudioTrack track, AudioHandler handler)
{
Expand All @@ -146,6 +168,9 @@ public void onTrackUpdate(long guildId, AudioTrack track, AudioHandler handler)

// update channel topic if applicable
updateTopic(guildId, handler, false);

if(bot.getConfig().getAutoNowPlaying())
sendNowPlayingMessageForNewTrack(guildId);
}

public void onMessageDelete(Guild guild, long messageId)
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ npimages = false
stayinchannel = false


// If you set this, the bot will automatically send a message displaying the current
// track and which user queued it when a track starts playing for guilds that have
// set a text channel for music commands.

autonowplaying = false


// This sets the maximum amount of seconds any track loaded can be. If not set or set
// to any number less than or equal to zero, there is no maximum time length. This time
// restriction applies to songs loaded from any source.
Expand Down