Skip to content

Commit

Permalink
Update 0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Bleuzen committed Jun 7, 2017
1 parent 6a9172f commit 29b9444
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 42 deletions.
8 changes: 4 additions & 4 deletions dist/aur/PKGBUILD
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# Maintainer: Bleuzen <supgesu at gmail dot com>

pkgname=blizcord
pkgver=0.3.0.1
pkgrel=2
pkgdesc="A simple Discord bot playing music from YouTube or local files"
pkgver=0.3.1
pkgrel=1
pkgdesc="A simple Discord bot which can play music from YouTube or local files"
arch=('any')
url="https://github.com/Bleuzen/Blizcord/"
license=('mit')
depends=('jre8-openjdk')
install=$pkgname.install
source=("blizcord-${pkgver}.deb::https://github.com/Bleuzen/Blizcord/releases/download/${pkgver}/blizcord-${pkgver}.deb")
md5sums=('276bba4a04251f94445f5a87740a0a10')
md5sums=('20040b567d3f96d2978d6d1e0fbdee3d')

package() {
msg2 "Extracting the data.tar.xz..."
Expand Down
2 changes: 1 addition & 1 deletion dist/deb/buildDeb.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 1. Place Blizcord in opt/Blizcord

# 2. Change the version
VERSION="0.3.0.1"
VERSION="0.3.1"

sudo sh -c "echo \"Package: blizcord
Version: $VERSION
Expand Down
30 changes: 20 additions & 10 deletions src/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import net.dv8tion.jda.core.entities.User;
import net.dv8tion.jda.core.entities.VoiceChannel;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import net.dv8tion.jda.core.exceptions.PermissionException;
import net.dv8tion.jda.core.hooks.ListenerAdapter;
import net.dv8tion.jda.core.managers.GuildController;

Expand Down Expand Up @@ -88,7 +89,7 @@ public static void start() {
Log.print("Created control channel.");
}
if(guild.getVoiceChannelsByName(Config.get(Config.VOICE_CHANNEL), true).isEmpty()) {
controller.createVoiceChannel(Config.get(Config.VOICE_CHANNEL)).setBitrate(Values.DISCORD_MAX_BITRATE).complete();
controller.createVoiceChannel(Config.get(Config.VOICE_CHANNEL)).complete();
Log.print("Created music channel.");
}
} catch(Exception e) {
Expand Down Expand Up @@ -136,7 +137,11 @@ public static void start() {
a.errExit(e.getMessage());
}

controlChannel.sendMessage(Values.BOT_NAME + " v" + Values.BOT_VERSION + " started.\nType ``" + Config.get(Config.COMMAND_PREFIX) + "help`` to see all commands.").queue();
try {
controlChannel.sendMessage(Values.BOT_NAME + " v" + Values.BOT_VERSION + " started.\nType ``" + Config.get(Config.COMMAND_PREFIX) + "help`` to see all commands.").queue();
} catch (PermissionException e) {
sendToOwner("Please give me the permision to read and write in your control channel: " + controlChannel.getName());
}
}

