Compare commits

..

4 Commits

4 changed files with 137 additions and 22 deletions

View File

@ -7,6 +7,8 @@ package moe.nekojimi.chords;
import java.io.File; import java.io.File;
import java.net.URL; import java.net.URL;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*; import java.util.*;
import javax.security.auth.login.LoginException; import javax.security.auth.login.LoginException;
import moe.nekojimi.chords.commands.*; import moe.nekojimi.chords.commands.*;
@ -16,6 +18,7 @@ import moe.nekojimi.musicsearcher.providers.Searcher;
import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceLeaveEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.managers.AudioManager; import net.dv8tion.jda.api.managers.AudioManager;
@ -77,6 +80,7 @@ public class Main extends ListenerAdapter
public Main() public Main()
{ {
log("INFO", "Starting up...");
downloader = new Downloader(); downloader = new Downloader();
downloader.setMessageHandler((Song song, Exception ex) -> downloader.setMessageHandler((Song song, Exception ex) ->
{ {
@ -85,8 +89,10 @@ public class Main extends ListenerAdapter
if (channel != null) if (channel != null)
if (ex == null) if (ex == null)
if (song.getLocation() != null) if (song.getLocation() != null)
{
channel.sendMessage(/*bracketNo + */"Finished downloading " + song + " for " + song.getRequestedBy() + ", added to queue!").queue(); channel.sendMessage(/*bracketNo + */"Finished downloading " + song + " for " + song.getRequestedBy() + ", added to queue!").queue();
else log("DOWN", "Downloaded " + song);
} else
{ {
Format format = song.getBestFormat(); Format format = song.getBestFormat();
String formatDetails = ""; String formatDetails = "";
@ -95,9 +101,13 @@ public class Main extends ListenerAdapter
formatDetails = " (" + format.getBitrate() / 1000 + "k, " + String.format("%.2f", format.getSize() / (1024.0 * 1024.0)) + "MiB)"; formatDetails = " (" + format.getBitrate() / 1000 + "k, " + String.format("%.2f", format.getSize() / (1024.0 * 1024.0)) + "MiB)";
} }
channel.sendMessage(/*bracketNo + */"Now downloading " + song + formatDetails + " for " + song.getRequestedBy() + " ...").queue(); channel.sendMessage(/*bracketNo + */"Now downloading " + song + formatDetails + " for " + song.getRequestedBy() + " ...").queue();
log("DOWN", "Downloading " + song + "...");
} }
else else
{
channel.sendMessage(/*bracketNo + */"Failed to download " + song + " for " + song.getRequestedBy() + "! Reason: " + ex.getMessage()).queue(); channel.sendMessage(/*bracketNo + */"Failed to download " + song + " for " + song.getRequestedBy() + "! Reason: " + ex.getMessage()).queue();
log("DOWN", "Failed to download " + song + "! Reason: " + ex.getMessage());
}
}); });
searcher = MetaSearcher.loadYAML(new File("searchproviders.yml")); searcher = MetaSearcher.loadYAML(new File("searchproviders.yml"));
@ -111,6 +121,8 @@ public class Main extends ListenerAdapter
addCommand(new SkipCommand(this)); addCommand(new SkipCommand(this));
helpCommand = new HelpCommand(this); helpCommand = new HelpCommand(this);
addCommand(helpCommand); addCommand(helpCommand);
log("INFO", "Started OK!");
} }
private void addCommand(Command command) private void addCommand(Command command)
@ -123,6 +135,19 @@ public class Main extends ListenerAdapter
this.jda = jda; this.jda = jda;
} }
@Override
public void onGuildVoiceLeave(GuildVoiceLeaveEvent event)
{
if (this.currentVoiceChannel == null)
return;
final VoiceChannel channelLeft = event.getChannelLeft();
if (channelLeft.getMembers().isEmpty())
if (channelLeft == currentVoiceChannel)
disconnect();
}
@Override @Override
public void onGuildMessageReceived(GuildMessageReceivedEvent event) public void onGuildMessageReceived(GuildMessageReceivedEvent event)
{ {
@ -139,6 +164,8 @@ public class Main extends ListenerAdapter
if (author.isBot()) if (author.isBot())
return; return;
log("MESG", "G:" + guild.getName() + " A:" + author.getName() + " C:" + content);
try try
{ {
String[] split = content.split("\\s+", 2); String[] split = content.split("\\s+", 2);
@ -162,6 +189,7 @@ public class Main extends ListenerAdapter
} catch (Exception ex) } catch (Exception ex)
{ {
event.getChannel().sendMessage("Error in command! " + ex.getMessage()).queue(); event.getChannel().sendMessage("Error in command! " + ex.getMessage()).queue();
log("UERR", "Command error:" + ex.getMessage());
} }
} }
@ -223,6 +251,11 @@ public class Main extends ListenerAdapter
} }
} }
public void log(String type, String message)
{
System.out.println(type + " " + LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME) + "\t" + message);
}
public MusicHandler getMusicHandler() public MusicHandler getMusicHandler()
{ {
return musicHandler; return musicHandler;

View File

@ -0,0 +1,64 @@
/*
* Copyright (C) 2022 jimj316
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package moe.nekojimi.chords;
import com.amihaiemil.eoyaml.YamlMapping;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author jimj316
*/
public class Playlist
{
private final String name;
private final List<Song> songs = new ArrayList<>();
public Playlist(String name)
{
this.name = name;
}
public YamlMapping toYaml()
{
throw new UnsupportedOperationException("Not supported yet.");
}
public static Playlist fromYaml(YamlMapping yaml)
{
throw new UnsupportedOperationException("Not supported yet.");
}
public void addSong(Song song)
{
song.setKept(true);
songs.add(song);
}
public String getName()
{
return name;
}
public List<Song> getSongs()
{
return songs;
}
}

View File

@ -75,6 +75,11 @@ public class Song
return song; return song;
} }
public boolean isDownloaded()
{
return (location != null && location.exists() && location.canRead());
}
public String getTitle() public String getTitle()
{ {
return title; return title;
@ -191,5 +196,4 @@ public class Song
return formats.get(0); return formats.get(0);
} }
} }

