/
HelpCommand.java
146 lines (131 loc) · 5.62 KB
/
HelpCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* Copyright 2015-2016 Austin Keener
*
* 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 net.dv8tion.discord.commands;
import net.dv8tion.jda.core.MessageBuilder;
import net.dv8tion.jda.core.entities.ChannelType;
import net.dv8tion.jda.core.entities.PrivateChannel;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.TreeMap;
public class HelpCommand extends Command
{
private static final String NO_NAME = "No name provided for this command. Sorry!";
private static final String NO_DESCRIPTION = "No description has been provided for this command. Sorry!";
private static final String NO_USAGE = "No usage instructions have been provided for this command. Sorry!";
private TreeMap<String, Command> commands;
public HelpCommand()
{
commands = new TreeMap<>();
}
public Command registerCommand(Command command)
{
commands.put(command.getAliases().get(0), command);
return command;
}
@Override
public void onCommand(MessageReceivedEvent e, String[] args)
{
if(!e.isFromType(ChannelType.PRIVATE))
{
e.getTextChannel().sendMessage(new MessageBuilder()
.append(e.getAuthor())
.append(": Help information was sent as a private message.")
.build()).queue();
}
sendPrivate(e.getAuthor().openPrivateChannel().complete(), args);
}
@Override
public List<String> getAliases()
{
return Arrays.asList(".help", ".commands");
}
@Override
public String getDescription()
{
return "Command that helps use all other commands!";
}
@Override
public String getName()
{
return "Help Command";
}
@Override
public List<String> getUsageInstructions()
{
return Collections.singletonList(
".help **OR** .help *<command>*\n"
+ ".help - returns the list of commands along with a simple description of each.\n"
+ ".help <command> - returns the name, description, aliases and usage information of a command.\n"
+ " - This can use the aliases of a command as input as well.\n"
+ "__Example:__ .help ann");
}
private void sendPrivate(PrivateChannel channel, String[] args)
{
if (args.length < 2)
{
StringBuilder s = new StringBuilder();
for (Command c : commands.values())
{
String description = c.getDescription();
description = (description == null || description.isEmpty()) ? NO_DESCRIPTION : description;
s.append("**").append(c.getAliases().get(0)).append("** - ");
s.append(description).append("\n");
}
channel.sendMessage(new MessageBuilder()
.append("The following commands are supported by the bot\n")
.append(s.toString())
.build()).queue();
}
else
{
String command = args[1].charAt(0) == '.' ? args[1] : "." + args[1]; //If there is not a preceding . attached to the command we are search, then prepend one.
for (Command c : commands.values())
{
if (c.getAliases().contains(command))
{
String name = c.getName();
String description = c.getDescription();
List<String> usageInstructions = c.getUsageInstructions();
name = (name == null || name.isEmpty()) ? NO_NAME : name;
description = (description == null || description.isEmpty()) ? NO_DESCRIPTION : description;
usageInstructions = (usageInstructions == null || usageInstructions.isEmpty()) ? Collections.singletonList(NO_USAGE) : usageInstructions;
//TODO: Replace with a PrivateMessage
channel.sendMessage(new MessageBuilder()
.append("**Name:** " + name + "\n")
.append("**Description:** " + description + "\n")
.append("**Alliases:** " + StringUtils.join(c.getAliases(), ", ") + "\n")
.append("**Usage:** ")
.append(usageInstructions.get(0))
.build()).queue();
for (int i = 1; i < usageInstructions.size(); i++)
{
channel.sendMessage(new MessageBuilder()
.append("__" + name + " Usage Cont. (" + (i + 1) + ")__\n")
.append(usageInstructions.get(i))
.build()).queue();
}
return;
}
}
channel.sendMessage(new MessageBuilder()
.append("The provided command '**" + args[1] + "**' does not exist. Use .help to list all commands.")
.build()).queue();
}
}
}