static void shutdown() {
Expand All @@ -153,10 +158,6 @@ private static void join() {
// for multi server: GuildMessageReceivedEvent#event.getGuild()
guild.getAudioManager().openAudioConnection(channel);
joined = true;

if(channel.getBitrate() != Values.DISCORD_MAX_BITRATE) {
controlChannel.sendMessage(guild.getOwner().getAsMention() + " Hint: You should set your channel's bitrate to " + (Values.DISCORD_MAX_BITRATE / 1000) + "kbps (highest) if you want to listen to music.").queue();
}
} catch(Exception e) {
controlChannel.sendMessage("Failed to join voice channel: " + cName + "\n"
+ "Please check your config and give me the permission to join it.").queue();
Expand Down Expand Up @@ -202,7 +203,7 @@ public void onMessageReceived(MessageReceivedEvent event) {
+ "!load <name> (Load a saved playlist)\n"
+ "!pause (Pause or resume the current track)\n"
+ "!skip (<how many songs>) (Skip one or more songs from the playlist)\n"
+ "!goto <hours:minutes:seconds> (Go to a given time)\n"
+ "!seek <hours:minutes:seconds> (Seek to the specified position)\n"
+ "!jump (<how many seconds>) (Jump forward in the current track)\n"
+ "!repeat (<how many times>) (Repeat the current playlist)\n"
+ "!stop (Stop the playback and clear the playlist)\n"
Expand Down Expand Up @@ -256,7 +257,7 @@ public void onMessageReceived(MessageReceivedEvent event) {
break;


case "goto":
case "seek":
if(!isAdmin(author)) {
channel.sendMessage(author.getAsMention() + " ``Only admins can use this command.``").queue();
return;
Expand Down Expand Up @@ -307,6 +308,11 @@ public void onMessageReceived(MessageReceivedEvent event) {
return;
}

if(!PlayerThread.isPlaying()) {
channel.sendMessage(author.getAsMention() + " ``Currently I'm not playing.``").queue();
return;
}

int seconds;
if(arg == null) {
seconds = 10;
Expand Down Expand Up @@ -593,8 +599,12 @@ static void setGame(Game game) {
}

static void sendUpdateMessage() {
guild.getOwner().getUser().openPrivateChannel().complete().sendMessage("A new version is available!\n"
+ "https://github.com/" + Values.BOT_GITHUB_REPO + "/releases").queue();
sendToOwner("A new version is available!\n"
+ "https://github.com/" + Values.BOT_GITHUB_REPO + "/releases");
}

private static void sendToOwner(String msg) {
guild.getOwner().getUser().openPrivateChannel().complete().sendMessage(msg).queue();
}

static String getTrackName(AudioTrack track) {
Expand Down
28 changes: 22 additions & 6 deletions src/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Config {
static final String DISPLAY_SONG_AS_GAME = "DISPLAY_SONG_AS_GAME";
static final String UPDATE_CHECK_INTERVAL_HOURS = "UPDATE_CHECK_INTERVAL_HOURS";
static final String ADMINS_ROLE = "ADMINS_ROLE";
static final String VOLUME = "VOLUME";

private static JSONObject defaults;
private static boolean initialized;
Expand All @@ -24,6 +25,10 @@ public class Config {
private static JSONObject json;

static boolean init(File configFile) {
return init(configFile, false);
}

static boolean init(File configFile, boolean fromConfigGUI) { // don't crash after generation if fromConfigGUI
if(initialized) {
return true;
}
Expand All @@ -38,6 +43,7 @@ static boolean init(File configFile) {
defaults.put(VOICE_CHANNEL, "Music");
defaults.put(DISPLAY_SONG_AS_GAME, "true");
defaults.put(UPDATE_CHECK_INTERVAL_HOURS, "24 #set to 0 to disable");
defaults.put(VOLUME, "100");

JSONObject read;
try {
Expand All @@ -60,19 +66,25 @@ static boolean init(File configFile) {
}
}

json = read;

if(toAdd == null) {
json = read;
initialized = true;
return true;
} else {
json = new JSONObject();
if(generate(toAdd)) {
a.errExit("Config file got generated or updated. Please edit it now.");
String gotGeneratedOrUpdatedMSG = "Config file got generated or updated.";
if(fromConfigGUI) {
GUI.showMsgBox(gotGeneratedOrUpdatedMSG);
initialized = true;
} else {
a.errExit(gotGeneratedOrUpdatedMSG + " Please edit it now.");
}
} else {
a.errExit("Failed to generate config. (Do you have write access here?)");
}
return false; // will never get called, but Eclipse wants it
}

return initialized;
}

static String get(String key) {
Expand All @@ -83,9 +95,13 @@ static void set(String key, String value) {
json.put(key, toValue(value));
}

private static void setRaw(String key, String value) {
json.put(key, value);
}

private static boolean generate(ArrayList<String> toAdd) {
for(String key : toAdd) {
set(key, defaults.getString(key));
setRaw(key, defaults.getString(key));
}

//TODO: save in other order if possible (BOT_TOKEN at the top)
Expand Down
4 changes: 4 additions & 0 deletions src/GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ static void onErrExit(String msg) {
showErrMsgBox("Bot crashed." + (msg == null ? "" : (" Reason:" + System.lineSeparator() + msg)));
}

static void showMsgBox(String msg) {
JOptionPane.showMessageDialog(null, msg, Values.BOT_NAME, JOptionPane.INFORMATION_MESSAGE);
}

private static void showErrMsgBox(String msg) {
JOptionPane.showMessageDialog(null, msg, Values.BOT_NAME, JOptionPane.ERROR_MESSAGE);
}
Expand Down
72 changes: 59 additions & 13 deletions src/GUI_Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,22 @@ public class GUI_Config extends JFrame {
private final JSpinner update_check_interval_hours_spinner;
private final JLabel lblIntervalInHours;
private final JButton btnGet;
private final JCheckBox chckbxAdminsRole;
private final JCheckBox chckbxCustomVolume;
private final JSpinner spinnerVolume;

public GUI_Config(File config) {
configFile = config;

setTitle(configFile.getName());
setResizable(false);
setSize(360, 320);
setSize(360, 330);
setLocationRelativeTo(null);

getContentPane().setLayout(null);

JPanel panel = new JPanel();
panel.setBounds(0, 4, 348, 228);
panel.setBounds(0, 4, 348, 244);
getContentPane().add(panel);
panel.setLayout(null);

Expand Down Expand Up @@ -84,10 +87,6 @@ public void keyPressed(KeyEvent e) {
voicechannel.setBounds(140, 92, 206, 26);
panel.add(voicechannel);

JLabel adminsrolelable = new JLabel("Admins role:");
adminsrolelable.setBounds(10, 120, 120, 26);
panel.add(adminsrolelable);

adminsrole = new JTextField();
adminsrole.setBounds(140, 120, 206, 26);
panel.add(adminsrole);
Expand All @@ -109,7 +108,7 @@ public void actionPerformed(ActionEvent e) {
update_check_interval_hours_spinner = new JSpinner();
update_check_interval_hours_spinner.setEnabled(false);
update_check_interval_hours_spinner.setModel(new SpinnerNumberModel(24, 1, null, 1));
update_check_interval_hours_spinner.setBounds(274, 176, 66, 26);
update_check_interval_hours_spinner.setBounds(276, 176, 64, 26);
panel.add(update_check_interval_hours_spinner);

lblIntervalInHours = new JLabel("Interval in hours:");
Expand All @@ -121,6 +120,7 @@ public void actionPerformed(ActionEvent e) {
panel.add(controlchannel);

btnGet = new JButton("Get");
btnGet.setToolTipText(Values.DISCORD_GET_TOKEN);
btnGet.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Expand All @@ -137,6 +137,37 @@ public void actionPerformed(ActionEvent e) {
btnGet.setBounds(266, 8, 80, 26);
panel.add(btnGet);

chckbxAdminsRole = new JCheckBox("Admins role");
chckbxAdminsRole.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(chckbxAdminsRole.isSelected()) {
adminsrole.setEnabled(true);
} else {
adminsrole.setEnabled(false);
adminsrole.setText("");
}
}
});
chckbxAdminsRole.setBounds(10, 120, 120, 26);
panel.add(chckbxAdminsRole);

chckbxCustomVolume = new JCheckBox("Custom Volume");
chckbxCustomVolume.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
spinnerVolume.setEnabled(chckbxCustomVolume.isSelected());
}
});
chckbxCustomVolume.setBounds(10, 204, 240, 26);
panel.add(chckbxCustomVolume);

spinnerVolume = new JSpinner();
spinnerVolume.setEnabled(false);
spinnerVolume.setModel(new SpinnerNumberModel(100, 5, 100, 5));
spinnerVolume.setBounds(276, 204, 64, 26);
panel.add(spinnerVolume);

JButton btnApply = new JButton("Apply");
btnApply.addActionListener(new ActionListener() {
@Override
Expand All @@ -150,6 +181,7 @@ public void actionPerformed(ActionEvent arg0) {
Config.set(Config.ADMINS_ROLE, adminsrole.getText());
Config.set(Config.DISPLAY_SONG_AS_GAME, String.valueOf(display_song_as_game.isSelected()));
Config.set(Config.UPDATE_CHECK_INTERVAL_HOURS, (update_check_box.isSelected() ? update_check_interval_hours_spinner.getValue().toString() : "0"));
Config.set(Config.VOLUME, (chckbxCustomVolume.isSelected() ? spinnerVolume.getValue().toString() : "100"));

if(Config.save()) {
JOptionPane.showMessageDialog(null, "Config saved.", Values.BOT_NAME, JOptionPane.INFORMATION_MESSAGE);
Expand All @@ -161,7 +193,7 @@ public void actionPerformed(ActionEvent arg0) {
GUI.mvToFront();
}
});
btnApply.setBounds(258, 254, 90, 26);
btnApply.setBounds(250, 258, 90, 26);
getContentPane().add(btnApply);

setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
Expand All @@ -173,19 +205,25 @@ public void actionPerformed(ActionEvent arg0) {
}

private void read() {
Config.init(configFile);
Config.init(configFile, true);

adminsrole.setText(Config.get(Config.ADMINS_ROLE));
commandprefix.setText(Config.get(Config.COMMAND_PREFIX));
controlchannel.setText(Config.get(Config.CONTROL_CHANNEL));
display_song_as_game.setSelected(Boolean.parseBoolean(Config.get(Config.DISPLAY_SONG_AS_GAME)));
voicechannel.setText(Config.get(Config.VOICE_CHANNEL));

int h = Integer.parseInt(Config.get(Config.UPDATE_CHECK_INTERVAL_HOURS));
if (h != 0) {
int updateH = Integer.parseInt(Config.get(Config.UPDATE_CHECK_INTERVAL_HOURS));
if (updateH != 0) {
update_check_box.setSelected(true);
update_check_interval_hours_spinner.setEnabled(true);
update_check_interval_hours_spinner.setValue(h);
update_check_interval_hours_spinner.setValue(updateH);
}

int vol = Integer.parseInt(Config.get(Config.VOLUME));
if (vol != 100) {
chckbxCustomVolume.setSelected(true);
spinnerVolume.setEnabled(true);
spinnerVolume.setValue(vol);
}

String token = Config.get(Config.BOT_TOKEN);
Expand All @@ -196,6 +234,14 @@ private void read() {
bottoken.setText(token);
}

String adminsRoleStr = Config.get(Config.ADMINS_ROLE);
if(adminsRoleStr.isEmpty()) {
adminsrole.setEnabled(false);
} else {
adminsrole.setText(adminsRoleStr);
chckbxAdminsRole.setSelected(true);
}

}

private void hideGetButton() {
Expand Down
17 changes: 16 additions & 1 deletion src/PlayerThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ static void init() {
AudioSourceManagers.registerRemoteSources(playerManager);
AudioSourceManagers.registerLocalSource(playerManager);

// Set custom OutputFormat
//playerManager.getConfiguration().setOutputFormat(new AudioDataFormat(2, 48000, 960, AudioDataFormat.Codec.OPUS));
// Default:
//playerManager.getConfiguration().setOutputFormat(StandardAudioDataFormats.DISCORD_OPUS);
// Currently lavaplayer doesn't play anything with another bitrate than 960. Maybe this is a bug. I asked the developer. Waiting for an answer ...

initGuildAudioPlayer(Bot.getGuild());
}

Expand All @@ -41,6 +47,13 @@ private static synchronized void initGuildAudioPlayer(Guild guild) {
}

guild.getAudioManager().setSendingHandler(musicManager.getSendHandler());

// try to set volume
try {
musicManager.player.setVolume(Integer.parseInt(Config.get(Config.VOLUME)));
} catch (NumberFormatException e) {
a.errExit("Invalid volume");
}
}

static void sendPlaylist(User user, MessageChannel channel) {
Expand Down Expand Up @@ -128,7 +141,9 @@ public void loadFailed(FriendlyException exception) {
}

private static long getYouTubeStartTimeMS(String trackUrl) {
trackUrl = trackUrl.replace("?t=", "&t="); // ? used in youtu.be links
if(trackUrl.contains("youtu.be")) {
trackUrl = trackUrl.replace("?t=", "&t="); // "?" used in youtu.be links
}

if (trackUrl.indexOf("&t=") == -1) {
return 0;
Expand Down
Loading

0 comments on commit 29b9444

Please sign in to comment.