Skip to content

Commit

Permalink
updated libs
Browse files Browse the repository at this point in the history
fixed internal bugs
lots of other things (see 0.0.9 release)
  • Loading branch information
jagrosh committed Sep 2, 2017
1 parent 6b454c2 commit 5deb41c
Show file tree
Hide file tree
Showing 16 changed files with 500 additions and 148 deletions.
6 changes: 3 additions & 3 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.8</version>
<version>0.0.9</version>
<packaging>jar</packaging>

<repositories>
Expand All @@ -23,7 +23,7 @@
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>3.2.0_242</version>
<version>3.2.0_258</version>
</dependency>
<dependency>
<groupId>com.sedmelluq</groupId>
Expand All @@ -33,7 +33,7 @@
<dependency>
<groupId>com.jagrosh</groupId>
<artifactId>JDA-Utilities</artifactId>
<version>1.5</version>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
Expand Down
75 changes: 65 additions & 10 deletions src/main/java/com/jagrosh/jmusicbot/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,21 @@
import com.jagrosh.jmusicbot.audio.AudioHandler;
import com.jagrosh.jmusicbot.gui.GUI;
import com.jagrosh.jmusicbot.utils.FormatUtil;
import java.util.Objects;
import javafx.util.Pair;
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.Message;
import net.dv8tion.jda.core.entities.Role;
import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.entities.VoiceChannel;
import net.dv8tion.jda.core.events.ReadyEvent;
import net.dv8tion.jda.core.events.ShutdownEvent;
import net.dv8tion.jda.core.events.guild.GuildJoinEvent;
import net.dv8tion.jda.core.events.message.MessageDeleteEvent;
import net.dv8tion.jda.core.exceptions.PermissionException;
import net.dv8tion.jda.core.hooks.ListenerAdapter;
import net.dv8tion.jda.core.utils.SimpleLog;
import org.json.JSONException;
Expand All @@ -55,6 +60,7 @@
public class Bot extends ListenerAdapter {

private final HashMap<String,Settings> settings;
private final HashMap<Long,Pair<Long,Long>> lastNP; // guild -> channel,message
private final AudioPlayerManager manager;
private final EventWaiter waiter;
private final ScheduledExecutorService threadpool;
Expand Down Expand Up @@ -91,6 +97,7 @@ public Bot(EventWaiter waiter, Config config)
this.config = config;
this.waiter = waiter;
this.settings = new HashMap<>();
this.lastNP = new HashMap<>();
manager = new DefaultAudioPlayerManager();
threadpool = Executors.newSingleThreadScheduledExecutor();
AudioSourceManagers.registerRemoteSources(manager);
Expand All @@ -112,6 +119,11 @@ public Bot(EventWaiter waiter, Config config)
}
}

public JDA getJDA()
{
return jda;
}

public EventWaiter getWaiter()
{
return waiter;
Expand Down Expand Up @@ -143,7 +155,8 @@ public AudioHandler setUpHandler(Guild guild)
handler = new AudioHandler(player, guild, this);
player.addListener(handler);
guild.getAudioManager().setSendingHandler(handler);
threadpool.scheduleWithFixedDelay(() -> updateTopic(guild,handler), 0, 5, TimeUnit.SECONDS);
if(AudioHandler.USE_NP_REFRESH)
threadpool.scheduleWithFixedDelay(() -> updateLastNP(guild.getIdLong()), 0, 5, TimeUnit.SECONDS);
}
else
handler = (AudioHandler)guild.getAudioManager().getSendingHandler();
Expand All @@ -152,27 +165,69 @@ public AudioHandler setUpHandler(Guild guild)

public void resetGame()
{
if(config.getGame()==null || config.getGame().equalsIgnoreCase("none"))
jda.getPresence().setGame(null);
else
jda.getPresence().setGame(Game.of(config.getGame()));
Game game = config.getGame()==null || config.getGame().equalsIgnoreCase("none") ? null : Game.of(config.getGame());
if(!Objects.equals(jda.getPresence().getGame(), game))
jda.getPresence().setGame(game);
}

public void setLastNP(Message m)
{
lastNP.put(m.getGuild().getIdLong(), new Pair<>(m.getTextChannel().getIdLong(), m.getIdLong()));
}

@Override
public void onMessageDelete(MessageDeleteEvent event) {
if(lastNP.containsKey(event.getGuild().getIdLong()))
{
Pair<Long,Long> pair = lastNP.get(event.getGuild().getIdLong());
if(pair.getValue()==event.getMessageIdLong())
lastNP.remove(event.getGuild().getIdLong());
}
}

private void updateTopic(Guild guild, AudioHandler handler)
private void updateLastNP(long guildId)
{
Guild guild = jda.getGuildById(guildId);
if(guild==null)
return;
if(!lastNP.containsKey(guildId))
return;
Pair<Long,Long> pair = lastNP.get(guildId);
if(pair==null)
return;
TextChannel tc = guild.getTextChannelById(pair.getKey());
if(tc==null)
{
lastNP.remove(guildId);
return;
}
try {
tc.editMessageById(pair.getValue(), FormatUtil.nowPlayingMessage(guild, config.getSuccess())).queue(m->{}, t -> lastNP.remove(guildId));
} catch(Exception e) {
lastNP.remove(guildId);
}
}

