mirror of https://github.com/bvn13/JIrcBot.git
small fixes
parent
1f55b11457
commit
8cade905ef
|
@ -1,10 +1,13 @@
|
||||||
package ru.bvn13.jircbot.listeners;
|
package ru.bvn13.jircbot.listeners;
|
||||||
|
|
||||||
import org.pircbotx.hooks.events.MessageEvent;
|
import org.pircbotx.hooks.events.MessageEvent;
|
||||||
|
import org.pircbotx.hooks.events.PrivateMessageEvent;
|
||||||
|
import org.pircbotx.hooks.types.GenericMessageEvent;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import ru.bvn13.jircbot.bot.ImprovedListenerAdapter;
|
import ru.bvn13.jircbot.bot.ImprovedListenerAdapter;
|
||||||
import ru.bvn13.jircbot.config.JircBotConfiguration;
|
import ru.bvn13.jircbot.config.JircBotConfiguration;
|
||||||
|
import ru.bvn13.jircbot.documentation.DescriptionProvided;
|
||||||
import ru.bvn13.jircbot.documentation.DocumentationProvider;
|
import ru.bvn13.jircbot.documentation.DocumentationProvider;
|
||||||
import ru.bvn13.jircbot.documentation.ListenerDescription;
|
import ru.bvn13.jircbot.documentation.ListenerDescription;
|
||||||
|
|
||||||
|
@ -17,18 +20,18 @@ import java.util.Optional;
|
||||||
* Created by bvn13 on 15.03.2019.
|
* Created by bvn13 on 15.03.2019.
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
public class HelpListener extends ImprovedListenerAdapter {
|
public class HelpListener extends ImprovedListenerAdapter implements DescriptionProvided {
|
||||||
|
|
||||||
private static final List<String> COMMAND = Arrays.asList("?help", "?h");
|
private static final List<String> COMMAND = Arrays.asList("?help", "?h");
|
||||||
|
|
||||||
private JircBotConfiguration configuration;
|
private JircBotConfiguration configuration;
|
||||||
private DocumentationProvider documentationProvider;
|
private DocumentationProvider documentationProvider;
|
||||||
|
|
||||||
private String adviceToFolowMainUrl;
|
private String adviceToFollowMainUrl;
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
private void init() {
|
private void init() {
|
||||||
adviceToFolowMainUrl = String.format("see all docs: %s/docs", configuration.getMainUrl());
|
adviceToFollowMainUrl = String.format("see all docs: %s/docs", configuration.getMainUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -39,6 +42,17 @@ public class HelpListener extends ImprovedListenerAdapter {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
answer(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPrivateMessage(PrivateMessageEvent event) throws Exception {
|
||||||
|
super.onPrivateMessage(event);
|
||||||
|
|
||||||
|
answer(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void answer(GenericMessageEvent event) {
|
||||||
boolean isHelp = false;
|
boolean isHelp = false;
|
||||||
String command = "";
|
String command = "";
|
||||||
for (String c : COMMAND) {
|
for (String c : COMMAND) {
|
||||||
|
@ -56,24 +70,23 @@ public class HelpListener extends ImprovedListenerAdapter {
|
||||||
String message = event.getMessage().replace(command, "").trim();
|
String message = event.getMessage().replace(command, "").trim();
|
||||||
|
|
||||||
if (message.isEmpty()) {
|
if (message.isEmpty()) {
|
||||||
event.respond(adviceToFolowMainUrl);
|
event.respond(adviceToFollowMainUrl);
|
||||||
} else {
|
} else {
|
||||||
answerWithHelp(event, message);
|
answerWithHelp(event, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void answerWithHelp(MessageEvent event, String message) {
|
private void answerWithHelp(GenericMessageEvent event, String message) {
|
||||||
String[] words = message.replace(" ", "").split(" ");
|
String[] words = message.replace(" ", "").split(" ");
|
||||||
if (words.length > 1) {
|
if (words.length > 1 || words[0].equalsIgnoreCase("all")) {
|
||||||
event.respond(String.format("help syntax: ?help | ?help <COMMAND> | Commands: %s", documentationProvider.getAllCommands()));
|
event.respond(String.format("help syntax: ?help | ?help <COMMAND> | Commands: %s", documentationProvider.getAllCommands()));
|
||||||
} else {
|
} else {
|
||||||
Optional<ListenerDescription.CommandDescription> description = documentationProvider.findByCommand(words[0]);
|
Optional<ListenerDescription.CommandDescription> description = documentationProvider.findByCommand(words[0].toLowerCase());
|
||||||
if (description.isPresent()) {
|
if (description.isPresent()) {
|
||||||
ListenerDescription.CommandDescription d = description.get();
|
ListenerDescription.CommandDescription d = description.get();
|
||||||
event.respond(String.format("COMMAND: %s, DESCRIPTION: %s, EXAMPLE: %s", d.getCommand(), d.getDescription(), d.getExample()));
|
event.respond(String.format("COMMAND: %s, DESCRIPTION: %s, EXAMPLE: %s", d.getCommand(), d.getDescription(), d.getExample()));
|
||||||
} else {
|
} else {
|
||||||
event.respond(String.format("wrong command %s. %s%s", words[0], adviceToFolowMainUrl.substring(0, 1).toUpperCase(), adviceToFolowMainUrl.substring(1)));
|
event.respond(String.format("wrong command %s. %s%s", words[0], adviceToFollowMainUrl.substring(0, 1).toUpperCase(), adviceToFollowMainUrl.substring(1)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -87,4 +100,22 @@ public class HelpListener extends ImprovedListenerAdapter {
|
||||||
public void setConfiguration(JircBotConfiguration configuration) {
|
public void setConfiguration(JircBotConfiguration configuration) {
|
||||||
this.configuration = configuration;
|
this.configuration = configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ListenerDescription getDescription() {
|
||||||
|
return ListenerDescription.create()
|
||||||
|
.setModuleName("HelpListener")
|
||||||
|
.setModuleDescription("Provides a help")
|
||||||
|
.addCommand(ListenerDescription.CommandDescription.builder()
|
||||||
|
.command("help")
|
||||||
|
.description("Provides help of command usage")
|
||||||
|
.example("?h[elp] all|<command>")
|
||||||
|
.build()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerDescription(DocumentationProvider documentationProvider) {
|
||||||
|
documentationProvider.register(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
|
|
||||||
jircbot.version=@bot.version@
|
jircbot.version=2.1.0
|
||||||
|
|
||||||
jircbot.config=config.json
|
jircbot.config=config.json
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue