Skip to content

Commit

Permalink
[REQUEST] make option to disable track announcements
Browse files Browse the repository at this point in the history
And some other things
  • Loading branch information
duncte123 committed Feb 13, 2018
1 parent dfef6f5 commit ea4c17e
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 12 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ plugins {

group = 'ml.duncte123.skybot'

version '3.61.5'
version '3.61.6'

sourceCompatibility = 1.9

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import fredboat.audio.player.LavalinkManager;
import lavalink.client.player.IPlayer;
import ml.duncte123.skybot.objects.guild.GuildSettings;
import ml.duncte123.skybot.utils.GuildSettingsUtils;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.TextChannel;

Expand All @@ -30,6 +32,8 @@ public class GuildMusicManager {
*/
public TextChannel latestChannel = null;

final GuildSettings guildSettings;

/**
* This is our player
*/
Expand All @@ -55,6 +59,7 @@ public GuildMusicManager(Guild g) {
scheduler = new TrackScheduler(player, this);
sendHandler = new AudioPlayerSenderHandler(player);
player.addListener(scheduler);
this.guildSettings = GuildSettingsUtils.getGuild(g);
}

/**
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/ml/duncte123/skybot/audio/TrackScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void nextTrack() {
AudioTrack nextTrack = queue.poll();
if(nextTrack != null) {
player.playTrack(nextTrack);
MessageUtils.sendMsg(guildMusicManager.latestChannel, "Now playing: " + nextTrack.getInfo().title);
announceNextTrack(nextTrack);
}
} else if(player.getPlayingTrack() != null)
player.seekTo(player.getPlayingTrack().getDuration());
Expand All @@ -121,10 +121,11 @@ public void onTrackEnd(AudioPlayer player, AudioTrack track, AudioTrackEndReason
logger.debug("repeating");
if (!repeatPlayList) {
this.player.playTrack(lastTrack.makeClone());
MessageUtils.sendMsg(guildMusicManager.latestChannel, "Now playing: " + lastTrack.getInfo().title);
announceNextTrack(lastTrack);
} else {
logger.debug("a playlist.....");
queue(lastTrack.makeClone());
nextTrack();
}
} else {
logger.debug("starting next track");
Expand Down Expand Up @@ -176,4 +177,9 @@ public void shuffle() {
Collections.shuffle((List<?>) queue);
}

private void announceNextTrack(AudioTrack track) {
if(guildMusicManager.guildSettings.isAnnounceTracks())
MessageUtils.sendMsg(guildMusicManager.latestChannel, "Now playing: " + track.getInfo().title);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ public void executeCommand(String invoke, String[] args, GuildMessageReceivedEve
MessageEmbed message = EmbedUtils.embedMessage("Here are the settings from this guild.\n" +
"**Show join/leave messages:** " + (settings.isEnableJoinMessage() ? "<:check:314349398811475968>" : "<:xmark:314349398824058880>") + "\n" +
"**Swearword filter:** " + (settings.isEnableSwearFilter() ? "<:check:314349398811475968>" : "<:xmark:314349398824058880>") + "\n" +
"**Announce next track:** " + (settings.isAnnounceTracks() ? "<:check:314349398811475968>" : "<:xmark:314349398824058880>") + "\n" +
"**Join message:** " + settings.getCustomJoinMessage() + "\n" +
"**Leave message:** " + settings.getCustomLeaveMessage() + "\n" +
"**AutoRole:** " + (settings.getAutoroleRole() == null || settings.getAutoroleRole().equals("") ? "Not Set": event.getGuild().getRoleById(settings.getAutoroleRole()).getAsMention() )+ "\n" +
"**AutoRole:** " + (settings.getAutoroleRole() == null || settings.getAutoroleRole().equals("")
? "Not Set": event.getGuild().getRoleById(settings.getAutoroleRole()).getAsMention() )+ "\n" +
"**Current prefix:** " + settings.getCustomPrefix() + "\n" +
"**Modlog Channel:** " + (logChan !=null ? logChan.getAsMention(): "none") + "\n" +
"**Welcome/Leave channel:** " + (welcomeLeaveChannel != null ? welcomeLeaveChannel.getAsMention() : "none")
Expand Down Expand Up @@ -222,6 +224,13 @@ public void executeCommand(String invoke, String[] args, GuildMessageReceivedEve
GuildSettingsUtils.updateGuildSettings(event.getGuild(), settings.setServerDesc(description) );
sendMsg(event, "Description has been updated, check `" + PREFIX + "guildinfo` to see your description");
break;

case "toggleannouncetracks":
boolean shouldAnnounceTracks = !settings.isAnnounceTracks();
GuildSettingsUtils.updateGuildSettings(event.getGuild(), settings.setAnnounceTracks(shouldAnnounceTracks) );
sendMsg(event, "Announcing the next track has been **"
+ ( shouldAnnounceTracks ? "enabled" : "disabled" ) + "**" );
break;
}
}

Expand All @@ -237,7 +246,8 @@ public String help() {
"`"+PREFIX+"setLogChannel <text channel>` => Sets the channel to log messages in\n" +
"`"+PREFIX+"setWelcomeChannel <channel>` => Sets the channel that displays the welcome and leave messages\n" +
"`"+PREFIX+"autorole <role>` => Gives members a role when they join\n" +
"`"+PREFIX+"setdescription <desc>` => Set a custom description in " + PREFIX + "guildinfo"
"`"+PREFIX+"setdescription <desc>` => Set a custom description in " + PREFIX + "guildinfo\n" +
"`"+PREFIX+"toggleannouncetracks` => Toggles if the player should announce the next playing track"

;
}
Expand All @@ -264,7 +274,8 @@ public String[] getAliases() {
"setleavechannel",
"setleavemessage",
"autorole",
"setdescription"
"setdescription",
"toggleannouncetracks"
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ private void innitDB(Connection connection) {
" `autoRole` varchar(255) DEFAULT NULL,\n" +
" `enableJoinMessage` tinyint(1) NOT NULL DEFAULT '0',\n" +
" `enableSwearFilter` tinyint(1) NOT NULL DEFAULT '0',\n" +
" `announceNextTrack` tinyint(1) NOT NULL DEFAULT '1',\n" +
" `customWelcomeMessage` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci DEFAULT NULL,\n" +
" `customLeaveMessage` text DEFAULT NULL,\n" +
" `serverDesc` text DEFAULT NULL,\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ private void innitDB(Connection connection) {
"autoRole VARCHAR(255) NULL," +
"enableJoinMessage tinyint(1) NOT NULL DEFAULT '0'," +
"enableSwearFilter tinyint(1) NOT NULL DEFAULT '0'," +
"announceNextTrack tinyint(1) NOT NULL DEFAULT '1'," +
"customWelcomeMessage TEXT NOT NULL," +
"serverDesc TEXT NULL," +
"customLeaveMessage TEXT NOT NULL);");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ public class SpotifyAudioSourceManager implements AudioSourceManager, HttpConfig
private final YoutubeSearchProvider youtubeSearchProvider;
private final YoutubeAudioSourceManager youtubeAudioSourceManager;
private final SoundCloudAudioSourceManager manager = null;
private final ScheduledExecutorService service;

// public SpotifyAudioSourceManager(SoundCloudAudioSourceManager manager) {
// public SpotifyAudioSourceManager(SoundCloudAudioSourceManager manager) {
public SpotifyAudioSourceManager(YoutubeAudioSourceManager youtubeAudioSourceManager) {
String defaultValue = "To use Spotify search, please create an app over at https://developer.spotify.com/web-api/";
String clientId = AirUtils.config.getString("apis.spotify.clientId", defaultValue);
Expand All @@ -89,6 +90,7 @@ public SpotifyAudioSourceManager(YoutubeAudioSourceManager youtubeAudioSourceMan
this.api = null;
//this.manager = null;
youtubeSearchProvider = null;
this.service = null;
this.youtubeAudioSourceManager = null;
} else {
//this.manager = manager;
Expand All @@ -98,7 +100,7 @@ public SpotifyAudioSourceManager(YoutubeAudioSourceManager youtubeAudioSourceMan
.clientId(clientId)
.clientSecret(clientSecret)
.build();
final ScheduledExecutorService service = Executors.newScheduledThreadPool(1, r -> new Thread(r, "Spotify-Token-Update-Thread"));
this.service = Executors.newScheduledThreadPool(1, r -> new Thread(r, "Spotify-Token-Update-Thread"));
service.scheduleAtFixedRate(this::updateAccessToken, 0, 1, TimeUnit.HOURS);
}
}
Expand Down Expand Up @@ -245,7 +247,11 @@ public AudioTrack decodeTrack(AudioTrackInfo trackInfo, DataInput input) {

@Override
public void shutdown() {
//youtubeAudioSourceManager.shutdown();
if(this.youtubeAudioSourceManager != null)
this.youtubeAudioSourceManager.shutdown();
if(this.service != null)
this.service.shutdown();

}

@Override
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/ml/duncte123/skybot/objects/guild/GuildSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public class GuildSettings {
*/
private String serverDesc = null;

private boolean announceTracks = false;

/**
* This will init everything
*
Expand Down Expand Up @@ -263,6 +265,24 @@ public String getServerDesc() {
return serverDesc;
}

/**
* Sets if the audio player should announce the tracks
* @param announceTracks true to announce tracks
* @return the current {@link GuildSettings}
*/
public GuildSettings setAnnounceTracks(boolean announceTracks) {
this.announceTracks = announceTracks;
return this;
}

/**
* Returns if we should announce the next track
* @return if we should announce the next track
*/
public boolean isAnnounceTracks() {
return announceTracks;
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public static void loadGuildSettings() {
String leaveMessage = replaceNewLines(resSettings.getString("customLeaveMessage"));
String autoroleId = resSettings.getString("autoRole");
String serverDesc = replaceNewLines(resSettings.getString("serverDesc"));
boolean announceNextTrack = resSettings.getBoolean("announceNextTrack");

AirUtils.guildSettings.put(guildId, new GuildSettings(guildId)
.setEnableJoinMessage(enableJoinMsg)
Expand All @@ -107,6 +108,7 @@ public static void loadGuildSettings() {
.setCustomLeaveMessage(leaveMessage)
.setAutoroleRole(autoroleId)
.setServerDesc(serverDesc)
.setAnnounceTracks(announceNextTrack)
);
}

Expand Down Expand Up @@ -160,6 +162,8 @@ public static void updateGuildSettings(Guild guild, GuildSettings settings) {
String chanId = settings.getLogChannel();
String welcomeLeaveChannel = settings.getWelcomeLeaveChannel();
String serverDesc = settings.getServerDesc();
boolean announceNextTrack = settings.isAnnounceTracks();

String dbName = AirUtils.db.getName();
Connection database = AirUtils.db.getConnManager().getConnection();

Expand All @@ -173,7 +177,8 @@ public static void updateGuildSettings(Guild guild, GuildSettings settings) {
"logChannelId= ? ," +
"welcomeLeaveChannel= ? ," +
"customLeaveMessage = ? ," +
"serverDesc = ? " +
"serverDesc = ? ," +
"announceNextTrack = ? " +
"WHERE guildId='" + guildId + "'");
preparedStatement.setBoolean(1, enableJoinMessage);
preparedStatement.setBoolean(2, enableSwearFilter);
Expand All @@ -184,6 +189,7 @@ public static void updateGuildSettings(Guild guild, GuildSettings settings) {
preparedStatement.setString(7, welcomeLeaveChannel);
preparedStatement.setString(8, replaceUnicode(customLeaveMessage));
preparedStatement.setString(9, replaceUnicode(serverDesc));
preparedStatement.setBoolean(10, announceNextTrack);
preparedStatement.executeUpdate();

} catch (SQLException e1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class EvalCommand : Command() {
}
}
when (out) {
null, (out as String).isBlank() -> {
null -> {
coroutine.coroutineContext.cancel()
MessageUtils.sendSuccess(event.message)
}
Expand Down Expand Up @@ -237,6 +237,10 @@ class EvalCommand : Command() {
}
}
else -> {
if(out.toString().isEmpty() || out.toString().isBlank() || out.toString() == "") {
MessageUtils.sendSuccess(event.message)
return
}
if (isRanByBotOwner) {
MessageBuilder()
.append(out.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class RepeatCommand : MusicCommand() {
}

scheduler.isRepeating = !scheduler.isRepeating
MessageUtils.sendMsg(event, "Player was set to: **${if (scheduler.isRepeating) " " else "not"} repeating**")
MessageUtils.sendMsg(event, "Player was set to: **${if (scheduler.isRepeating) "" else "not "} repeating**")
}

override fun help(): String = "Makes the player repeat the currently playing song"
Expand Down

0 comments on commit ea4c17e

Please sign in to comment.