initial rewrite commit
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Copyright 2016 John Grosh <john.a.grosh@gmail.com>.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package me.jagrosh.jmusicbot.utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import net.dv8tion.jda.core.entities.Guild;
|
||||
import net.dv8tion.jda.core.entities.Role;
|
||||
import net.dv8tion.jda.core.entities.TextChannel;
|
||||
import net.dv8tion.jda.core.entities.VoiceChannel;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author John Grosh <john.a.grosh@gmail.com>
|
||||
*/
|
||||
public class FinderUtil {
|
||||
|
||||
public static List<TextChannel> findTextChannel(String query, Guild guild)
|
||||
{
|
||||
String id;
|
||||
if(query.matches("<#\\d+>"))
|
||||
{
|
||||
id = query.replaceAll("<#(\\d+)>", "$1");
|
||||
TextChannel tc = guild.getJDA().getTextChannelById(id);
|
||||
if(tc!=null && tc.getGuild().equals(guild))
|
||||
return Collections.singletonList(tc);
|
||||
}
|
||||
ArrayList<TextChannel> exact = new ArrayList<>();
|
||||
ArrayList<TextChannel> wrongcase = new ArrayList<>();
|
||||
ArrayList<TextChannel> startswith = new ArrayList<>();
|
||||
ArrayList<TextChannel> contains = new ArrayList<>();
|
||||
String lowerquery = query.toLowerCase();
|
||||
guild.getTextChannels().stream().forEach((tc) -> {
|
||||
if(tc.getName().equals(lowerquery))
|
||||
exact.add(tc);
|
||||
else if (tc.getName().equalsIgnoreCase(lowerquery) && exact.isEmpty())
|
||||
wrongcase.add(tc);
|
||||
else if (tc.getName().toLowerCase().startsWith(lowerquery) && wrongcase.isEmpty())
|
||||
startswith.add(tc);
|
||||
else if (tc.getName().toLowerCase().contains(lowerquery) && startswith.isEmpty())
|
||||
contains.add(tc);
|
||||
});
|
||||
if(!exact.isEmpty())
|
||||
return exact;
|
||||
if(!wrongcase.isEmpty())
|
||||
return wrongcase;
|
||||
if(!startswith.isEmpty())
|
||||
return startswith;
|
||||
return contains;
|
||||
}
|
||||
|
||||
public static List<VoiceChannel> findVoiceChannel(String query, Guild guild)
|
||||
{
|
||||
String id;
|
||||
if(query.matches("<#\\d+>"))
|
||||
{
|
||||
id = query.replaceAll("<#(\\d+)>", "$1");
|
||||
VoiceChannel tc = guild.getJDA().getVoiceChannelById(id);
|
||||
if(tc!=null && tc.getGuild().equals(guild))
|
||||
return Collections.singletonList(tc);
|
||||
}
|
||||
ArrayList<VoiceChannel> exact = new ArrayList<>();
|
||||
ArrayList<VoiceChannel> wrongcase = new ArrayList<>();
|
||||
ArrayList<VoiceChannel> startswith = new ArrayList<>();
|
||||
ArrayList<VoiceChannel> contains = new ArrayList<>();
|
||||
String lowerquery = query.toLowerCase();
|
||||
guild.getVoiceChannels().stream().forEach((tc) -> {
|
||||
if(tc.getName().equals(lowerquery))
|
||||
exact.add(tc);
|
||||
else if (tc.getName().equalsIgnoreCase(lowerquery) && exact.isEmpty())
|
||||
wrongcase.add(tc);
|
||||
else if (tc.getName().toLowerCase().startsWith(lowerquery) && wrongcase.isEmpty())
|
||||
startswith.add(tc);
|
||||
else if (tc.getName().toLowerCase().contains(lowerquery) && startswith.isEmpty())
|
||||
contains.add(tc);
|
||||
});
|
||||
if(!exact.isEmpty())
|
||||
return exact;
|
||||
if(!wrongcase.isEmpty())
|
||||
return wrongcase;
|
||||
if(!startswith.isEmpty())
|
||||
return startswith;
|
||||
return contains;
|
||||
}
|
||||
|
||||
public static List<Role> findRole(String query, Guild guild)
|
||||
{
|
||||
String id;
|
||||
if(query.matches("<@&\\d+>"))
|
||||
{
|
||||
id = query.replaceAll("<@&(\\d+)>", "$1");
|
||||
Role role = guild.getRoleById(id);
|
||||
if(role!=null)
|
||||
return Collections.singletonList(role);
|
||||
}
|
||||
if(query.matches("[Ii][Dd]\\s*:\\s*\\d+"))
|
||||
{
|
||||
id = query.replaceAll("[Ii][Dd]\\s*:\\s*(\\d+)", "$1");
|
||||
for(Role role: guild.getRoles())
|
||||
if(role.getId().equals(id))
|
||||
return Collections.singletonList(role);
|
||||
}
|
||||
ArrayList<Role> exact = new ArrayList<>();
|
||||
ArrayList<Role> wrongcase = new ArrayList<>();
|
||||
ArrayList<Role> startswith = new ArrayList<>();
|
||||
ArrayList<Role> contains = new ArrayList<>();
|
||||
String lowerQuery = query.toLowerCase();
|
||||
guild.getRoles().stream().forEach((role) -> {
|
||||
if(role.getName().equals(query))
|
||||
exact.add(role);
|
||||
else if (role.getName().equalsIgnoreCase(query) && exact.isEmpty())
|
||||
wrongcase.add(role);
|
||||
else if (role.getName().toLowerCase().startsWith(lowerQuery) && wrongcase.isEmpty())
|
||||
startswith.add(role);
|
||||
else if (role.getName().toLowerCase().contains(lowerQuery) && startswith.isEmpty())
|
||||
contains.add(role);
|
||||
});
|
||||
if(!exact.isEmpty())
|
||||
return exact;
|
||||
if(!wrongcase.isEmpty())
|
||||
return wrongcase;
|
||||
if(!startswith.isEmpty())
|
||||
return startswith;
|
||||
return contains;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright 2016 John Grosh <john.a.grosh@gmail.com>.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package me.jagrosh.jmusicbot.utils;
|
||||
|
||||
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
|
||||
import java.util.List;
|
||||
import me.jagrosh.jmusicbot.audio.AudioHandler;
|
||||
import net.dv8tion.jda.core.JDA;
|
||||
import net.dv8tion.jda.core.entities.Role;
|
||||
import net.dv8tion.jda.core.entities.TextChannel;
|
||||
import net.dv8tion.jda.core.entities.User;
|
||||
import net.dv8tion.jda.core.entities.VoiceChannel;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author John Grosh <john.a.grosh@gmail.com>
|
||||
*/
|
||||
public class FormatUtil {
|
||||
|
||||
public static String formatTime(long duration)
|
||||
{
|
||||
long seconds = Math.round(duration/1000.0);
|
||||
long hours = seconds/(60*60);
|
||||
seconds %= 60*60;
|
||||
long minutes = seconds/60;
|
||||
seconds %= 60;
|
||||
return (hours>0 ? hours+":" : "") + (minutes<10 ? "0"+minutes : minutes) + ":" + (seconds<10 ? "0"+seconds : seconds);
|
||||
}
|
||||
|
||||
public static String formattedAudio(AudioHandler handler, JDA jda, boolean inTopic)
|
||||
{
|
||||
if(handler==null || handler.getCurrentTrack()==null)
|
||||
{
|
||||
return "No music playing\n\u23F9 "+progressBar(-1)+" "+volumeIcon(handler.getPlayer().getVolume());
|
||||
}
|
||||
else
|
||||
{
|
||||
String userid = handler.getCurrentTrack().getIdentifier();
|
||||
User user = jda.getUserById(userid);
|
||||
AudioTrack track = handler.getCurrentTrack().getTrack();
|
||||
String title = track.getInfo().title;
|
||||
if(inTopic && title.length()>30)
|
||||
title = title.substring(0,27)+"...";
|
||||
double progress = (double)track.getPosition()/track.getDuration();
|
||||
String str = "**"+title+"** ["+(user==null||inTopic ? "<@"+userid+">" : user.getName())+"]\n\u25B6 "+progressBar(progress)
|
||||
+" "+(inTopic ? "" : "`")+"["+formatTime(track.getPosition()) + "/" + formatTime(track.getDuration())
|
||||
+"]` " +volumeIcon(handler.getPlayer().getVolume());
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
private static String progressBar(double percent)
|
||||
{
|
||||
String str = "";
|
||||
for(int i=0; i<10; i++)
|
||||
if(i == (int)(percent*10))
|
||||
str+="\uD83D\uDD18";
|
||||
else
|
||||
str+="▬";
|
||||
return str;
|
||||
}
|
||||
|
||||
public static String volumeIcon(int volume)
|
||||
{
|
||||
if(volume == 0)
|
||||
return "\uD83D\uDD07";
|
||||
if(volume < 30)
|
||||
return "\uD83D\uDD08";
|
||||
if(volume < 70)
|
||||
return "\uD83D\uDD09";
|
||||
return "\uD83D\uDD0A";
|
||||
}
|
||||
|
||||
public static String listOfTChannels(List<TextChannel> list, String query)
|
||||
{
|
||||
String out = " Multiple text channels found matching \""+query+"\":";
|
||||
for(int i=0; i<6 && i<list.size(); i++)
|
||||
out+="\n - "+list.get(i).getName()+" (<#"+list.get(i).getId()+">)";
|
||||
if(list.size()>6)
|
||||
out+="\n**And "+(list.size()-6)+" more...**";
|
||||
return out;
|
||||
}
|
||||
public static String listOfVChannels(List<VoiceChannel> list, String query)
|
||||
{
|
||||
String out = " Multiple voice channels found matching \""+query+"\":";
|
||||
for(int i=0; i<6 && i<list.size(); i++)
|
||||
out+="\n - "+list.get(i).getName()+" (ID:"+list.get(i).getId()+")";
|
||||
if(list.size()>6)
|
||||
out+="\n**And "+(list.size()-6)+" more...**";
|
||||
return out;
|
||||
}
|
||||
public static String listOfRoles(List<Role> list, String query)
|
||||
{
|
||||
String out = " Multiple text channels found matching \""+query+"\":";
|
||||
for(int i=0; i<6 && i<list.size(); i++)
|
||||
out+="\n - "+list.get(i).getName()+" (ID:"+list.get(i).getId()+")";
|
||||
if(list.size()>6)
|
||||
out+="\n**And "+(list.size()-6)+" more...**";
|
||||
return out;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2017 John Grosh <john.a.grosh@gmail.com>.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package me.jagrosh.jmusicbot.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author John Grosh <john.a.grosh@gmail.com>
|
||||
*/
|
||||
public class OtherUtil {
|
||||
|
||||
public static InputStream imageFromUrl(String url)
|
||||
{
|
||||
if(url==null)
|
||||
return null;
|
||||
try {
|
||||
URL u = new URL(url);
|
||||
URLConnection urlConnection = u.openConnection();
|
||||
urlConnection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36");
|
||||
return urlConnection.getInputStream();
|
||||
} catch(IOException|IllegalArgumentException e) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user