Spela upp musik med en discord bot med JDA

Permalänk
Medlem

Spela upp musik med en discord bot med JDA

Jag håller på att göra en discord bot som ska kunna spela upp musik från en url där en mp3 finns. Jag har gjort så den kan joina och lämna. Men att spela upp musik löser jag inte. Någon som vet hur man gör?

Använder Java, Discord-JDA

```
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
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.track.AudioTrack;
import jdk.jfr.Recording;
import net.dv8tion.jda.api.AccountType;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.events.channel.voice.VoiceChannelCreateEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.managers.AudioManager;
import netscape.javascript.JSObject;
import org.apache.commons.collections4.map.MultiValueMap;
import org.json.JSONArray;
import org.json.JSONObject;

import javax.security.auth.login.LoginException;
import javax.sound.midi.Track;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.Map;

public class bot extends ListenerAdapter {
private static final String USER_AGENT = "Mozilla/5.0";

public static void main(String[] args) throws LoginException {
JDABuilder builder = new JDABuilder(AccountType.BOT);
builder.addEventListeners(new bot());
builder.setActivity(Activity.watching("vids on RomlandTube"));
builder.setToken("my token is here");
builder.build();
}

@Override
public void onMessageReceived(MessageReceivedEvent event) {
MessageChannel channel = event.getChannel();
Message msg = event.getMessage();

if(msg.getContentRaw().equals("!tube join")) {
// Creates a variable equal to the channel that the user is in.
VoiceChannel connectedChannel = event.getMember().getVoiceState().getChannel();
System.out.println("joinnnnn");
// Checks if they are in a channel -- not being in a channel means that the variable = null.
AudioManager audioManager = event.getGuild().getAudioManager();
audioManager.openAudioConnection(connectedChannel);
} else if(msg.getContentRaw().equals("!tube leave")) { // Checks if the command is !leave.
// Gets the channel in which the bot is currently connected.
VoiceChannel connectedChannel = event.getGuild().getSelfMember().getVoiceState().getChannel();
// Checks if the bot is connected to a voice channel.
if(connectedChannel == null) {
// Get slightly fed up at the user.
channel.sendMessage("I am not connected to a voice channel!").queue();
return;
}
// Disconnect from the channel.
event.getGuild().getAudioManager().closeAudioConnection();
// Notify the user.
channel.sendMessage("Disconnected from the voice channel!").queue();
} else if (msg.getContentRaw().startsWith("!tube play")) {
try {
// Creates a variable equal to the channel that the user is in.
VoiceChannel connectedChannel = event.getMember().getVoiceState().getChannel();
// Checks if they are in a channel -- not being in a channel means that the variable = null.
String id = apirequest(msg.getContentRaw().substring(11));

} catch (IOException e) {
e.printStackTrace();
}

}
}

public static String apirequest(String search) throws IOException {
String id = null;
URL obj = new URL("https://www.tube.romland.space/api.php?title=" + search);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
System.out.println("GET Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) { // success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
JSONArray jsonarry = new JSONArray(response.toString());
JSONObject jsonobj = new JSONObject(jsonarry.get(0).toString());
id = jsonobj.get("id").toString();
System.out.println(jsonobj.get("title"));

} else {
System.out.println("GET request not worked");
}
return id;

}
}
```

Visa signatur

DESKTOP:
AMD Ryzen 5 2600X @ 4.4 GHz - RX 590 8GB - 16GB DDR4
LAPTOPS:
Razer Blade 14 - i7 4720HQ - GTX 970M - 16GB DDR3L
Macbook Pro Early 2015 - i5, 8GB DDR3L

Permalänk
Medlem

LavaPlayer kanske kan vara något? Ett bibliotek som ska kunna spela upp musikfiler från en URL (bland mycket annat): https://github.com/sedmelluq/lavaplayer

Visa signatur

Citera eller nämn gärna mig (@ToJa92) om du svarar på något jag skrivit.
Uppskattar du eller blir hjälpt av ett inlägg jag skrivit är jag tacksam om du gillar det.