Skip to content

Commit

Permalink
added altprefix, songinstatus, and setstatus features
Browse files Browse the repository at this point in the history
slight change to internals to reduce some bugs
  • Loading branch information
jagrosh committed Aug 18, 2017
1 parent 0912de7 commit dddf4c8
Show file tree
Hide file tree
Showing 19 changed files with 394 additions and 327 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.jagrosh</groupId>
<artifactId>JMusicBot</artifactId>
<version>0.0.7</version>
<version>0.0.8</version>
<packaging>jar</packaging>

<repositories>
Expand All @@ -23,17 +23,17 @@
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>3.1.1_219</version>
<version>3.2.0_242</version>
</dependency>
<dependency>
<groupId>com.sedmelluq</groupId>
<artifactId>lavaplayer</artifactId>
<version>1.2.42</version>
<version>1.2.43</version>
</dependency>
<dependency>
<groupId>com.jagrosh</groupId>
<artifactId>JDA-Utilities</artifactId>
<version>1.2</version>
<version>1.5</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/jagrosh/jmusicbot/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.jagrosh.jmusicbot.utils.FormatUtil;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.Game;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.Role;
import net.dv8tion.jda.core.entities.TextChannel;
Expand Down Expand Up @@ -149,6 +150,14 @@ public AudioHandler setUpHandler(Guild guild)
return handler;
}

public void resetGame()
{
if(config.getGame()==null || config.getGame().equalsIgnoreCase("none"))
jda.getPresence().setGame(null);
else
jda.getPresence().setGame(Game.of(config.getGame()));
}

