Merge remote-tracking branch 'origin/master'

# Conflicts:
#	.idea/workspace.xml
#	src/main/java/me/savvy/rixa/modules/twitter/TwitterModule.java
This commit is contained in:
Venal 2017-07-11 23:27:32 -04:00
commit d86e8f2cdd
14 changed files with 796 additions and 506 deletions

1011
.idea/workspace.xml generated

File diff suppressed because it is too large Load Diff

View File

@ -4,6 +4,7 @@ import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import me.savvy.rixa.commands.admin.BatchMoveCommand; import me.savvy.rixa.commands.admin.BatchMoveCommand;
import me.savvy.rixa.commands.admin.ConfigCommand; import me.savvy.rixa.commands.admin.ConfigCommand;
import me.savvy.rixa.commands.admin.InviteCommand;
import me.savvy.rixa.commands.general.*; import me.savvy.rixa.commands.general.*;
import me.savvy.rixa.commands.handlers.CommandExec; import me.savvy.rixa.commands.handlers.CommandExec;
import me.savvy.rixa.commands.handlers.CommandHandler; import me.savvy.rixa.commands.handlers.CommandHandler;
@ -97,7 +98,7 @@ public class Rixa {
new InfoCommand(), new ServerInfoCommand(), new HelpCommand(), new InfoCommand(), new ServerInfoCommand(), new HelpCommand(),
new DeleteMessagesCommand(), new PingCommand(), new PurgeMessagesCommand(), new DeleteMessagesCommand(), new PingCommand(), new PurgeMessagesCommand(),
new BatchMoveCommand(), new MuteCommand(), new MusicCommand(), new BatchMoveCommand(), new MuteCommand(), new MusicCommand(),
new ConfigCommand(), new UrbanDictionaryCommand(), /*new InviteCommand()*/}); new ConfigCommand(), new UrbanDictionaryCommand(), new YoutubeCommand()});
register(new React[] {new HelpReaction(), new ConfigReaction()}); register(new React[] {new HelpReaction(), new ConfigReaction()});
data = new Data(DataType.SQL); data = new Data(DataType.SQL);
} }

View File

@ -33,6 +33,13 @@ public class ConfigCommand implements CommandExec {
"%pconfig set defaultRole <role> ; Set role to be assigned when a user joins the server!", "%pconfig set defaultRole <role> ; Set role to be assigned when a user joins the server!",
"%pconfig set muteRole <role> ; Set role to be assigned when a user is muted!", "%pconfig set muteRole <role> ; Set role to be assigned when a user is muted!",
"%pconfig set musicRole <musicRole> ; Set role required to use the music functions! (Not required)", "%pconfig set musicRole <musicRole> ; Set role required to use the music functions! (Not required)",
"%pconfig set twitterCKey <key> ; Set Twitter Consumer Key!",
"%pconfig set twitterCSecret <key> ; Set Twitter Consumer Secret!",
"%pconfig set twitterAToken <key> ; Set Twitter Access Key!",
"%pconfig set twitterASecret <key> ; Set Twitter Access Secret!",
"%config set twitterChannel ; Set the channel for Twitter feed updates!",
"%pconfig joinMessage <joinMessage> ; Set the greetings message for when a user joins the server!", "%pconfig joinMessage <joinMessage> ; Set the greetings message for when a user joins the server!",
"%pconfig quitMessage <quitMessage> ; Set the quit message for when a user leaves the server!", "%pconfig quitMessage <quitMessage> ; Set the quit message for when a user leaves the server!",
"%pconfig joinPm <joinPm> ; Set the message to be private messaged when a user joins!", "%pconfig joinPm <joinPm> ; Set the message to be private messaged when a user joins!",
@ -40,7 +47,8 @@ public class ConfigCommand implements CommandExec {
"%pconfig addPerm <role> <permission> ; Give a role permission to access a command!", "%pconfig addPerm <role> <permission> ; Give a role permission to access a command!",
"%pconfig removePerm <role> <permission> ; Remove a role's permission to access a command!", "%pconfig removePerm <role> <permission> ; Remove a role's permission to access a command!",
"%pconfig enable <module> ; Enabled a Rixa Module!", "%pconfig enable <module> ; Enabled a Rixa Module!",
"%pconfig disable <module> ; Disable a Rixa Module!"); "%pconfig disable <module> ; Disable a Rixa Module!"
);
} }
@Override @Override
@Command(mainCommand = "config", @Command(mainCommand = "config",

View File

@ -24,12 +24,14 @@ import me.savvy.rixa.guild.RixaManager;
import me.savvy.rixa.modules.music.MusicManager; import me.savvy.rixa.modules.music.MusicManager;
import me.savvy.rixa.modules.music.TrackScheduler; import me.savvy.rixa.modules.music.TrackScheduler;
import me.savvy.rixa.utils.MessageBuilder; import me.savvy.rixa.utils.MessageBuilder;
import me.savvy.rixa.utils.YoutubeSearch;
import net.dv8tion.jda.core.Permission; import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.*; import net.dv8tion.jda.core.entities.*;
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent; import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.core.exceptions.PermissionException; import net.dv8tion.jda.core.exceptions.PermissionException;
import java.awt.*; import java.awt.*;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -249,6 +251,18 @@ public class MusicCommand implements CommandExec {
new MessageBuilder(message[2] + " is not a valid integer. Try a number between 10 and 100.").setColor(event.getMember().getColor()).queue(event.getChannel()); new MessageBuilder(message[2] + " is not a valid integer. Try a number between 10 and 100.").setColor(event.getMember().getColor()).queue(event.getChannel());
} }
} }
} // music youtube <query
if(message.length >= 3) {
if(message[1].equalsIgnoreCase("youtube") || message[1].equalsIgnoreCase("yt") || message[1].equalsIgnoreCase("search")
|| message[1].equalsIgnoreCase("ytsearch")) {
String search = getMessage(message, 2);
try {
YoutubeSearch ytSearch = new YoutubeSearch(search);
loadAndPlay(mng, event.getChannel(), ytSearch.getUrl(0), false);
} catch (IOException e) {
e.printStackTrace();
}
}
} }
} }
@ -323,4 +337,12 @@ public class MusicCommand implements CommandExec {
private void sendHelp() { private void sendHelp() {
} }
private String getMessage(String[] messages, int argToBegin) {
StringBuilder builder = new StringBuilder();
for(int i = argToBegin; i < messages.length; i++) {
builder.append(messages[i]).append(" ");
}
return builder.toString().trim();
}
} }

