Cleaned up the command handler.
This commit is contained in:
parent
2db68aac61
commit
3288f7aa75
|
@ -36,7 +36,7 @@ Because the play command is defined as `play [URL]`, and the queue command is de
|
|||
|
||||
* `skip` - If a song is playing, the bot will skip it and play the next song in the queue.
|
||||
|
||||
* `queue [all]` - Lists up to the first 10 items on the queue, if no argument is given. If you add `all`, the bot will upload a list to [PasteBin](http://pastebin.com) of the entire queue, and give you
|
||||
* `queue [all]` - Lists up to the first 10 items on the queue, if no argument is given. If you add `all`, the bot will upload a list to [PasteBin](http://pastebin.com) of the entire queue, provided it is greater than 10 elements, and give you a link which expires in 10 minutes.
|
||||
|
||||
* `repeat [true|false]` - Sets the bot to repeat the playlist, as in once a song is removed from the queue to be played, it is added back to the end of the playlist.
|
||||
|
||||
|
|
25
pom.xml
25
pom.xml
|
@ -17,6 +17,31 @@
|
|||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>handiebot.HandieBot</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<packaging>jar</packaging>
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
Manifest-Version: 1.0
|
||||
Main-Class: handiebot.HandieBot
|
||||
|
|
@ -2,6 +2,7 @@ package handiebot;
|
|||
|
||||
import handiebot.command.CommandHandler;
|
||||
import handiebot.lavaplayer.MusicPlayer;
|
||||
import handiebot.utils.DisappearingMessage;
|
||||
import handiebot.view.BotLog;
|
||||
import handiebot.view.BotWindow;
|
||||
import handiebot.view.View;
|
||||
|
@ -10,6 +11,7 @@ import sx.blah.discord.api.IDiscordClient;
|
|||
import sx.blah.discord.api.events.EventSubscriber;
|
||||
import sx.blah.discord.handle.impl.events.ReadyEvent;
|
||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
||||
import sx.blah.discord.handle.obj.IGuild;
|
||||
import sx.blah.discord.util.DiscordException;
|
||||
import sx.blah.discord.util.RateLimitException;
|
||||
|
||||
|
@ -40,6 +42,9 @@ public class HandieBot {
|
|||
@EventSubscriber
|
||||
public void onReady(ReadyEvent event){
|
||||
log.log(BotLog.TYPE.INFO, "HandieBot initialized.");
|
||||
for (IGuild guild : client.getGuilds()){
|
||||
DisappearingMessage.deleteMessageAfter(5000, musicPlayer.getChatChannel(guild).sendMessage("HandieBot initialized."));
|
||||
}
|
||||
//client.changeAvatar(Image.forStream("png", getClass().getClassLoader().getResourceAsStream("avatarIcon.png")));
|
||||
}
|
||||
|
||||
|
|
|
@ -1,21 +1,25 @@
|
|||
package handiebot.command;
|
||||
|
||||
import com.sun.istack.internal.NotNull;
|
||||
import handiebot.command.commands.music.PlayCommand;
|
||||
import handiebot.command.commands.music.PlaylistCommand;
|
||||
import handiebot.command.commands.music.RepeatCommand;
|
||||
import handiebot.command.commands.music.ShuffleCommand;
|
||||
import handiebot.command.commands.HelpCommand;
|
||||
import handiebot.command.commands.InfoCommand;
|
||||
import handiebot.command.commands.SetPrefixCommand;
|
||||
import handiebot.command.commands.music.*;
|
||||
import handiebot.utils.DisappearingMessage;
|
||||
import handiebot.utils.FileUtil;
|
||||
import handiebot.view.BotLog;
|
||||
import handiebot.view.actions.QuitAction;
|
||||
import handiebot.view.actions.music.QueueListAction;
|
||||
import handiebot.view.actions.music.SkipAction;
|
||||
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
|
||||
import sx.blah.discord.handle.obj.*;
|
||||
import sx.blah.discord.util.EmbedBuilder;
|
||||
import sx.blah.discord.handle.obj.IChannel;
|
||||
import sx.blah.discord.handle.obj.IGuild;
|
||||
import sx.blah.discord.handle.obj.IMessage;
|
||||
import sx.blah.discord.handle.obj.IUser;
|
||||
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static handiebot.HandieBot.client;
|
||||
import static handiebot.HandieBot.log;
|
||||
|
||||
/**
|
||||
|
@ -24,7 +28,8 @@ import static handiebot.HandieBot.log;
|
|||
*/
|
||||
public class CommandHandler {
|
||||
|
||||
public static String PREFIX = "!";
|
||||
public static final String DEFAULT_PREFIX = "!";
|
||||
public static Map<IGuild, String> PREFIXES = loadGuildPrefixes();
|
||||
/**
|
||||
* Main method to handle user messages.
|
||||
* @param event The event generated by the message.
|
||||
|
@ -32,49 +37,53 @@ public class CommandHandler {
|
|||
public static void handleCommand(MessageReceivedEvent event){
|
||||
IMessage message = event.getMessage();
|
||||
IUser user = event.getAuthor();
|
||||
//Exit immediately if the user is a bot; avoids bot spam chat.
|
||||
if (user.isBot()){
|
||||
return;
|
||||
}
|
||||
IChannel channel = event.getChannel();
|
||||
IGuild guild = event.getGuild();
|
||||
//Check if the guild already has a prefix assigned, and if not, give it the default.
|
||||
if (!PREFIXES.containsKey(guild)){
|
||||
PREFIXES.put(guild, DEFAULT_PREFIX);
|
||||
}
|
||||
String command = extractCommand(message);
|
||||
String[] args = extractArgs(message);
|
||||
//Create a context to give to each command's execution, so it knows what channel to reply on, etc.
|
||||
CommandContext context = new CommandContext(user, channel, guild, args);
|
||||
if (guild != null && command != null){
|
||||
DisappearingMessage.deleteMessageAfter(1000, message);
|
||||
if (command.equals("play")){
|
||||
//Play or queue a song.
|
||||
new PlayCommand().execute(context);
|
||||
} else if (command.equals("skip") && args.length == 0){
|
||||
//Skip the current song.
|
||||
new SkipAction(guild).actionPerformed(null);
|
||||
} else if (command.equals("help")){
|
||||
//Send a PM to the user with help info.
|
||||
sendHelpInfo(user);//TODO finish the help command and fill in with new descriptions each time.
|
||||
} else if (command.equals("queue")){
|
||||
//Display the first few items of the queue.
|
||||
new QueueListAction(guild, (args.length == 1) && args[0].equals("all")).actionPerformed(null);
|
||||
} else if (command.equals("repeat")) {
|
||||
//Toggle repeat.
|
||||
new RepeatCommand().execute(context);
|
||||
} else if (command.equals("shuffle")){
|
||||
new ShuffleCommand().execute(context);
|
||||
} else if (command.equals("clear")){
|
||||
//TODO clear command.
|
||||
} else if (command.equals("quit")){
|
||||
//Quit the application.
|
||||
new QuitAction(guild).actionPerformed(null);
|
||||
} else if (command.equals("playlist")){
|
||||
//Do playlist actions.
|
||||
new PlaylistCommand().execute(context);
|
||||
} else if (command.equals("prefix") && args.length == 1){
|
||||
//Set the prefix to the first argument.
|
||||
if (args[0].length() != 1){
|
||||
new DisappearingMessage(channel, "You may only set the prefix to 1 character. To do otherwise is simply foolish.", 3000);
|
||||
} else {
|
||||
new DisappearingMessage(channel, "Command prefix set to "+PREFIX, 10000);
|
||||
log.log(BotLog.TYPE.INFO, guild, "Prefix set to "+PREFIX);
|
||||
setPrefix(args[0]);
|
||||
}
|
||||
} else {
|
||||
log.log(BotLog.TYPE.ERROR, guild, "Invalid command: "+command+" issued by "+user.getName());
|
||||
switch (command){
|
||||
//Music commands.
|
||||
case ("play"):
|
||||
new PlayCommand().execute(context);
|
||||
break;
|
||||
case ("skip"):
|
||||
new SkipCommand().execute(context);
|
||||
break;
|
||||
case ("queue"):
|
||||
new QueueCommand().execute(context);
|
||||
break;
|
||||
case ("repeat"):
|
||||
new RepeatCommand().execute(context);
|
||||
break;
|
||||
case ("shuffle"):
|
||||
new ShuffleCommand().execute(context);
|
||||
break;
|
||||
case ("playlist"):
|
||||
new PlaylistCommand().execute(context);
|
||||
break;
|
||||
//Other commands.
|
||||
case ("help"):
|
||||
new HelpCommand().execute(context);
|
||||
break;
|
||||
case ("info"):
|
||||
new InfoCommand().execute(context);
|
||||
break;
|
||||
case ("setprefix"):
|
||||
new SetPrefixCommand().execute(context);
|
||||
default:
|
||||
log.log(BotLog.TYPE.ERROR, guild, "Invalid command: "+command+" issued by "+user.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -86,8 +95,8 @@ public class CommandHandler {
|
|||
*/
|
||||
private static String extractCommand(IMessage message){
|
||||
String[] words = message.getContent().split(" ");
|
||||
if (words[0].startsWith(PREFIX)){
|
||||
return words[0].replaceFirst(PREFIX, "").toLowerCase();
|
||||
if (words[0].startsWith(PREFIXES.get(message.getGuild()))){
|
||||
return words[0].replaceFirst(PREFIXES.get(message.getGuild()), "").toLowerCase();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -97,10 +106,9 @@ public class CommandHandler {
|
|||
* @param message The message to parse.
|
||||
* @return A list of strings representing args.
|
||||
*/
|
||||
@NotNull
|
||||
private static String[] extractArgs(IMessage message){
|
||||
String[] words = message.getContent().split(" ");
|
||||
if (words[0].startsWith(PREFIX)){
|
||||
if (words[0].startsWith(PREFIXES.get(message.getGuild()))){
|
||||
String[] args = new String[words.length-1];
|
||||
for (int i = 0; i < words.length-1; i++){
|
||||
args[i] = words[i+1];
|
||||
|
@ -111,30 +119,34 @@ public class CommandHandler {
|
|||
}
|
||||
|
||||
/**
|
||||
* Method to send a useful list of commands to any user if they desire.
|
||||
* @param user The user to send the message to.
|
||||
* Loads a persistent list of prefixes for guilds from a file called guildPrefixes.txt
|
||||
* @return The mapping of guild to prefix.
|
||||
*/
|
||||
private static void sendHelpInfo(IUser user){
|
||||
IPrivateChannel pm = user.getOrCreatePMChannel();
|
||||
EmbedBuilder builder = new EmbedBuilder();
|
||||
|
||||
builder.withAuthorName("HandieBot");
|
||||
builder.withAuthorUrl("https://github.com/andrewlalis/HandieBot");
|
||||
builder.withAuthorIcon("https://github.com/andrewlalis/HandieBot/blob/master/src/main/resources/icon.png");
|
||||
|
||||
builder.withColor(new Color(255, 0, 0));
|
||||
builder.withDescription("I'm a discord bot that can manage music, as well as some other important functions which will be implemented later on. Some commands are shown below.");
|
||||
builder.appendField("Commands:", "play, skip, help", false);
|
||||
|
||||
pm.sendMessage(builder.build());
|
||||
private static Map<IGuild, String> loadGuildPrefixes(){
|
||||
File prefixFile = new File(FileUtil.getDataDirectory()+"guildPrefixes.txt");
|
||||
Map<IGuild, String> prefixes = new HashMap<>();
|
||||
if (prefixFile.exists()){
|
||||
List<String> lines = FileUtil.getLinesFromFile(prefixFile);
|
||||
for (String line : lines){
|
||||
String[] words = line.split(" / ");
|
||||
prefixes.put(client.getGuildByID(Long.parseLong(words[0])), words[1]);
|
||||
}
|
||||
}
|
||||
log.log(BotLog.TYPE.INFO, "Loaded prefixes.");
|
||||
return prefixes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the prefix used to identify commands.
|
||||
* @param prefix The prefix appended to the beginning of commands.
|
||||
* Saves the list of prefixes to a file.
|
||||
*/
|
||||
public static void setPrefix(String prefix){
|
||||
PREFIX = prefix;
|
||||
public static void saveGuildPrefixes(){
|
||||
File prefixFile = new File(FileUtil.getDataDirectory()+"guildPrefixes.txt");
|
||||
List<String> lines = new ArrayList<>();
|
||||
for (Map.Entry<IGuild, String> entry : PREFIXES.entrySet()){
|
||||
lines.add(Long.toString(entry.getKey().getLongID())+" / "+entry.getValue());
|
||||
}
|
||||
FileUtil.writeLinesToFile(lines, prefixFile);
|
||||
log.log(BotLog.TYPE.INFO, "Saved prefixes.");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
package handiebot.command.commands;
|
||||
|
||||
import handiebot.command.CommandContext;
|
||||
import handiebot.command.CommandHandler;
|
||||
import handiebot.command.types.ContextCommand;
|
||||
import handiebot.utils.DisappearingMessage;
|
||||
import sx.blah.discord.util.EmbedBuilder;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* @author Andrew Lalis
|
||||
* Command to display information about the bot, and some common commands.
|
||||
*/
|
||||
public class InfoCommand extends ContextCommand {
|
||||
|
||||
public InfoCommand() {
|
||||
super("info");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(CommandContext context) {
|
||||
EmbedBuilder builder = new EmbedBuilder();
|
||||
builder.withColor(new Color(255, 0, 0));
|
||||
builder.withDescription("HandieBot is a Discord bot created by Andrew Lalis. It can play music, manage playlists, and provide other assistance to users. Some useful commands are shown below.");
|
||||
builder.appendField("`"+ CommandHandler.PREFIXES.get(context.getGuild())+"help`", "Receive a message with a detailed list of all commands and how to use them.", false);
|
||||
builder.appendField("`"+CommandHandler.PREFIXES.get(context.getGuild())+"setprefix`", "Changed the prefix used at the beginning of each command.", false);
|
||||
DisappearingMessage.deleteMessageAfter(10000, context.getChannel().sendMessage(builder.build()));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package handiebot.command.commands;
|
||||
|
||||
import handiebot.command.CommandContext;
|
||||
import handiebot.command.CommandHandler;
|
||||
import handiebot.command.types.ContextCommand;
|
||||
import handiebot.utils.DisappearingMessage;
|
||||
import handiebot.view.BotLog;
|
||||
|
||||
import static handiebot.HandieBot.log;
|
||||
|
||||
/**
|
||||
* @author Andrew Lalis
|
||||
* Command to set the prefix used for a particular server.
|
||||
*/
|
||||
public class SetPrefixCommand extends ContextCommand {
|
||||
|
||||
public SetPrefixCommand() {
|
||||
super("setprefix");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(CommandContext context) {
|
||||
if (context.getArgs().length == 1) {
|
||||
CommandHandler.PREFIXES.put(context.getGuild(), context.getArgs()[0]);
|
||||
CommandHandler.saveGuildPrefixes();
|
||||
new DisappearingMessage(context.getChannel(), "Changed command prefix to \""+context.getArgs()[0]+"\"", 6000);
|
||||
log.log(BotLog.TYPE.INFO, "Changed command prefix to \""+context.getArgs()[0]+"\"");
|
||||
} else {
|
||||
new DisappearingMessage(context.getChannel(), "You must provide a new prefix.", 3000);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -86,7 +86,7 @@ public class PlaylistCommand extends ContextCommand {
|
|||
}
|
||||
playlist.save();
|
||||
log.log(BotLog.TYPE.INFO, "Created playlist: "+playlist.getName()+" with "+playlist.getTrackCount()+" new tracks.");
|
||||
new DisappearingMessage(context.getChannel(), "Your playlist *"+playlist.getName()+"* has been created.\nType `"+ CommandHandler.PREFIX+"playlist play "+playlist.getName()+"` to play it.", 5000);
|
||||
new DisappearingMessage(context.getChannel(), "Your playlist *"+playlist.getName()+"* has been created.\nType `"+ CommandHandler.PREFIXES.get(context.getGuild())+"playlist play "+playlist.getName()+"` to play it.", 5000);
|
||||
} else {
|
||||
new DisappearingMessage(context.getChannel(), "You must specify a name for the new playlist.", 3000);
|
||||
}
|
||||
|
@ -109,7 +109,7 @@ public class PlaylistCommand extends ContextCommand {
|
|||
new DisappearingMessage(context.getChannel(), "The playlist was not able to be deleted.", 3000);
|
||||
}
|
||||
} else {
|
||||
new DisappearingMessage(context.getChannel(), "The name you entered is not a playlist.\nType `"+CommandHandler.PREFIX+"playlist show` to list the playlists available.", 5000);
|
||||
new DisappearingMessage(context.getChannel(), "The name you entered is not a playlist.\nType `"+CommandHandler.PREFIXES.get(context.getGuild())+"playlist show` to list the playlists available.", 5000);
|
||||
}
|
||||
} else {
|
||||
new DisappearingMessage(context.getChannel(), "You must specify the name of a playlist to delete.", 3000);
|
||||
|
@ -128,7 +128,7 @@ public class PlaylistCommand extends ContextCommand {
|
|||
IMessage message = context.getChannel().sendMessage(playlist.toString());
|
||||
DisappearingMessage.deleteMessageAfter(6000, message);
|
||||
} else {
|
||||
new DisappearingMessage(context.getChannel(), "The playlist you specified does not exist.\nUse `"+CommandHandler.PREFIX+"playlist show` to view available playlists.", 5000);
|
||||
new DisappearingMessage(context.getChannel(), "The playlist you specified does not exist.\nUse `"+CommandHandler.PREFIXES.get(context.getGuild())+"playlist show` to view available playlists.", 5000);
|
||||
}
|
||||
} else {
|
||||
List<String> playlists = Playlist.getAvailablePlaylists();
|
||||
|
@ -163,7 +163,7 @@ public class PlaylistCommand extends ContextCommand {
|
|||
DisappearingMessage.deleteMessageAfter(6000, message);
|
||||
} else {
|
||||
if (context.getArgs().length == 1){
|
||||
new DisappearingMessage(context.getChannel(), "You must provide the name of a playlist to add a URL to.\nUse '"+CommandHandler.PREFIX+"playlist show` to view available playlists.", 5000);
|
||||
new DisappearingMessage(context.getChannel(), "You must provide the name of a playlist to add a URL to.\nUse '"+CommandHandler.PREFIXES.get(context.getGuild())+"playlist show` to view available playlists.", 5000);
|
||||
} else {
|
||||
new DisappearingMessage(context.getChannel(), "You must provide at least one URL to add.", 3000);
|
||||
}
|
||||
|
@ -187,7 +187,7 @@ public class PlaylistCommand extends ContextCommand {
|
|||
log.log(BotLog.TYPE.INFO, "Loaded playlist ["+playlist.getName()+"].");
|
||||
new DisappearingMessage(context.getChannel(), "Now playing from playlist: *"+playlist.getName()+"*.", 6000);
|
||||
} else {
|
||||
new DisappearingMessage(context.getChannel(), "You must provide a playlist to play.\nUse '"+CommandHandler.PREFIX+"playlist show` to view available playlists.", 3000);
|
||||
new DisappearingMessage(context.getChannel(), "You must provide a playlist to play.\nUse '"+CommandHandler.PREFIXES.get(context.getGuild())+"playlist show` to view available playlists.", 3000);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -265,7 +265,7 @@ public class PlaylistCommand extends ContextCommand {
|
|||
} catch (NumberFormatException e){
|
||||
new DisappearingMessage(context.getChannel(), "You must enter two integer values for the song indices.", 5000);
|
||||
}
|
||||
UnloadedTrack track = null;
|
||||
UnloadedTrack track;
|
||||
if (oldIndex > -1 && oldIndex < playlist.getTrackCount()){
|
||||
track = playlist.getTracks().remove(oldIndex);
|
||||
if (newIndex > -1 && newIndex <= playlist.getTrackCount()){
|
||||
|
|
|
@ -15,7 +15,7 @@ public class QueueCommand extends ContextCommand {
|
|||
|
||||
@Override
|
||||
public void execute(CommandContext context) {
|
||||
HandieBot.musicPlayer.showQueueList(context.getGuild(), (context.getArgs() != null && context.getArgs()[0].equals("all")));
|
||||
HandieBot.musicPlayer.showQueueList(context.getGuild(), (context.getArgs().length == 1 && context.getArgs()[0].equals("all")));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -150,7 +150,7 @@ public class MusicPlayer {
|
|||
public void showQueueList(IGuild guild, boolean showAll) {
|
||||
List<UnloadedTrack> tracks = getMusicManager(guild).scheduler.queueList();
|
||||
if (tracks.size() == 0) {
|
||||
new DisappearingMessage(getChatChannel(guild), "The queue is empty. Use **"+ CommandHandler.PREFIX+"play** *URL* to add songs.", 3000);
|
||||
new DisappearingMessage(getChatChannel(guild), "The queue is empty. Use **"+ CommandHandler.PREFIXES.get(guild)+"play** *URL* to add songs.", 3000);
|
||||
} else {
|
||||
if (tracks.size() > 10 && showAll) {
|
||||
String result = Pastebin.paste("Current queue for discord server: "+guild.getName()+".", getMusicManager(guild).scheduler.getActivePlaylist().toString());
|
||||
|
|
|
@ -174,8 +174,6 @@ public class TrackScheduler extends AudioEventAdapter {
|
|||
}
|
||||
if (endReason.mayStartNext){
|
||||
nextTrack();
|
||||
} else {
|
||||
log.log(BotLog.TYPE.MUSIC, this.guild, "Unable to go to the next track. Reason: "+endReason.name());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
package handiebot.utils;
|
||||
|
||||
import handiebot.view.BotLog;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static handiebot.HandieBot.log;
|
||||
|
||||
/**
|
||||
* @author Andrew Lalis
|
||||
* Class to simplify file operations.
|
||||
*/
|
||||
public class FileUtil {
|
||||
|
||||
public static String getDataDirectory(){
|
||||
return System.getProperty("user.home")+"/.handiebot/";
|
||||
}
|
||||
|
||||
public static List<String> getLinesFromFile(File file){
|
||||
try {
|
||||
return Files.readAllLines(file.toPath());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
public static void writeLinesToFile(List<String> lines, File file){
|
||||
if (lines.size() == 0){
|
||||
return;
|
||||
}
|
||||
if (!file.exists()){
|
||||
try {
|
||||
boolean success = file.createNewFile();
|
||||
if (!success) {
|
||||
log.log(BotLog.TYPE.ERROR, "Unable to create file. "+file.getAbsolutePath());
|
||||
return;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
log.log(BotLog.TYPE.ERROR, "Unable to create file. "+file.getAbsolutePath());
|
||||
return;
|
||||
}
|
||||
}
|
||||
try (PrintWriter writer = new PrintWriter(file)){
|
||||
while (lines.size() > 0) {
|
||||
writer.println(lines.remove(0));
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
log.log(BotLog.TYPE.ERROR, "Unable to write to file. "+file.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package handiebot.view;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* @author Andrew Lalis
|
||||
|
@ -10,12 +11,55 @@ public class View {
|
|||
private JTextPane outputArea;
|
||||
private JTextField commandField;
|
||||
|
||||
public View(){
|
||||
public View() {
|
||||
this.commandField.addKeyListener(new CommandLineListener());
|
||||
}
|
||||
|
||||
public JTextPane getOutputArea(){
|
||||
return this.outputArea;
|
||||
}
|
||||
public JTextPane getOutputArea() {
|
||||
return this.outputArea;
|
||||
}
|
||||
|
||||
{
|
||||
// GUI initializer generated by IntelliJ IDEA GUI Designer
|
||||
// >>> IMPORTANT!! <<<
|
||||
// DO NOT EDIT OR ADD ANY CODE HERE!
|
||||
$$$setupUI$$$();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method generated by IntelliJ IDEA GUI Designer
|
||||
* >>> IMPORTANT!! <<<
|
||||
* DO NOT edit this method OR call it in your code!
|
||||
*
|
||||
* @noinspection ALL
|
||||
*/
|
||||
private void $$$setupUI$$$() {
|
||||
mainPanel = new JPanel();
|
||||
mainPanel.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(2, 1, new Insets(0, 0, 0, 0), -1, -1));
|
||||
final JScrollPane scrollPane1 = new JScrollPane();
|
||||
scrollPane1.setFont(new Font("Consolas", scrollPane1.getFont().getStyle(), 12));
|
||||
mainPanel.add(scrollPane1, new com.intellij.uiDesigner.core.GridConstraints(0, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_BOTH, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
|
||||
outputArea = new JTextPane();
|
||||
outputArea.setEditable(false);
|
||||
outputArea.setFont(new Font("Consolas", outputArea.getFont().getStyle(), 12));
|
||||
outputArea.setSelectedTextColor(new Color(-1));
|
||||
outputArea.setSelectionColor(new Color(-9843846));
|
||||
outputArea.setText("");
|
||||
scrollPane1.setViewportView(outputArea);
|
||||
final JPanel panel1 = new JPanel();
|
||||
panel1.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
|
||||
mainPanel.add(panel1, new com.intellij.uiDesigner.core.GridConstraints(1, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_BOTH, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
|
||||
commandField = new JTextField();
|
||||
commandField.setFont(new Font("DialogInput", commandField.getFont().getStyle(), 16));
|
||||
commandField.setForeground(new Color(-16118999));
|
||||
commandField.setMargin(new Insets(0, 0, 0, 0));
|
||||
panel1.add(commandField, new com.intellij.uiDesigner.core.GridConstraints(0, 0, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
|
||||
}
|
||||
|
||||
/**
|
||||
* @noinspection ALL
|
||||
*/
|
||||
public JComponent $$$getRootComponent$$$() {
|
||||
return mainPanel;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue