(Terminé)

Refonte complète du système de jukebox et de menu.
This commit is contained in:
2021-10-05 15:20:23 +02:00
parent 43145071f1
commit f318614e96
14 changed files with 160 additions and 108 deletions

View File

@@ -21,5 +21,10 @@
<option name="name" value="BintrayJCenter" />
<option name="url" value="https://jcenter.bintray.com/" />
</remote-repository>
<remote-repository>
<option name="id" value="maven" />
<option name="name" value="maven" />
<option name="url" value="https://m2.dv8tion.net/releases" />
</remote-repository>
</component>
</project>

View File

@@ -14,13 +14,15 @@ plugins {
id 'application'
// Shadow plugin to make uberJars
id 'com.github.johnrengelman.shadow' version '6.1.0'
id 'com.github.johnrengelman.shadow' version '7.1.0'
}
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
mavenCentral()
maven {
url 'https://m2.dv8tion.net/releases'
}
}
dependencies {
@@ -34,8 +36,8 @@ dependencies {
implementation 'ws.schild:jave-all-deps:2.7.3'
implementation 'org.apache.commons:commons-lang3:3.0'
implementation 'com.sedmelluq:lavaplayer:1.3.67'
implementation 'net.dv8tion:JDA:4.2.0_227'
implementation 'com.sedmelluq:lavaplayer:1.3.75'
implementation 'net.dv8tion:JDA:4.2.1_253'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'

View File

@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@@ -28,9 +28,15 @@ public class JdrBot {
public static void main(String args[]) {
try {
basedate = LocalDateTime.now();
jda = JDABuilder.createDefault("MTY5OTMzMzgxMDMzOTE4NDY0.DerlJg.m7BdNv_OMHlYa-f4T3O0jJ9LldM")
/*jda = JDABuilder.createDefault("MTY5OTMzMzgxMDMzOTE4NDY0.DerlJg.m7BdNv_OMHlYa-f4T3O0jJ9LldM")
.setActivity(Activity.playing("un jeu de rôle"))
.enableCache(CacheFlag.VOICE_STATE).build();
*/
jda = JDABuilder.createDefault("ODk0ODc0Nzk5Nzg3MTE0NTA2.YVwXGg.JfIsIIQHIXbVsjoGGv2SfejnT9s")
.setActivity(Activity.playing("un jeu de rôle"))
.enableCache(CacheFlag.VOICE_STATE).build();
jda.awaitReady();
commandList = new ArrayList<Command>();

View File

@@ -32,7 +32,7 @@ public class JdrBotListener extends ListenerAdapter {
if (isACommand(e.getMessage().getContentRaw(), "yay"))
{
e.getChannel().sendMessage("Bien joue! Tu as su surpasse toutes les epreuves pour en arriver la! Je suis fier de toi!").queue();
e.getChannel().sendMessage("Bien joue! Tu as su surpasse toutes les epreuves pour en arriver la! Woo !").queue();
}
}

View File

@@ -0,0 +1,18 @@
package fr.Skydust.JdrBot.cmds;
import fr.Skydust.JdrBot.stock.Command;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
public class AddSong extends Command {
public AddSong() {
SetName("addsong");
SetUsage("[Chemin] [URL]");
SetDesc("(Admin) Ajoute une musique au jukebox");
}
@Override
public void call(GuildMessageReceivedEvent e) {
//TODO: Chemin sans le chemin initial
}
}

View File

@@ -12,7 +12,7 @@ import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.events.user.update.UserUpdateOnlineStatusEvent;
public class LastTimeOnline extends Command {
static HashMap<String, LocalDateTime> AllUsers = new HashMap<>();
static HashMap<Long, LocalDateTime> AllUsers = new HashMap<>();
public LastTimeOnline() {
SetName("lasttimeonline|lto");
@@ -27,12 +27,14 @@ public class LastTimeOnline extends Command {
return;
}
User u = e.getMessage().getMentionedUsers().get(0);
if(AllUsers.get(u.getId()) == null) {
if(AllUsers.get(u.getIdLong()) == null) {
e.getChannel().sendMessage(u.getAsMention()+" n'a pas change d'etat entre le demarrage du bot et maintenant").queue();
return;
}
OnlineStatus status = e.getGuild().getMember(u).getOnlineStatus();
LocalDateTime UserDate = AllUsers.get(e.getMessage().getMentionedUsers().get(0).getId());
LocalDateTime UserDate = AllUsers.get(e.getMessage().getMentionedUsers().get(0).getIdLong());
StringBuilder sb = new StringBuilder();
if(status.equals(OnlineStatus.INVISIBLE) || status.equals(OnlineStatus.OFFLINE)) {
sb.append(u.getAsMention()+" est en hors-ligne depuis ");
@@ -40,6 +42,7 @@ public class LastTimeOnline extends Command {
sb.append(u.getAsMention()+" est en ligne depuis ");
}
sb.append(Utils.formatDurationSmooth(Duration.between(UserDate, LocalDateTime.now())));
e.getChannel().sendMessage(sb.toString()).queue();
}
@@ -47,10 +50,10 @@ public class LastTimeOnline extends Command {
public static void onUserUpdateOnlineStatus(UserUpdateOnlineStatusEvent e) {
if(e.getOldOnlineStatus().equals(OnlineStatus.OFFLINE)) {
//Si il est en ligne
AllUsers.put(e.getUser().getId(), LocalDateTime.now());
AllUsers.put(e.getUser().getIdLong(), LocalDateTime.now());
} else if(!e.getOldOnlineStatus().equals(OnlineStatus.ONLINE) && !e.getOldOnlineStatus().equals(OnlineStatus.DO_NOT_DISTURB) && !e.getOldOnlineStatus().equals(OnlineStatus.IDLE)) {
//Si il est hors-ligne
AllUsers.put(e.getUser().getId(), LocalDateTime.now());
AllUsers.put(e.getUser().getIdLong(), LocalDateTime.now());
}
}

View File

@@ -25,7 +25,7 @@ public class PlayMusic extends Command {
private static final AudioPlayerManager playerManager = new DefaultAudioPlayerManager();
static Map<Long, GuildMusicManager> musicManagers;
public static Map<Long, JukeboxSystem> jukeboxSystems;
public static HashMap<Long, JukeboxSystem> jukeboxSystems = new HashMap<>();
public PlayMusic() {
musicManagers = new HashMap<>();
@@ -46,9 +46,17 @@ public class PlayMusic extends Command {
e.getChannel().sendMessage("/!\\ Le lecteur audio est deja present ici ! Pour l'arreter, tapez \"!stopmusic\" !").queue();
return;
}
if(!e.getMember().getVoiceState().inVoiceChannel()) {
e.getChannel().sendMessage("/!\\ Vous devez être dans un channel vocal").queue();
return;
}
//TODO: WHILE LOADING, CAN CALL AGAIN
// Creates a message to be edited by the jukebox system
e.getChannel().sendMessage("Loading...").queue(msg -> jukeboxSystems.put(e.getGuild().getIdLong(), new JukeboxSystem(msg)));
// Creates a message to be edited by the jukebox system
e.getGuild().getAudioManager().openAudioConnection(e.getMember().getVoiceState().getChannel());
e.getChannel().sendMessage("Chargement...").queue(msg -> jukeboxSystems.put(e.getGuild().getIdLong(), new JukeboxSystem(msg)));
}
public static JukeboxSystem getGuildsJukebox(Guild guild) {

View File

@@ -40,6 +40,7 @@ public class StopMusic extends Command {
JukeboxSystem jukebox = PlayMusic.getGuildsJukebox(g);
if(jukebox != null) {
System.out.println("JukeboxMessageIDStop:"+jukebox.jukeboxMessage.getId());
jukebox.jukeboxMessage.delete().queue();
}

View File

@@ -5,6 +5,7 @@ import fr.Skydust.JdrBot.cmds.playmusic.StopMusic;
import fr.Skydust.JdrBot.menu.Menu;
import fr.Skydust.JdrBot.menu.MenuSystem;
import fr.Skydust.JdrBot.utils.Utils;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import java.io.File;
@@ -17,7 +18,7 @@ public class JukeboxSystem {
// Dyn
// Keeping track of the song currently played to update menus accordingly
// Example : "Songs/Spell Temps/Temps ville 2"
// Example : "Songs/Spell Temps/2"
public String songPlayedPath = "";
private final HashMap<String, Menu> linkedPath = new HashMap<>();
@@ -40,7 +41,11 @@ public class JukeboxSystem {
List<String> items = new ArrayList<>();
File[] files = new File(currentFolder).listFiles();
if(files.length != 0) {
// If it isn't the root folder, place the return button
if(!isInRootFolder()) {
items.add(Utils.ReturnUnicode + " Retour");
}
if(files != null) {
// Sort files by alphabetical order with directories first
Arrays.sort(files, sortItemsComp);
@@ -51,89 +56,97 @@ public class JukeboxSystem {
* 2⃣ 🔉 Played Song
*/
for (File file : files) {
StringBuilder sb = new StringBuilder();
if(!file.getName().startsWith(".")) { // Ignore Unix hidden files
StringBuilder sb = new StringBuilder();
sb.append(Utils.CharsUnicodeArray[items.size()]).append(" ");
if (file.isDirectory())
sb.append(Utils.FolderUnicode);
else
sb.append(Utils.SongUnicode);
if(file.isDirectory())
sb.append(Utils.FolderUnicode);
else
sb.append(Utils.SongUnicode);
sb.append(" ");
sb.append(file.getName().replaceAll(".mp3", ""));
sb.append(" ");
sb.append(file.getName().replaceAll(".mp3", ""));
items.add(sb.toString());
}
// If it isn't the root folder, place the return button
if(!currentFolder.equals(initialFolder)) {
items.add(Utils.ReturnUnicode + Utils.CharsUnicodeArray[items.size()] + " Retour");
items.add(sb.toString());
}
}
}
menu = linkedPath.put(currentFolder,
new Menu("Music Player 4000", "Choisis la musique :", musicPlayerThumbnail, (String[]) items.toArray()) {
@Override
public void onButtonClicked(Message msg, int itemId) {
String str = items[itemId];
menu = new Menu("Music Player 4000", "Choisis la musique :", musicPlayerThumbnail, items.toArray(new String[0])) {
@Override
public void onButtonClicked(Member member, Message msg, int itemId) {
if(itemId >= items.length)
return;
// Loop back to open a subfolder
if (str.contains(Utils.FolderUnicode)) {
openFolder(currentFolder + "/" + getFileNameFromItem(str));
return;
}
String str = items[itemId];
// Return button
if (itemId == items.length - 1) {
// Back one menu
String subFolder = currentFolder.substring(0, currentFolder.lastIndexOf("/"));
MenuSystem.changeMenu(msg, linkedPath.get(subFolder));
return;
}
// Loop back to open a subfolder
if (str.contains(Utils.FolderUnicode)) {
openFolder(currentFolder + "/" + getFileNameFromItem(str));
return;
}
// If not already playing
if (str.contains(Utils.SongUnicode)) {
if(!songPlayedPath.equals("")) {
String folder = songPlayedPath.substring(0, songPlayedPath.lastIndexOf("/"));
int previousSongItemID = Integer.parseInt(songPlayedPath.substring(songPlayedPath.lastIndexOf("/")));
// Return button
if (!isInRootFolder() && itemId == 0) {
// Back one menu
String subFolder = currentFolder.substring(0, currentFolder.lastIndexOf("/"));
currentFolder = subFolder;
MenuSystem.changeMenu(msg, linkedPath.get(subFolder));
return;
}
Menu previousMenu = linkedPath.get(folder);
previousMenu.items[previousSongItemID] = previousMenu.items[previousSongItemID].replace(Utils.PlayUnicode, Utils.SongUnicode);
}
items[itemId] = items[itemId].replace(Utils.SongUnicode, Utils.PlayUnicode);
// If not already playing
if (str.contains(Utils.SongUnicode)) {
if(!songPlayedPath.equals("")) {
String folder = songPlayedPath.substring(0, songPlayedPath.lastIndexOf("/"));
int previousSongItemID = Integer.parseInt(songPlayedPath.substring(songPlayedPath.lastIndexOf("/")+1));
String songPath = currentFolder + getFileNameFromItem(str) + ".mp3";
PlayMusic.loadAndPlay(jukeboxMessage.getTextChannel(), songPath, true, true);
// Format: Songs/Folder/Folder/2
songPlayedPath = currentFolder + "/" + itemId;
// Reload the current menu
MenuSystem.changeMenu(msg, this);
} else {
//If already playing, stop the song
items[itemId] = items[itemId].replace(Utils.PlayUnicode, Utils.SongUnicode);
StopMusic.stopMusic(jukeboxMessage.getGuild());
}
Menu previousMenu = linkedPath.get(folder);
previousMenu.items[previousSongItemID] = previousMenu.items[previousSongItemID].replace(Utils.PlayUnicode, Utils.SongUnicode);
}
});
items[itemId] = items[itemId].replace(Utils.SongUnicode, Utils.PlayUnicode);
String songPath = currentFolder + "/" + getFileNameFromItem(str) + ".mp3";
PlayMusic.loadAndPlay(msg.getTextChannel(), songPath, true, true);
// Format: Songs/Folder/SubFolder/2
songPlayedPath = currentFolder + "/" + itemId;
} else {
//If already playing, stop the song
items[itemId] = items[itemId].replace(Utils.PlayUnicode, Utils.SongUnicode);
PlayMusic.getGuildAudioPlayer(member.getGuild()).scheduler.stop();
}
// Reload the current menu
MenuSystem.reloadMenu(msg);
}
};
linkedPath.put(currentFolder, menu);
}
MenuSystem.changeMenu(jukeboxMessage, menu);
// Reget the message to update it
Menu finalMenu = menu;
jukeboxMessage.getTextChannel().retrieveMessageById(jukeboxMessage.getIdLong()).queue(msg -> {
MenuSystem.changeMenu(msg, finalMenu);
});
}
public boolean isInRootFolder() {
return currentFolder.equals(initialFolder);
}
public static String getFileNameFromItem(String item) {
return item.split(" ", 3)[2];
return item.split(" ", 2)[1];
}
private final Comparator sortItemsComp = (o1, o2) -> {
File f1 = (File) o1;
File f2 = (File) o2;
if (f1.isDirectory() && !f2.isDirectory())
private final Comparator<File> sortItemsComp = (o1, o2) -> {
if (o1.isDirectory() && !o2.isDirectory())
{
// Directory before non-directory
return -1;
}
else if (!f1.isDirectory() && f2.isDirectory())
else if (!o1.isDirectory() && o2.isDirectory())
{
// Non-directory after directory
return 1;
@@ -142,7 +155,7 @@ public class JukeboxSystem {
{
// Alphabetic order otherwise
//return o1.compareTo(o2);
return f1.compareTo(f2);
return o1.compareTo(o2);
}
};
}

View File

@@ -3,11 +3,10 @@ package fr.Skydust.JdrBot.menu;
import fr.Skydust.JdrBot.utils.Utils;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.MessageBuilder;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.TextChannel;
import java.awt.*;
import java.awt.event.ActionEvent;
public class Menu {
public String thumbnail;
@@ -23,9 +22,7 @@ public class Menu {
this.items = items;
}
public void onButtonClicked(Message msg, int itemId) {
}
public void onButtonClicked(Member member, Message msg, int itemId) { }
/** Builds the menu message */
public Message makeMessage() {
@@ -33,7 +30,7 @@ public class Menu {
int i = 0;
for (String s : items) {
str.append(Utils.CharsUnicodeArray[i] +":"+ s +"\n");
str.append(Utils.CharsUnicodeArray[i] +" "+ s +"\n");
i++;
}
@@ -48,6 +45,4 @@ public class Menu {
.build())
.build();
}
}

View File

@@ -8,7 +8,7 @@ import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
import java.util.HashMap;
public class MenuSystem {
private static HashMap<Message, Menu> menuHash = new HashMap<>();
private static final HashMap<Message, Menu> menuHash = new HashMap<>();
/** Action Listening */
public static void onMessageReactionAdd(MessageReactionAddEvent e) {
@@ -37,23 +37,29 @@ public class MenuSystem {
return;
}
menu.onButtonClicked(msg, emoteId);
menu.onButtonClicked(e.getMember(), msg, emoteId);
}
});
}
}
/** Create a new menu message and add it the the specified channel */
public static void setupMenuWithMessage(TextChannel channel, Menu menu) {
channel.sendMessage(menu.makeMessage()).queue(msg ->
{
addReactions(msg, menu);
menuHash.put(msg, menu);
});
/** Reload the message with its menu */
public static void reloadMenu(Message currentMessage) {
Menu newMenu = menuHash.get(currentMessage);
if(newMenu == null) {
System.out.println("Error: No menu on this message");
return;
}
currentMessage.editMessage(newMenu.makeMessage()).queue();
updateReactions(currentMessage, newMenu);
}
/** Adds missing reactions */
public static void addReactions(Message msg, Menu menu) {
public static void updateReactions(Message msg, Menu menu) {
// Removes excess reactions
for (int y = menu.items.length; y < msg.getReactions().size(); y++) {
msg.getReactions().get(y).removeReaction().queue();
}
// Adds missing reactions
for (int i = msg.getReactions().size(); i < menu.items.length; i++) {
msg.addReaction(Utils.CharsUnicodeArray[i]).queue();
}
@@ -68,12 +74,7 @@ public class MenuSystem {
}
currentMessage.editMessage(newMenu.makeMessage()).queue();
// Remove excess reactions
for (int y = newMenu.items.length; y < currentMessage.getReactions().size(); y++) {
currentMessage.getReactions().get(y).removeReaction().queue();
}
addReactions(currentMessage, newMenu);
updateReactions(currentMessage, newMenu);
menuHash.put(currentMessage, newMenu);
}
}

View File

@@ -16,7 +16,7 @@ public class Utils {
public static final String PlayUnicode = "\uD83D\uDD09";
public static final String SongUnicode = "\uD83C\uDFB5";
public static final String FolderUnicode = "\uD83D\uDCC1";
public static final String ReturnUnicode = "\u21A9";
public static final String ReturnUnicode = "\uD83D\uDCC2";
public static final String ProgressBarForeground = "\uD83D\uDFE9";
public static final String ProgressBarBackground = "\u2B1C";

View File

@@ -1,3 +1,3 @@
Manifest-Version: 1.0
Main-Class: fr.Skydust.JdrBot.JdrBot;
Main-Class: fr.Skydust.JdrBot.JdrBot