1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
package soundchan;
import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManagers;
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import net.dv8tion.jda.core.AccountType;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.JDABuilder;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.MessageChannel;
import net.dv8tion.jda.core.entities.VoiceChannel;
import net.dv8tion.jda.core.events.guild.voice.GuildVoiceMoveEvent;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import net.dv8tion.jda.client.events.call.voice.CallVoiceJoinEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;
import net.dv8tion.jda.core.managers.AudioManager;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
public class Main extends ListenerAdapter {
public static void main(String[] args) throws Exception {
Properties properties = LoadProperties();
JDA jda = new JDABuilder(AccountType.BOT)
.setToken(properties.getProperty("botToken"))
.buildBlocking();
localFilePath = properties.getProperty("localFilePath");
jda.addEventListener(new Main());
followingUser = properties.getProperty("followingUser");
}
private static Properties LoadProperties(){
Properties properties = new Properties();
InputStream input = null;
try{
input = new FileInputStream("soundchan.properties");
properties.load(input);
}catch (IOException ex){
ex.printStackTrace();
} finally {
try {
input.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return properties;
}
private long monitoredGuildId = -1;
private Guild monitoredGuild;
private static String followingUser;
private static String localFilePath;
private final AudioPlayerManager playerManager;
private final Map<Long, GuildMusicManager> musicManagers;
private Main() {
this.musicManagers = new HashMap<>();
this.playerManager = new DefaultAudioPlayerManager();
AudioSourceManagers.registerRemoteSources(playerManager);
AudioSourceManagers.registerLocalSource(playerManager);
}
private synchronized GuildMusicManager getGuildAudioPlayer(Guild guild) {
long guildId = Long.parseLong(guild.getId());
GuildMusicManager musicManager = musicManagers.get(guildId);
if (musicManager == null) {
musicManager = new GuildMusicManager(playerManager);
musicManagers.put(guildId, musicManager);
}
guild.getAudioManager().setSendingHandler(musicManager.getSendHandler());
return musicManager;
}
private synchronized GuildMusicManager getGuildAudioPlayer() {
long guildId = monitoredGuildId;
GuildMusicManager musicManager = musicManagers.get(guildId);
if (musicManager == null) {
musicManager = new GuildMusicManager(playerManager);
musicManagers.put(guildId, musicManager);
}
monitoredGuild.getAudioManager().setSendingHandler(musicManager.getSendHandler());
return musicManager;
}
@Override
public void onCallVoiceJoin(CallVoiceJoinEvent event){
}
@Override
public void onGuildVoiceMove(GuildVoiceMoveEvent event) {
if(event.getMember().getEffectiveName().compareTo(followingUser) == 0) {
AudioManager audioManager = monitoredGuild.getAudioManager();
if(!audioManager.isAttemptingToConnect()) {
audioManager.openAudioConnection(event.getChannelJoined());
}
}
super.onGuildVoiceMove(event);
}
@Override
public void onMessageReceived(MessageReceivedEvent event) {
String[] command = event.getMessage().getContentRaw().split(" ", 2);
Guild guild = event.getGuild();
MessageChannel channel = null;
// This means SoundChan was DM'd
if (guild == null){
channel = event.getPrivateChannel();
}else{
// This means SoundChan was referred to in a TextChannel
channel = event.getTextChannel();
}
if(monitoredGuildId == -1 && guild != null){
monitoredGuildId = Long.parseLong(guild.getId());
monitoredGuild = guild;
}
if(monitoredGuild != null){
// "!" Signifies that you're looking to play a sound effect
if(command[0].startsWith("!") && command[0].length() > 1){
loadAndPlay(channel, localFilePath + "\\" + command[0].substring(1) + ".mp3");
}
// "~" Signifies that you're looking to play a song/sound from a url
if(command[0].startsWith("~") && command[0].length() > 1){
if ("~play".equals(command[0]) && command.length == 2) {
loadAndPlay(channel, command[1]);
} else if ("~skip".equals(command[0])) {
skipTrack(channel);
} else if ("~volume".equals(command[0]) && command.length == 2) {
changeVolume(channel, command[1]);
} else if ("~pause".equals(command[0])) {
pauseTrack(channel);
} else if ("~unpause".equals(command[0])) {
unpauseTrack(channel);
} else if ("~list".equals(command[0])) {
listTracks(channel);
}
}
}
super.onMessageReceived(event);
}
private void changeVolume(final MessageChannel channel, final String volume) {
GuildMusicManager musicManager = getGuildAudioPlayer();
musicManager.player.setVolume(Integer.parseInt(volume));
channel.sendMessage("Volume now set to " + volume + "%").queue();
}
private void listTracks(final MessageChannel channel) {
GuildMusicManager musicManager = getGuildAudioPlayer();
List<String> queueContents = musicManager.scheduler.getQueueContents();
String printMessage = "Tracks in the queue:\n";
for (String track:
queueContents) {
printMessage = printMessage + track + "\n";
}
channel.sendMessage(printMessage).queue();
}
private void pauseTrack(final MessageChannel channel){
GuildMusicManager musicManager = getGuildAudioPlayer();
musicManager.player.setPaused(true);
channel.sendMessage("Playback Paused.").queue();
}
private void unpauseTrack(final MessageChannel channel){
GuildMusicManager musicManager = getGuildAudioPlayer();
musicManager.player.setPaused(false);
channel.sendMessage("Unpaused playback.").queue();
}
private void loadAndPlay(final MessageChannel channel, final String trackUrl) {
GuildMusicManager musicManager = getGuildAudioPlayer();
playerManager.loadItemOrdered(musicManager, trackUrl, new AudioLoadResultHandler() {
@Override
public void trackLoaded(AudioTrack track) {
int timeStart = trackUrl.lastIndexOf('=');
if(timeStart != -1){
String timeString = trackUrl.substring(timeStart);
//The format will be 1h2m53s, need to parse that into seconds and then call
//track.setPosition(long position)
}
channel.sendMessage("Adding to queue " + track.getInfo().title).queue();
play(monitoredGuild, musicManager, track);
}
@Override
public void playlistLoaded(AudioPlaylist playlist) {
AudioTrack firstTrack = playlist.getSelectedTrack();
if (firstTrack == null) {
firstTrack = playlist.getTracks().get(0);
}
channel.sendMessage("Adding to queue " + firstTrack.getInfo().title + " (first track of playlist " + playlist.getName() + ")").queue();
play(monitoredGuild, musicManager, firstTrack);
}
@Override
public void noMatches() {
channel.sendMessage("Nothing found by " + trackUrl).queue();
}
@Override
public void loadFailed(FriendlyException exception) {
channel.sendMessage("Could not play: " + exception.getMessage()).queue();
}
});
}
private void play(Guild guild, GuildMusicManager musicManager, AudioTrack track) {
connectToFirstVoiceChannel(guild.getAudioManager());
musicManager.scheduler.queue(track);
}
private void skipTrack(MessageChannel channel) {
GuildMusicManager musicManager = getGuildAudioPlayer();
musicManager.scheduler.nextTrack();
channel.sendMessage("Skipped to next track.").queue();
}
private static void connectToFirstVoiceChannel(AudioManager audioManager) {
if (!audioManager.isConnected() && !audioManager.isAttemptingToConnect()) {
for (VoiceChannel voiceChannel : audioManager.getGuild().getVoiceChannels()) {
for(int i = 0; i < voiceChannel.getMembers().size(); i++) {
if(voiceChannel.getMembers().get(i).getEffectiveName().compareTo(followingUser) == 0) {
audioManager.openAudioConnection(voiceChannel);
break;
}
}
}
}
}
}
|