View File

@ -16,7 +16,6 @@ import java.net.URLEncoder;
*/ */
public class UrbanDictionaryCommand implements CommandExec { public class UrbanDictionaryCommand implements CommandExec {
@Override @Override
@Command(mainCommand = "urbandictionary", @Command(mainCommand = "urbandictionary",
aliases = {"ud"}, aliases = {"ud"},

View File

@ -0,0 +1,41 @@
package me.savvy.rixa.commands.general;
import me.savvy.rixa.commands.handlers.Command;
import me.savvy.rixa.commands.handlers.CommandExec;
import me.savvy.rixa.utils.MessageBuilder;
import me.savvy.rixa.utils.YoutubeSearch;
import net.dv8tion.jda.core.entities.ChannelType;
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;
import java.io.IOException;
/**
* Created by savit on 7/11/2017.
*/
public class YoutubeCommand implements CommandExec {
@Override
@Command(mainCommand = "youtube",
aliases = {"yt"},
description = "Search youtube for music videos!",
channelType = ChannelType.TEXT)
public void execute(GuildMessageReceivedEvent event) {
String[] message = event.getMessage().getContent().split(" ");
String search = getMessage(message, 1);
try {
YoutubeSearch ytSearch = new YoutubeSearch(search);
new MessageBuilder(ytSearch.getUrl(0))
.setColor(event.getMember().getColor()).queue(event.getChannel());
} catch (IOException e) {
e.printStackTrace();
}
}
private String getMessage(String[] messages, int argToBegin) {
StringBuilder builder = new StringBuilder() ;
for(int i = argToBegin; i < messages.length; i++) {
builder.append(messages[i]).append(" ");
}
return builder.toString().trim();
}
}

View File

@ -44,7 +44,6 @@ public class MessageEvent {
CommandRegistrar cmd = CommandHandler.get(splitContent[0]); CommandRegistrar cmd = CommandHandler.get(splitContent[0]);
Method m = cmd.getMethod(); Method m = cmd.getMethod();
try { try {
Rixa.getInstance().getLogger().info("Invoking: " + cmd.getCommandAnnotation().mainCommand());
m.invoke(cmd.getExecutor(), event); m.invoke(cmd.getExecutor(), event);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -1,7 +1,5 @@
package me.savvy.rixa.modules.twitter; package me.savvy.rixa.modules.twitter;
import lombok.Getter;
import lombok.Setter;
import me.savvy.rixa.guild.RixaGuild; import me.savvy.rixa.guild.RixaGuild;
import twitter4j.*; import twitter4j.*;
import twitter4j.conf.ConfigurationBuilder; import twitter4j.conf.ConfigurationBuilder;
@ -11,18 +9,18 @@ import twitter4j.conf.ConfigurationBuilder;
*/ */
public class TwitterModule { public class TwitterModule {
@Getter
private final TwitterStream twitterStream; private final TwitterStream twitterStream;
@Getter
private ConfigurationBuilder configurationBuilder; private ConfigurationBuilder configurationBuilder;
@Getter
private Twitter twitter; private Twitter twitter;
@Getter
private TwitterFactory twitterFactory; private TwitterFactory twitterFactory;
@Getter @Setter private final RixaGuild rixaGuild;
private String consumerKey, consumerSecret, accessToken, accessTokenSecret; private String consumerKey;
private String consumerSecret;
private String accessToken;
private String accessTokenSecret;
public TwitterModule(RixaGuild rixaGuild, String consumerKey, String consumerSecret, String accessToken, String accessTokenSecret) { public TwitterModule(RixaGuild rixaGuild, String consumerKey, String consumerSecret, String accessToken, String accessTokenSecret) {
this.rixaGuild = rixaGuild;
this.consumerKey = consumerKey; this.consumerKey = consumerKey;
this.consumerSecret = consumerSecret; this.consumerSecret = consumerSecret;
this.accessToken = accessToken; this.accessToken = accessToken;
@ -37,4 +35,55 @@ public class TwitterModule {
twitter = twitterFactory.getInstance(); twitter = twitterFactory.getInstance();
} }
public ConfigurationBuilder getConfigurationBuilder() {
return configurationBuilder;
}
public TwitterFactory getTwitterFactory() {
return twitterFactory;
}
public Twitter getTwitter() {
return twitter;
}
public String getConsumerKey() {
return consumerKey;
}
public void setConsumerKey(String consumerKey) {
this.consumerKey = consumerKey;
}
public String getConsumerSecret() {
return consumerSecret;
}
public void setConsumerSecret(String consumerSecret) {
this.consumerSecret = consumerSecret;
}
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public String getAccessTokenSecret() {
return accessTokenSecret;
}
public void setAccessTokenSecret(String accessTokenSecret) {
this.accessTokenSecret = accessTokenSecret;
}
public TwitterStream getTwitterStream() {
return twitterStream;
}
public RixaGuild getRixaGuild() {
return rixaGuild;
}
} }

View File

@ -0,0 +1,42 @@
package me.savvy.rixa.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by savit on 7/11/2017.
*/
public class WebUtil {
public static String getWebPage(String url) throws IOException {
URL searchURL = new URL(url);
HttpURLConnection conn = (HttpURLConnection) searchURL.openConnection();
return getWebPage(conn);
}
public static String getWebPage(HttpURLConnection conn) throws IOException {
StringBuilder sb = new StringBuilder();
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
conn.setRequestProperty("Referer", "http://www.google.com");
int response = conn.getResponseCode();
if (response == 403) {
System.out.println("Quota Exceeded");
}
else if (response != 200) {
System.out.println("DEBUG: Response code: " + response);
}
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
sb.append(line).append("\n");
}
in.close();
return sb.toString();
}
}

View File

@ -1,7 +1,101 @@
package me.savvy.rixa.utils; package me.savvy.rixa.utils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
/** /**
* Created by savit on 7/11/2017. * Created by savit on 7/11/2017.
*/ */
public class YoutubeSearch { public class YoutubeSearch {
private static final String API_KEY;
private JSONArray items;
private final String YOUTUBE_WATCH_BASE_URL = "https://www.youtube.com/watch?v=";
static {
API_KEY = "AIzaSyD1wjRGbzKgvjqAU25pREy1dVio9WpcuS0";
}
public YoutubeSearch(String query) throws IOException {
try {
String url = "https://www.googleapis.com/youtube/v3/search?"
+ "q=" + URLEncoder.encode(query, "UTF-8")
+ "&part=id%2Csnippet"
+ "&safeSearch=none"
+ "&key=" + API_KEY;
search(url);
}
catch (UnsupportedEncodingException ignored) {}
}
public String getKind(int index) {
return items.getJSONObject(index).getString("kind");
}
public String getIdKind(int index) {
return items.getJSONObject(index).getJSONObject("id").getString("kind");
}
public String getVideoId(int index) {
return items.getJSONObject(index).getJSONObject("id").getString("videoId");
}
public String getPublishedTime(int index) {
return items.getJSONObject(index).getJSONObject("snippet").getString("publishedAt");
}
public String getChannelId(int index) {
return items.getJSONObject(index).getJSONObject("snippet").getString("channelId");
}
public String getTitle(int index) {
return items.getJSONObject(index).getJSONObject("snippet").getString("title");
}
public String getDescription(int index) {
return items.getJSONObject(index).getJSONObject("snippet").getString("description");
}
public String getChannelTitle(int index) {
return items.getJSONObject(index).getJSONObject("snippet").getString("channelTitle");
}
public String getLiveBroadcastContent(int index) {
return items.getJSONObject(index).getJSONObject("snippet").getString("liveBroadcastContent");
}
public String getThumbnailDefaultUrl(int index) {
return items.getJSONObject(index).getJSONObject("snippet").getJSONObject("default").getString("url");
}
public String getThumbnailMediumUrl(int index) {
return items.getJSONObject(index).getJSONObject("snippet").getJSONObject("medium").getString("url");
}
public String getThumbnailHighUrl(int index) {
return items.getJSONObject(index).getJSONObject("snippet").getJSONObject("high").getString("url");
}
public String getUrl(int index) {
return YOUTUBE_WATCH_BASE_URL + getVideoId(index);
}
private void search(String url) throws IOException {
String json = WebUtil.getWebPage(url);
JSONObject obj = new JSONObject(json);
try {
items = obj.getJSONArray("items");
}
catch (JSONException e) {
System.out.println("No search results found.");// throw new NotFoundException("No results found.");
}
}
} }