View File

@ -6,11 +6,9 @@
package moe.nekojimi.chords.commands; package moe.nekojimi.chords.commands;
import java.util.List; import java.util.List;
import java.util.Optional;
import moe.nekojimi.chords.Main; import moe.nekojimi.chords.Main;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.VoiceChannel;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
public class JoinCommand extends Command public class JoinCommand extends Command
@ -24,24 +22,40 @@ public class JoinCommand extends Command
@Override @Override
public void call(GuildMessageReceivedEvent event, List<String> args) public void call(GuildMessageReceivedEvent event, List<String> args)
{ {
String arg0 = args.get(0);
Guild guild = event.getGuild();
boolean isNumber = arg0.matches("\\d+"); // This is a regular expression that ensures the input consists of digits
VoiceChannel channel = null;
if (isNumber) // The input is an id?
channel = guild.getVoiceChannelById(arg0);
if (channel == null) // Then the input must be a name?
{
List<VoiceChannel> channels = guild.getVoiceChannelsByName(arg0, true);
if (!channels.isEmpty()) // Make sure we found at least one exact match
channel = channels.get(0); // We found a channel! This cannot be null.
}
TextChannel textChannel = event.getChannel(); TextChannel textChannel = event.getChannel();
if (channel == null) // I have no idea what you want mr user VoiceChannel channel = null;
if (args.isEmpty() || args.get(0).isBlank())
{ {
onUnknownChannel(textChannel, arg0); // Let the user know about our failure GuildVoiceState voiceState = event.getMember().getVoiceState();
return; if (voiceState != null && voiceState.inVoiceChannel())
channel = voiceState.getChannel();
else
{
Guild guild = event.getMessage().getGuild();
List<VoiceChannel> voiceChannels = guild.getVoiceChannels();
Optional<VoiceChannel> findFirst = voiceChannels.stream().filter((t) -> !t.getMembers().isEmpty()).findFirst();
channel = findFirst.orElseThrow(() -> new RuntimeException("Cannot find any voice channels with people in!"));
}
} else
{
String arg0 = args.get(0);
Guild guild = event.getGuild();
boolean isNumber = arg0.matches("\\d+"); // This is a regular expression that ensures the input consists of digits
if (isNumber) // The input is an id?
channel = guild.getVoiceChannelById(arg0);
if (channel == null) // Then the input must be a name?
{
List<VoiceChannel> channels = guild.getVoiceChannelsByName(arg0, true);
if (!channels.isEmpty()) // Make sure we found at least one exact match
channel = channels.get(0); // We found a channel! This cannot be null.
}
if (channel == null) // I have no idea what you want mr user
{
onUnknownChannel(textChannel, arg0); // Let the user know about our failure
return;
}
} }
bot.connectTo(channel); // We found a channel to connect to! bot.connectTo(channel); // We found a channel to connect to!
onConnecting(channel, textChannel); // Let the user know, we were successful! onConnecting(channel, textChannel); // Let the user know, we were successful!