private void updateTopic(Guild guild, AudioHandler handler)
{
TextChannel tchan = guild.getTextChannelById(getSettings(guild).getTextId());
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/jagrosh/jmusicbot/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
public class Config {
private boolean nogui;
private String prefix;
private String altprefix;
private String token;
private String owner;
private String success;
Expand All @@ -40,6 +41,7 @@ public class Config {
private String help;
private boolean stay;
private boolean dbots;
private boolean songingame;

public Config(boolean nogui)
{
Expand All @@ -61,6 +63,9 @@ public Config(boolean nogui)
case "prefix":
prefix = value;
break;
case "altprefix":
altprefix = value;
break;
case "owner":
owner = value;
break;
Expand All @@ -82,6 +87,9 @@ public Config(boolean nogui)
case "noprogressintopic":
FormatUtil.NO_PROGRESS_BAR_IN_TOPIC = "true".equalsIgnoreCase(value);
break;
case "songinstatus":
songingame = "true".equalsIgnoreCase(value);
break;
case "stayinchannel":
stay = "true".equalsIgnoreCase(value);
break;
Expand Down Expand Up @@ -148,6 +156,11 @@ public String getPrefix()
return prefix;
}

public String getAltPrefix()
{
return altprefix;
}

public String getToken()
{
return token;
Expand Down Expand Up @@ -193,6 +206,11 @@ public boolean getStay()
return stay;
}

public boolean getSongInStatus()
{
return songingame;
}

public boolean getDBots()
{
return dbots;
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/jagrosh/jmusicbot/JMusicBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,21 @@ public static void main(String[] args){
Bot bot = new Bot(waiter, config);

AboutCommand.IS_AUTHOR = false;
AboutCommand.REPLACEMENT_ICON = "\uD83C\uDFB6";
AudioHandler.STAY_IN_CHANNEL = config.getStay();
AudioHandler.SONG_IN_STATUS = config.getSongInStatus();

// set up the command client

CommandClientBuilder cb = new CommandClientBuilder()
.setPrefix(config.getPrefix())
.setAlternativePrefix(config.getAltPrefix())
.setOwnerId(config.getOwnerId())
.setEmojis(config.getSuccess(), config.getWarning(), config.getError())
.setHelpWord(config.getHelp())
.addCommands(
new AboutCommand(Color.BLUE.brighter(),
"a music bot that is [easy to host yourself!](https://github.com/jagrosh/MusicBot) (v0.0.7)",
"a music bot that is [easy to host yourself!](https://github.com/jagrosh/MusicBot) (v0.0.8)",
new String[]{"High-quality music playback", "FairQueue™ Technology", "Easy to host yourself"},
RECOMMENDED_PERMS),
new PingCommand(),
Expand Down Expand Up @@ -101,10 +104,13 @@ public static void main(String[] args){
new SetavatarCmd(bot),
new SetgameCmd(bot),
new SetnameCmd(bot),
new SetstatusCmd(bot),
new ShutdownCmd(bot)
);
if(config.getGame()==null)
cb.useDefaultGame();
else if(config.getGame().equalsIgnoreCase("none"))
cb.setGame(null);
else
cb.setGame(Game.of(config.getGame()));
CommandClient client = cb.build();
Expand Down
54 changes: 25 additions & 29 deletions src/main/java/com/jagrosh/jmusicbot/audio/AudioHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.jagrosh.jmusicbot.playlist.Playlist;
import com.jagrosh.jmusicbot.queue.FairQueue;
import net.dv8tion.jda.core.audio.AudioSendHandler;
import net.dv8tion.jda.core.entities.Game;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.User;

Expand All @@ -43,8 +44,9 @@ public class AudioHandler extends AudioEventAdapter implements AudioSendHandler
private final List<AudioTrack> defaultQueue;
private final Bot bot;
private AudioFrame lastFrame;
private QueuedTrack current;
private long requester;
public static boolean STAY_IN_CHANNEL;
public static boolean SONG_IN_STATUS;

public AudioHandler(AudioPlayer audioPlayer, Guild guild, Bot bot) {
this.audioPlayer = audioPlayer;
Expand All @@ -57,22 +59,14 @@ public AudioHandler(AudioPlayer audioPlayer, Guild guild, Bot bot) {

public int addTrack(AudioTrack track, User user)
{
QueuedTrack qt = new QueuedTrack(track, user.getId());
if(current==null)
if(requester==0)
{
current = qt;
requester = user.getIdLong();
audioPlayer.playTrack(track);
return -1;
}
else
{
return queue.add(qt);
}
}

public QueuedTrack getCurrentTrack()
{
return current;
return queue.add(new QueuedTrack(track, user.getIdLong()));
}

public FairQueue<QueuedTrack> getQueue()
Expand All @@ -90,7 +84,7 @@ public void stopAndClear()

public boolean isMusicPlaying()
{
return guild.getSelfMember().getVoiceState().inVoiceChannel() && current!=null;
return guild.getSelfMember().getVoiceState().inVoiceChannel() && audioPlayer.getPlayingTrack()!=null;
}

public Set<String> getVotes()
Expand All @@ -103,12 +97,16 @@ public AudioPlayer getPlayer()
return audioPlayer;
}

public long getRequester()
{
return requester;
}

public boolean playFromDefault()
{
if(!defaultQueue.isEmpty())
{
current = new QueuedTrack(defaultQueue.remove(0), null);
audioPlayer.playTrack(current.getTrack());
audioPlayer.playTrack(defaultQueue.remove(0));
return true;
}
if(bot.getSettings(guild)==null || bot.getSettings(guild).getDefaultPlaylist()==null)
Expand All @@ -117,46 +115,44 @@ public boolean playFromDefault()
if(pl==null || pl.getItems().isEmpty())
return false;
pl.loadTracks(bot.getAudioManager(), (at) -> {
if(current==null)
{
current = new QueuedTrack(at, null);
if(audioPlayer.getPlayingTrack()==null)
audioPlayer.playTrack(at);
}
else
defaultQueue.add(at);
}, () -> {
if(pl.getTracks().isEmpty())
{
current = null;
if(!STAY_IN_CHANNEL)
guild.getAudioManager().closeAudioConnection();
}
if(pl.getTracks().isEmpty() && !STAY_IN_CHANNEL)
guild.getAudioManager().closeAudioConnection();
});
return true;
}

@Override
public void onTrackEnd(AudioPlayer player, AudioTrack track, AudioTrackEndReason endReason) {
requester = 0;
if(queue.isEmpty())
{
current = null;
if(!playFromDefault())
{
current = null;
bot.resetGame();
if(!STAY_IN_CHANNEL)
guild.getAudioManager().closeAudioConnection();
}
}
else
{
current = queue.pull();
player.playTrack(current.getTrack());
QueuedTrack qt = queue.pull();
requester = qt.getIdentifier();
player.playTrack(qt.getTrack());
}
}

@Override
public void onTrackStart(AudioPlayer player, AudioTrack track) {
votes.clear();
if(SONG_IN_STATUS && guild.getJDA().getGuilds().size()==1)
{
guild.getJDA().getPresence().setGame(Game.of(track.getInfo().title));
}
}

@Override
Expand Down
104 changes: 52 additions & 52 deletions src/main/java/com/jagrosh/jmusicbot/audio/QueuedTrack.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
/*
* Copyright 2016 John Grosh <[email protected]>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jagrosh.jmusicbot.audio;

import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import com.jagrosh.jmusicbot.queue.Queueable;
import com.jagrosh.jmusicbot.utils.FormatUtil;

/**
*
* @author John Grosh <[email protected]>
*/
public class QueuedTrack implements Queueable {

private final AudioTrack track;
private final String owner;

public QueuedTrack(AudioTrack track, String owner)
{
this.track = track;
this.owner = owner;
}

@Override
public String getIdentifier() {
return owner;
}

public AudioTrack getTrack()
{
return track;
}

@Override
public String toString() {
return "`["+FormatUtil.formatTime(track.getDuration())+"]` **" + track.getInfo().title +"** - <@"+owner+">";
}

}
/*
* Copyright 2016 John Grosh <[email protected]>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jagrosh.jmusicbot.audio;

import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import com.jagrosh.jmusicbot.queue.Queueable;
import com.jagrosh.jmusicbot.utils.FormatUtil;

/**
*
* @author John Grosh <[email protected]>
*/
public class QueuedTrack implements Queueable {

private final AudioTrack track;
private final long owner;

public QueuedTrack(AudioTrack track, long owner)
{
this.track = track;
this.owner = owner;
}

@Override
public long getIdentifier() {
return owner;
}

public AudioTrack getTrack()
{
return track;
}

@Override
public String toString() {
return "`["+FormatUtil.formatTime(track.getDuration())+"]` **" + track.getInfo().title +"** - <@"+owner+">";
}

}
Loading

0 comments on commit dddf4c8

Please sign in to comment.