Skip to content

Instantly share code, notes, and snippets.

@azenla
Last active December 15, 2015 14:19
Show Gist options
  • Save azenla/5273385 to your computer and use it in GitHub Desktop.
Save azenla/5273385 to your computer and use it in GitHub Desktop.
OtherCommands
package kaendfinger.kenbot.module.text;
import com.google.gson.Gson;
import kaendfinger.kenbot.api.registry.Command;
import kaendfinger.kenbot.api.registry.EventListener;
import org.pircbotx.Channel;
import org.pircbotx.hooks.events.JoinEvent;
import org.pircbotx.hooks.events.MessageEvent;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
public class OtherCommands {
public static HashMap<Channel, String> channelsAutoVoicing;
@Command(help = "Search Google", usage = "google <query>", maxArgs = -1, minArgs = 1)
public static void google(MessageEvent event, String[] args) throws Exception {
StringBuilder builder = new StringBuilder();
for (int i = 0 ; i<args.length ; i++) {
if (i + 1==args.length) {
builder.append(args[i]);
} else {
builder.append(args[i] + " ");
}
}
String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
String charset = "UTF-8";
String queryString = builder.toString();
URL url = new URL(google + URLEncoder.encode(queryString, charset));
Reader reader = new InputStreamReader(url.openStream(), charset);
GoogleResults results = new Gson().fromJson(reader, GoogleResults.class);
String pageURL = results.getResponseData().getResults().get(0).getUrl();
String pageTitle = results.getResponseData().getResults().get(0).getTitle();
String out = "$> " + pageTitle + " | " + pageURL;
if (!ModuleTextCommands.useNoticeForGoogle) {
event.getChannel().sendMessage(out);
} else {
event.getBot().sendNotice(event.getUser(), out);
}
}
@Command(help = "Get a URL's page title", usage = "pagetitle <URL>", minArgs = 1, maxArgs = 1)
public static void pagetitle(MessageEvent event, String[] args) throws Exception {
String url = args[0];
event.getBot().sendNotice(event.getUser(), TitleExtractor.getPageTitle(url));
}
@Command(help = "Send a RAW line.", usage = "raw <LINE>", maxArgs = -1, minArgs = 1)
public static void raw(MessageEvent event, String[] args) {
StringBuilder builder = new StringBuilder();
for (int i = 0 ; i<args.length ; i++) {
if (i + 1==args.length) {
builder.append(args[i]);
} else {
builder.append(args[i] + " ");
}
}
event.getBot().sendRawLineNow(builder.toString());
}
@Command(help = "Start Auto-Voicing", usage = "autovoice")
public static void autovoice(MessageEvent event) throws Exception {
if (channelsAutoVoicing.containsKey(event.getChannel())) {
event.getBot().sendNotice(event.getUser(), "This channel is already auto-voicing! Use the command
\"autovoiceoff\" to turn auto-voicing off.");
return;
}
if (!event.getChannel().isOp(event.getUser())) {
event.getBot().sendNotice(event.getUser(), "Sorry. You are not allowed to start AutoVoicing!");
return;
}
event.getBot().sendNotice(event.getChannel(), "This channel will now Auto-Voice users who join!");
channelsAutoVoicing.put(event.getChannel(), "true");
}
@Command(help = "Stop Auto-Voicer", usage = "stopautovoice")
public static void stopautovoice(MessageEvent event) {
if (channelsAutoVoicing.containsKey(event.getChannel())) {
channelsAutoVoicing.put(event.getChannel(), "false");
} else {
event.getBot().sendNotice(event.getUser(), "This channel is not auto-voicing!");
}
}
@EventListener(name = "AutoVoicerJoin", event="JoinEvent")
public static void autoVoicerJoinHandler(JoinEvent event) throws Exception {
if (!channelsAutoVoicing.get(event.getChannel()).equals("true")) {
return;
}
event.getChannel().voice(event.getUser());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment