6 changed files with 231 additions and 6 deletions
@ -0,0 +1,9 @@
@@ -0,0 +1,9 @@
|
||||
package musicbot; |
||||
|
||||
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; |
||||
|
||||
public interface ButtonInteraction { |
||||
String getName(); |
||||
|
||||
void execute(ButtonInteractionEvent event); |
||||
} |
||||
@ -0,0 +1,29 @@
@@ -0,0 +1,29 @@
|
||||
package musicbot; |
||||
|
||||
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; |
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter; |
||||
import org.jetbrains.annotations.NotNull; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
public class ButtonInteractionManager extends ListenerAdapter { |
||||
private final List<ButtonInteraction> buttonInteractions = new ArrayList<>(); |
||||
|
||||
@Override |
||||
public void onButtonInteraction(@NotNull ButtonInteractionEvent event) { |
||||
final String id = event.getComponent().getId(); |
||||
|
||||
final String[] args = id.split("_"); |
||||
|
||||
for (final ButtonInteraction buttonInteraction : buttonInteractions) { |
||||
if (args[0].equals(buttonInteraction.getName())) { |
||||
buttonInteraction.execute(event); |
||||
} |
||||
} |
||||
} |
||||
|
||||
public void add(@NotNull final ButtonInteraction buttonInteraction) { |
||||
buttonInteractions.add(buttonInteraction); |
||||
} |
||||
} |
||||
@ -0,0 +1,53 @@
@@ -0,0 +1,53 @@
|
||||
package musicbot; |
||||
|
||||
import net.dv8tion.jda.api.EmbedBuilder; |
||||
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; |
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter; |
||||
import net.dv8tion.jda.api.interactions.components.buttons.Button; |
||||
import org.jetbrains.annotations.NotNull; |
||||
import java.util.List; |
||||
|
||||
public class Listeners extends ListenerAdapter { |
||||
|
||||
@Override |
||||
public void onButtonInteraction(@NotNull ButtonInteractionEvent event) { |
||||
final String id = event.getComponent().getId(); |
||||
final String label = event.getComponent().getLabel(); |
||||
|
||||
final String[] args = id.split("_"); |
||||
event.deferReply().queue(); |
||||
|
||||
if (args[0].equalsIgnoreCase("recommend")) { |
||||
|
||||
final YoutubeSearch youtubeSearch = new YoutubeSearch(); |
||||
final String result = youtubeSearch.searchForMusic(label, args[1]); |
||||
|
||||
event.getHook().sendMessage(result).queue(); |
||||
} |
||||
|
||||
if (args[0].equalsIgnoreCase("genre")) { |
||||
final int page = Integer.parseInt(args[1]); |
||||
final int offset = page * 10; |
||||
final int index = offset + 10; |
||||
final int nextPage = page + 1; |
||||
|
||||
final SpotifyClient spotifyClient = new SpotifyClient(); |
||||
final List<String> genres = spotifyClient.findGenres(); |
||||
final int maxSize = genres.size(); |
||||
final int maxPage = (int) Math.floor(maxSize / 10); |
||||
|
||||
final EmbedBuilder eb = new EmbedBuilder(); |
||||
|
||||
if (nextPage <= maxPage) { |
||||
genres.subList(offset, index).forEach(genreItem -> eb.addField(genreItem, "", false)); |
||||
|
||||
event.getHook().sendMessageEmbeds(eb.build()).addActionRow( |
||||
Button.primary("genre_" + nextPage, "More") |
||||
).queue(); |
||||
} else { |
||||
genres.subList(offset, maxSize).forEach(genreItem -> eb.addField(genreItem, "", false)); |
||||
event.getHook().sendMessageEmbeds(eb.build()).queue(); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,38 @@
@@ -0,0 +1,38 @@
|
||||
package musicbot; |
||||
|
||||
import com.google.common.collect.ImmutableList; |
||||
import net.dv8tion.jda.api.EmbedBuilder; |
||||
import net.dv8tion.jda.api.entities.MessageEmbed; |
||||
import net.dv8tion.jda.api.interactions.commands.OptionMapping; |
||||
import net.dv8tion.jda.api.interactions.components.buttons.Button; |
||||
import se.michaelthelin.spotify.model_objects.specification.Artist; |
||||
import se.michaelthelin.spotify.model_objects.specification.ArtistSimplified; |
||||
import se.michaelthelin.spotify.model_objects.specification.Track; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
import java.util.Optional; |
||||
import java.util.stream.Collectors; |
||||
|
||||
public class RecommendationService { |
||||
|
||||
public List<Track> getRecommendations(final String artist, final String genre, final int page) { |
||||
|
||||
if (artist.isEmpty() && genre.isEmpty()) { |
||||
return ImmutableList.of(); |
||||
} |
||||
final SpotifyClient spotifyClient = new SpotifyClient(); |
||||
final Optional<String> maybeArtistId = findArtistId(artist); |
||||
final String artistId = maybeArtistId.orElse(""); |
||||
final List<Track> tracks = spotifyClient.findRecommendations(artistId, genre); |
||||
|
||||
return tracks; |
||||
} |
||||
|
||||
private Optional<String> findArtistId(final String artist) { |
||||
final SpotifyClient spotifyClient = new SpotifyClient(); |
||||
final Optional<Artist> maybeArtist = spotifyClient.findArtist(artist); |
||||
|
||||
return maybeArtist.map(Artist::getId); |
||||
} |
||||
} |
||||
@ -0,0 +1,86 @@
@@ -0,0 +1,86 @@
|
||||
package musicbot.buttonInteractions; |
||||
|
||||
import musicbot.ButtonInteraction; |
||||
import musicbot.RecommendationService; |
||||
import net.dv8tion.jda.api.EmbedBuilder; |
||||
import net.dv8tion.jda.api.entities.MessageEmbed; |
||||
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent; |
||||
import net.dv8tion.jda.api.interactions.components.buttons.Button; |
||||
import se.michaelthelin.spotify.model_objects.specification.ArtistSimplified; |
||||
import se.michaelthelin.spotify.model_objects.specification.Track; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
import java.util.stream.Collectors; |
||||
|
||||
public class MoreRecommendation implements ButtonInteraction { |
||||
private static final int INCREMENT = 5; |
||||
private static final String ARGS_DELIMITER = "_"; |
||||
private static final String ARTIST_GENRE_DELIMITER = "--"; |
||||
private static final int NUM_RECOMMENDATIONS = 5; |
||||
|
||||
@Override |
||||
public String getName() { |
||||
return "more-recommend"; |
||||
} |
||||
|
||||
@Override |
||||
public void execute(final ButtonInteractionEvent event) { |
||||
final String id = event.getComponent().getId(); |
||||
final String[] args = id.split(ARGS_DELIMITER); |
||||
final String[] artistGenre = args[1].split(ARTIST_GENRE_DELIMITER); |
||||
final String artist = artistGenre[0]; |
||||
final String genre = artistGenre.length < 2 ? "" : artistGenre[1]; |
||||
final int page = Integer.parseInt(args[2]); |
||||
final int nextPage = page + 1; |
||||
final int maxIndex = INCREMENT * page; |
||||
final int baseIndex = maxIndex - INCREMENT; |
||||
|
||||
final List<Track> tracks = new RecommendationService().getRecommendations(artist, genre, page); |
||||
final int maxSize = tracks.size(); |
||||
final int maxPage = (int) Math.floor(maxSize / NUM_RECOMMENDATIONS); |
||||
|
||||
final EmbedBuilder eb = new EmbedBuilder(); |
||||
final List<Track> trackList = tracks.subList(baseIndex, maxIndex); |
||||
if (!trackList.isEmpty()) { |
||||
eb |
||||
.setTitle("More recommendations based on \"" + artist + "\"") |
||||
.setDescription("Here are more recommendations based on your input."); |
||||
|
||||
trackList.forEach(track -> { |
||||
final List<String> trackArtists = Arrays.stream(track.getArtists()) |
||||
.map(ArtistSimplified::getName) |
||||
.collect(Collectors.toList()); |
||||
final String artists = String.join(", ", trackArtists); |
||||
|
||||
eb.addField(track.getName(), artists, false); |
||||
}); |
||||
|
||||
final MessageEmbed embed = eb.build(); |
||||
|
||||
final List<Button> buttons = trackList.stream().map(track -> { |
||||
final String artists = Arrays.stream(track.getArtists()).map(ArtistSimplified::getName) |
||||
.collect(Collectors.joining(", ")); |
||||
|
||||
return Button.primary("recommend_" + artists, track.getName()); |
||||
}).collect(Collectors.toList()); |
||||
|
||||
if (nextPage <= maxPage) { |
||||
final String buttonId = "more-recommend_" + artist + "--" + genre + "_" + nextPage; |
||||
|
||||
event |
||||
.getHook() |
||||
.sendMessageEmbeds(embed) |
||||
.addActionRow(buttons) |
||||
.addActionRow(Button.secondary(buttonId, "Get more recommendations")) |
||||
.queue(); |
||||
} else { |
||||
event |
||||
.getHook() |
||||
.sendMessageEmbeds(embed) |
||||
.addActionRow(buttons) |
||||
.queue(); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue