Initial commit with base

This commit is contained in:
ZtereoHYPE 2021-11-03 18:01:46 +01:00
commit 6d7addc22d
20 changed files with 810 additions and 0 deletions

View file

@ -0,0 +1,9 @@
package codes.ztereohype.ztereomusic.command;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
// TODO: add categories/groups and perms but in a smart way pls
public interface Command {
CommandMeta getMeta();
void execute(MessageReceivedEvent messageRecievedEvent, String[] args);
}

View file

@ -0,0 +1,20 @@
package codes.ztereohype.ztereomusic.command;
import lombok.Getter;
@Getter
public class CommandMeta {
private final String name;
private final String description;
private final String[] aliases;
private final boolean isNsfw;
private final boolean isHidden;
public CommandMeta(String name, String description, String[] aliases, boolean isNsfw, boolean isHidden) {
this.name = name;
this.description = description;
this.aliases = aliases;
this.isNsfw = isNsfw;
this.isHidden = isHidden;
}
}

View file

@ -0,0 +1,24 @@
package codes.ztereohype.ztereomusic.command.commands;
import codes.ztereohype.ztereomusic.command.Command;
import codes.ztereohype.ztereomusic.command.CommandMeta;
import lombok.Getter;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import java.util.List;
public class Ping implements Command {
CommandMeta meta = new CommandMeta("ping", "A command to get pinged", new String[]{"pong", "pog"}, false, false);
private List<String> list;
@Override
public CommandMeta getMeta() {
return this.meta;
}
public void execute(MessageReceivedEvent messageEvent, String[] args) {
messageEvent.getMessage().reply("get ping'd lolmao").queue();
list.add("owo");
}
}

View file

@ -0,0 +1,130 @@
package codes.ztereohype.ztereomusic.command.commands;
import codes.ztereohype.ztereomusic.Bot;
import codes.ztereohype.ztereomusic.audio.AudioPlayerSendHandler;
import codes.ztereohype.ztereomusic.audio.TrackScheduer;
import codes.ztereohype.ztereomusic.command.Command;
import codes.ztereohype.ztereomusic.command.CommandMeta;
import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
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.api.entities.*;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.managers.AudioManager;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Playtest implements Command {
CommandMeta meta = new CommandMeta("playtest", "A command to test music commands", new String[]{"play"}, false, false);
@Override
public CommandMeta getMeta() {
return this.meta;
}
public void execute(MessageReceivedEvent messageEvent, String[] args) {
Member author = Objects.requireNonNull(messageEvent.getMember());
if (author.getVoiceState() == null) {
messageEvent.getChannel().sendMessage("I was unable to access your information... strange...").queue();
return;
}
if (!author.getVoiceState().inVoiceChannel()) {
messageEvent.getMessage().reply("You are not in a voice channel!").queue();
return;
}
Guild guild = messageEvent.getGuild();
VoiceChannel voiceChannel = author.getVoiceState().getChannel();
MessageChannel messageChannel = messageEvent.getChannel();
AudioManager manager = guild.getAudioManager();
AudioPlayerManager playerManager = Bot.playerManager;
// check if args merged are/have url, if so try to feed it into lava, else try to youtube api the fuck out of it.
String mergedArgs = String.join(" ", args);
//todo find better one
Pattern urlPattern = Pattern.compile("^(http|https)://([a-z]+\\.[a-z]+)+/\\S+$", Pattern.CASE_INSENSITIVE);
Matcher matchedUrls = urlPattern.matcher(mergedArgs);
boolean urlFound = matchedUrls.find();
String identifier;
if (!urlFound) {
// youtube api shit
messageEvent.getMessage().reply("please send a link").queue();
return;
} else {
// set identifier to the parsed url
identifier = mergedArgs;
}
/* Generally you would want to create a player per every
different target you might want to separately stream audio to.
It is totally fine to create them even if they are unlikely to be used,
as they do not use any resources on their own without an active track. */
AudioPlayer player;
TrackScheduer trackScheduler;
boolean isInVC = manager.isConnected();
boolean isInSameVC = isInVC && Objects.equals(manager.getConnectedChannel(), voiceChannel);
if (isInSameVC && Bot.trackScheduerMap.containsKey(voiceChannel)) {
System.out.println("Found a trackScheduler for this VC already! reusing...");
trackScheduler = Bot.trackScheduerMap.get(voiceChannel);
player = trackScheduler.getPlayer();
player.addListener(trackScheduler);
} else {
// Maybe we don't wanna clear the whole queue when he gets kicked out?
// idk
if (Bot.trackScheduerMap.containsKey(voiceChannel)) {
System.out.println("Found old trackScheduler for this channel. Cleaning it up...");
Bot.trackScheduerMap.remove(voiceChannel);
}
System.out.println("Creating a new trackScheduler...");
player = playerManager.createPlayer();
trackScheduler = new TrackScheduer(player, messageChannel);
player.addListener(trackScheduler);
manager.setSendingHandler(new AudioPlayerSendHandler(player));
manager.openAudioConnection(voiceChannel);
Bot.trackScheduerMap.put(voiceChannel, trackScheduler);
}
playerManager.loadItem(identifier, new AudioLoadResultHandler() {
@Override
public void trackLoaded(AudioTrack track) {
trackScheduler.queue(track);
}
@Override
public void playlistLoaded(AudioPlaylist playlist) {
for (AudioTrack track : playlist.getTracks()) {
trackScheduler.queue(track);
}
}
@Override
public void noMatches() {
messageEvent.getMessage().reply("I found no matches for that song!").queue();
}
@Override
public void loadFailed(FriendlyException throwable) {
messageEvent.getMessage().reply("everything blew up and died. i'm sorry.").queue();
}
});
}
}