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

Commit

Permalink
removed dep and created util for same solution
Browse files Browse the repository at this point in the history
can quote from other DMs now
bumped version number
  • Loading branch information
jagrosh committed Aug 18, 2017
1 parent 5906eff commit 4fff4d8
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
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.9";
public static final String VERSION = "1.0";
public static final String FAILURE = "\u274C";
}
19 changes: 17 additions & 2 deletions src/jselfbot/commands/QuoteCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

import jselfbot.Command;
import net.dv8tion.jda.core.EmbedBuilder;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.MessageChannel;
import net.dv8tion.jda.core.entities.User;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;

/**
Expand All @@ -44,7 +46,7 @@ protected void execute(String args, MessageReceivedEvent event) {
tempReply("`"+id1+"` is not a valid message or channel ID", event);
return;
}
MessageChannel channel = event.getJDA().getTextChannelById(id1);
MessageChannel channel = resolveChannel(id1, event.getJDA());
String messageId;
String followingText = null;
if(channel != null) //channel id found, need a message id now
Expand All @@ -70,7 +72,7 @@ protected void execute(String args, MessageReceivedEvent event) {
{
String[] parts2 = parts[1].split("\\s+", 2);
String id2 = parts2[0].replaceAll("<#(\\d+)>", "$1");
channel = event.getJDA().getTextChannelById(id2);
channel = resolveChannel(id2, event.getJDA());
if(channel == null)
followingText = parts[1];
else
Expand Down Expand Up @@ -123,6 +125,19 @@ protected void execute(String args, MessageReceivedEvent event) {
}
}

private static MessageChannel resolveChannel(String id, JDA jda)
{
MessageChannel mc = jda.getTextChannelById(id);
if(mc!=null)
return mc;
mc = jda.getPrivateChannelById(id);
if(mc!=null)
return mc;
User u = jda.getUserById(id);
if(u!=null)
return u.openPrivateChannel().complete();
return null;
}

private static boolean isId(String id)
{
Expand Down
4 changes: 2 additions & 2 deletions src/jselfbot/entities/Emojis.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import java.nio.file.Paths;
import java.util.Collection;
import java.util.HashMap;
import jselfbot.utils.FormatUtil;
import net.dv8tion.jda.core.utils.SimpleLog;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;

Expand All @@ -43,7 +43,7 @@ public Emojis()
emojis = new HashMap<>();
JSONObject obj;
try {
obj = new JSONObject(StringUtils.join(Files.readAllLines(Paths.get(FILENAME)), "\n"));
obj = new JSONObject(FormatUtil.join(Files.readAllLines(Paths.get(FILENAME)), "\n"));
obj.keySet().stream().forEach(name -> emojis.put(name.toLowerCase(), obj.getString(name)));
LOG.info("Successfully loaded "+emojis.keySet().size()+" custom emojis!");
} catch(IOException e) {
Expand Down
6 changes: 2 additions & 4 deletions src/jselfbot/entities/Todolist.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import jselfbot.utils.FormatUtil;
import net.dv8tion.jda.core.utils.SimpleLog;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -47,7 +45,7 @@ public Todolist()
todolist = new ArrayList<>();
JSONArray array;
try {
array = new JSONArray(StringUtils.join(Files.readAllLines(Paths.get(FILENAME)), "\n"));
array = new JSONArray(FormatUtil.join(Files.readAllLines(Paths.get(FILENAME)), "\n"));
for(int i=0; i<array.length(); i++)
todolist.add(new TodoItem(array.getJSONObject(i)));
LOG.info("Successfully loaded "+todolist.size()+" todo entries!");
Expand Down
26 changes: 26 additions & 0 deletions src/jselfbot/utils/FormatUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package jselfbot.utils;

import java.util.List;

/**
*
* @author John Grosh ([email protected])
*/
public class FormatUtil
{

public static String join(List<String> strings, String joiner)
{
if(strings.isEmpty())
return "";
StringBuilder sb = new StringBuilder(strings.get(0));
for(int i=1; i<strings.size(); i++)
sb.append(joiner).append(strings.get(i));
return sb.toString();
}
}

0 comments on commit 4fff4d8

Please sign in to comment.