public void updateTopic(long guildId, AudioHandler handler)
{
Guild guild = jda.getGuildById(guildId);
if(guild==null)
return;
TextChannel tchan = guild.getTextChannelById(getSettings(guild).getTextId());
if(tchan!=null && guild.getSelfMember().hasPermission(tchan, Permission.MANAGE_CHANNEL))
{
String otherText;
if(tchan.getTopic()==null || tchan.getTopic().isEmpty())
otherText = "\u200B";
else if(tchan.getTopic().contains("\u200B"))
otherText = tchan.getTopic().substring(tchan.getTopic().indexOf("\u200B"));
otherText = tchan.getTopic().substring(tchan.getTopic().lastIndexOf("\u200B"));
else
otherText = "\u200B\n "+tchan.getTopic();
String text = FormatUtil.formattedAudio(handler, guild.getJDA())+otherText;
String text = FormatUtil.topicFormat(handler, guild.getJDA())+otherText;
if(!text.equals(tchan.getTopic()))
tchan.getManager().setTopic(text).queue();
try {
tchan.getManager().setTopic(text).queue();
} catch(PermissionException e){}
}
}

Expand All @@ -186,7 +241,7 @@ public void shutdown(){
{
ah.getQueue().clear();
ah.getPlayer().destroy();
updateTopic(g, ah);
updateTopic(g.getIdLong(), ah);
}
});
jda.shutdown();
Expand Down
46 changes: 41 additions & 5 deletions src/main/java/com/jagrosh/jmusicbot/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
*/
package com.jagrosh.jmusicbot;

import com.jagrosh.jmusicbot.utils.FormatUtil;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import javax.swing.JOptionPane;
import net.dv8tion.jda.core.OnlineStatus;

/**
*
Expand All @@ -42,13 +43,17 @@ public class Config {
private boolean stay;
private boolean dbots;
private boolean songingame;
private boolean useEval;
private boolean npimages;
private long maxSeconds;
private OnlineStatus status = OnlineStatus.UNKNOWN;

public Config(boolean nogui)
{
this.nogui = nogui;
List<String> lines;
try {
lines = Files.readAllLines(Paths.get("config.txt"));
lines = Files.readAllLines(Paths.get("config.txt"), Charset.forName("ISO-8859-1"));
System.out.println("[INFO] Loading config: "+Paths.get("config.txt").toFile().getAbsolutePath());
for(String line: lines)
{
Expand Down Expand Up @@ -84,18 +89,29 @@ public Config(boolean nogui)
case "help":
help = value;
break;
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;
case "eval":
useEval = "true".equalsIgnoreCase(value);
break;
case "dbots":
dbots = "110373943822540800".equals(value);
break;
case "npimages":
npimages = "true".equalsIgnoreCase(value);
break;
case "maxtime":
try{
maxSeconds = Long.parseLong(value);
}catch(NumberFormatException e){}
break;
case "status":
status = OnlineStatus.fromKey(value);
break;
}
}
} catch (IOException ex) {
Expand Down Expand Up @@ -216,6 +232,26 @@ public boolean getDBots()
return dbots;
}

public boolean useEval()
{
return useEval;
}

public boolean useNPImages()
{
return npimages;
}

public long getMaxSeconds()
{
return maxSeconds;
}

public OnlineStatus getStatus()
{
return status;
}

private void alert(String message)
{
if(nogui)
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/com/jagrosh/jmusicbot/JMusicBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,16 @@ public static void main(String[] args){
EventWaiter waiter = new EventWaiter();
Bot bot = new Bot(waiter, config);

AboutCommand.IS_AUTHOR = false;
AboutCommand.REPLACEMENT_ICON = "\uD83C\uDFB6";
AboutCommand ab = new AboutCommand(Color.BLUE.brighter(),
"a music bot that is [easy to host yourself!](https://github.com/jagrosh/MusicBot) (v0.0.9)",
new String[]{"High-quality music playback", "FairQueue™ Technology", "Easy to host yourself"},
RECOMMENDED_PERMS);
ab.setIsAuthor(false);
ab.setReplacementCharacter("\uD83C\uDFB6");
AudioHandler.STAY_IN_CHANNEL = config.getStay();
AudioHandler.SONG_IN_STATUS = config.getSongInStatus();
AudioHandler.MAX_SECONDS = config.getMaxSeconds();
AudioHandler.USE_NP_REFRESH = !config.useNPImages();

// set up the command client

Expand All @@ -72,10 +78,7 @@ public static void main(String[] args){
.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.8)",
new String[]{"High-quality music playback", "FairQueue™ Technology", "Easy to host yourself"},
RECOMMENDED_PERMS),
ab,
new PingCommand(),
new SettingsCmd(bot),

Expand All @@ -100,14 +103,19 @@ public static void main(String[] args){
new SetvcCmd(bot),

//new GuildlistCommand(waiter),
new AutoplaylistCmd(bot),
new PlaylistCmd(bot),
new SetavatarCmd(bot),
new SetgameCmd(bot),
new SetnameCmd(bot),
new SetstatusCmd(bot),
new ShutdownCmd(bot)
);
if(config.useEval())
cb.addCommand(new EvalCmd(bot));
boolean nogame = false;
if(config.getStatus()!=OnlineStatus.UNKNOWN)
cb.setStatus(config.getStatus());
if(config.getGame()==null)
cb.useDefaultGame();
else if(config.getGame().equalsIgnoreCase("none"))
Expand Down Expand Up @@ -138,7 +146,7 @@ else if(config.getGame().equalsIgnoreCase("none"))
.setToken(config.getToken())
.setAudioEnabled(true)
.setGame(nogame ? null : Game.of("loading..."))
.setStatus(OnlineStatus.DO_NOT_DISTURB)
.setStatus(config.getStatus()==OnlineStatus.INVISIBLE||config.getStatus()==OnlineStatus.OFFLINE ? OnlineStatus.INVISIBLE : OnlineStatus.DO_NOT_DISTURB)
.addEventListener(client)
.addEventListener(waiter)
.addEventListener(bot)
Expand Down
Loading

0 comments on commit 5deb41c

Please sign in to comment.