Skip to content
This repository has been archived by the owner on Jun 15, 2018. It is now read-only.

Commit

Permalink
updated library
Browse files Browse the repository at this point in the history
added ping command
google command can return multiple results
quote command will make the correct selection when quoting the last message in a different channel
  • Loading branch information
jagrosh committed Dec 10, 2016
1 parent ce5c6d1 commit e022c2f
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 12 deletions.
11 changes: 10 additions & 1 deletion src/jselfbot/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
*/
package jselfbot;

import java.util.List;
import jselfbot.commands.*;
import jselfbot.entities.Config;
import jselfbot.entities.Emojis;
import net.dv8tion.jda.core.EmbedBuilder;
import net.dv8tion.jda.core.MessageBuilder;
import net.dv8tion.jda.core.events.ReadyEvent;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;
import net.dv8tion.jda.core.utils.SimpleLog;

/**
*
Expand All @@ -47,13 +48,21 @@ public Bot(Config config)
new GoogleCmd(),
new ListCmd(emojis),
new MeCmd(),
new PingCmd(),
new PollCmd(),
new QuoteCmd(),
new SetCmd(emojis),
new TimeCmd(config.getZoneId())
};
}

@Override
public void onReady(ReadyEvent event) {
SimpleLog.getLog("Selfbot").info("Successfully logged in as "+event.getJDA().getSelfUser().getName()
+"#"+event.getJDA().getSelfUser().getDiscriminator()+"!\nYou are currently in "+event.getJDA().getGuilds().size()
+" guilds!\nType "+prefix+"help in Discord to get started!");
}

@Override
public void onMessageReceived(MessageReceivedEvent event) {
// the selfbot only replies to the user account it is running on
Expand Down
2 changes: 1 addition & 1 deletion src/jselfbot/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
* @author John Grosh (jagrosh)
*/
public class Constants {
public static final String VERSION = "0.1";
public static final String VERSION = "0.3";
public static final String FAILURE = "\u274C";
}
2 changes: 1 addition & 1 deletion src/jselfbot/commands/ClearCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected void execute(String args, MessageReceivedEvent event) {
event.getChannel().getHistory().retrievePast(num).queue(success -> {
List<Message> list = success.stream().filter(m -> event.getJDA().getSelfUser().equals(m.getAuthor())).collect(Collectors.toList());
list.forEach(m -> m.deleteMessage().queue());
tempReply("Deleted "+list.size()+" messages.", event);
tempReply("Deleting "+list.size()+" messages.", event);
}, failure -> {
tempReply("Failed retrieve messages.", event);
});
Expand Down
2 changes: 1 addition & 1 deletion src/jselfbot/commands/GameCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected void execute(String args, MessageReceivedEvent event) {
{
try {
event.getJDA().getPresence().setGame(Game.of(args));
result = "Game set to `"+args+"`";
result = "Game set to `"+args+"`. Note that it will appear to everyone else but will not show in your own client.";
} catch(Exception e) {
result = "Game could not be set to `"+args+"`";
}
Expand Down
32 changes: 27 additions & 5 deletions src/jselfbot/commands/GoogleCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,46 @@ public GoogleCmd()
google = new GoogleSearcher();
this.name = "google";
this.description = "search google";
this.arguments = "<query>";
this.arguments = "[num] <query>";
this.type = Type.EDIT_ORIGINAL;
}

@Override
protected void execute(String args, MessageReceivedEvent event) {
ArrayList<String> results = google.getDataFromGoogle(args);
String[] parts = args.split("\\s+", 2);
int num = 1;
String query;
if(parts.length >1 && parts[0].matches("\\d+"))
{
num = Integer.parseInt(parts[0]);
query = parts[1];
}
else
query = args;
if(num<1 || num>10)
{
tempReply("Can only search 1 to 10 results at once!", event);
return;
}
ArrayList<String> results = google.getDataFromGoogle(query);
if(results == null)
{
tempReply("Error searching", event);
tempReply("Error, could not search google", event);
}
else if(results.isEmpty())
{
tempReply("No results found for `"+args+"`", event);
tempReply("No results found for `"+query+"`", event);
}
else
{
reply("\uD83D\uDD0E "+results.get(0), event);
StringBuilder builder = new StringBuilder("`"+query+"` \uD83D\uDD0E "+results.get(0));
if(num>1 && results.size()>1)
{
builder.append("\nSee also:");
for(int i=1; i<num && i<results.size(); i++)
builder.append("\n<").append(results.get(i)).append(">");
}
reply(builder.toString(), event);
}
}

Expand Down
42 changes: 42 additions & 0 deletions src/jselfbot/commands/PingCmd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2016 John Grosh (jagrosh).
*
* 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 jselfbot.commands;

import java.time.temporal.ChronoUnit;
import jselfbot.Command;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;

/**
*
* @author John Grosh (jagrosh)
*/
public class PingCmd extends Command {

public PingCmd()
{
this.name = "ping";
this.description = "checks bot's connection";
this.type = Type.KEEP_AND_RESEND;
}

@Override
protected void execute(String args, MessageReceivedEvent event) {
event.getMessage().editMessage("Pinging...")
.queue(m -> m.editMessage("Ping: "+m.getCreationTime().until(m.getEditedTime(), ChronoUnit.MILLIS)+"ms")
.queue());
}

}
2 changes: 1 addition & 1 deletion src/jselfbot/commands/QuoteCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected void execute(String args, MessageReceivedEvent event) {
tempReply("No message history around `"+messageId+"`", event);
return;
}
Message msg = mh.getCachedHistory().size()==1 ? mh.getCachedHistory().get(0) : mh.getCachedHistory().get(1);
Message msg = mh.getCachedHistory().size()==1 || mh.getCachedHistory().size()==2 ? mh.getCachedHistory().get(0) : mh.getCachedHistory().get(1);
EmbedBuilder builder = new EmbedBuilder();
builder.setAuthor(msg.getAuthor().getName()+" #"+msg.getAuthor().getDiscriminator(), null,
msg.getAuthor().getAvatarUrl()==null ? msg.getAuthor().getDefaultAvatarUrl() : msg.getAuthor().getAvatarUrl());
Expand Down
4 changes: 2 additions & 2 deletions src/jselfbot/entities/GoogleSearcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class GoogleSearcher {
public ArrayList<String> getDataFromGoogle(String query) {
String request;
try {
request = "https://www.google.com/search?q=" + URLEncoder.encode(query, "UTF-8") + "&num=10";
request = "https://www.google.com/search?q=" + URLEncoder.encode(query, "UTF-8") + "&num=20";
} catch (UnsupportedEncodingException ex) {
System.err.println(ex);
return null;
Expand All @@ -57,7 +57,7 @@ public ArrayList<String> getDataFromGoogle(String query) {
try {
//result.add(temp.substring(7,temp.indexOf("&sa="))+"\n");
String rslt = URLDecoder.decode(temp.substring(7,temp.indexOf("&sa=")),"UTF-8");
if(!rslt.equals("/settings/ads/preferences?hl=en"))
if(!rslt.equals("/settings/ads/preferences?hl=en") && !rslt.startsWith("http://webcache.googleusercontent.com"))
result.add(rslt);
} catch (UnsupportedEncodingException ex) {
}
Expand Down

0 comments on commit e022c2f

Please sign in to comment.