1
|
|
|
package com.github.netkorp.telegram.framework.managers; |
2
|
|
|
|
3
|
|
|
import com.github.netkorp.telegram.framework.annotations.CommandGroup; |
4
|
|
|
import com.github.netkorp.telegram.framework.annotations.FreeCommand; |
5
|
|
|
import com.github.netkorp.telegram.framework.bots.PollingTelegramBot; |
6
|
|
|
import com.github.netkorp.telegram.framework.commands.interfaces.CloseCommand; |
7
|
|
|
import com.github.netkorp.telegram.framework.commands.interfaces.Command; |
8
|
|
|
import com.github.netkorp.telegram.framework.commands.interfaces.DoneCommand; |
9
|
|
|
import com.github.netkorp.telegram.framework.commands.interfaces.HelpCommand; |
10
|
|
|
import com.github.netkorp.telegram.framework.commands.interfaces.MultistageCommand; |
11
|
|
|
import com.github.netkorp.telegram.framework.exceptions.CommandNotActive; |
12
|
|
|
import com.github.netkorp.telegram.framework.exceptions.CommandNotFound; |
13
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
14
|
|
|
import org.springframework.stereotype.Component; |
15
|
|
|
|
16
|
|
|
import java.util.HashMap; |
17
|
|
|
import java.util.LinkedList; |
18
|
|
|
import java.util.List; |
19
|
|
|
import java.util.Map; |
20
|
|
|
import java.util.SortedMap; |
21
|
|
|
import java.util.TreeMap; |
22
|
|
|
|
23
|
|
|
@Component |
24
|
|
|
public class CommandManager { |
25
|
|
|
private final Map<String, Command> secureCommands; |
26
|
|
|
private final Map<String, Command> freeCommands; |
27
|
|
|
|
28
|
|
|
private final SortedMap<String, List<Command>> commandsByGroup; |
29
|
|
|
|
30
|
|
|
private final Map<Long, MultistageCommand> activeCommand; |
31
|
|
|
|
32
|
|
|
private String closeCommand; |
33
|
|
|
private String doneCommand; |
34
|
|
|
private String helpCommand; |
35
|
|
|
|
36
|
|
|
@Autowired |
37
|
|
|
public CommandManager(List<Command> commands, PollingTelegramBot bot) { |
38
|
|
|
this.secureCommands = new HashMap<>(); |
39
|
|
|
this.freeCommands = new HashMap<>(); |
40
|
|
|
|
41
|
|
|
this.commandsByGroup = new TreeMap<>(); |
42
|
|
|
this.activeCommand = new HashMap<>(); |
43
|
|
|
|
44
|
|
|
commands.forEach(command -> addCommand(command, bot)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private void addCommand(Command command, PollingTelegramBot bot) { |
48
|
|
|
command.setBot(bot); |
49
|
|
|
command.setCommandManager(this); |
50
|
|
|
this.secureCommands.put(command.command(), command); |
51
|
|
|
|
52
|
|
|
if (command.getClass().isAnnotationPresent(FreeCommand.class)) { |
53
|
|
|
this.freeCommands.put(command.command(), command); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (command instanceof CloseCommand) { |
57
|
|
|
closeCommand = command.command(); |
58
|
|
|
} else if (command instanceof DoneCommand) { |
59
|
|
|
doneCommand = command.command(); |
60
|
|
|
} else if (command instanceof HelpCommand) { |
61
|
|
|
helpCommand = command.command(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// Groups |
65
|
|
|
String group = command.getClass().isAnnotationPresent(CommandGroup.class) ? |
66
|
|
|
command.getClass().getAnnotation(CommandGroup.class).value() : ""; |
67
|
|
|
|
68
|
|
|
List<Command> commandList = this.commandsByGroup.getOrDefault(group, new LinkedList<>()); |
69
|
|
|
commandList.add(command); |
70
|
|
|
|
71
|
|
|
this.commandsByGroup.put(group, commandList); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the {@link Command} instance from a given getName. |
76
|
|
|
* |
77
|
|
|
* @param command The commands as {@link String}. |
78
|
|
|
* @return The {@link Command} instance. |
79
|
|
|
* @throws CommandNotFound If the given name is not related to any commands. |
80
|
|
|
*/ |
81
|
|
|
public Command getSecureCommand(String command) throws CommandNotFound { |
82
|
|
|
if (!this.secureCommands.containsKey(command)) { |
83
|
|
|
throw new CommandNotFound(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return this.secureCommands.get(command); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public Command getFreeCommand(String command) throws CommandNotFound { |
90
|
|
|
if (!this.freeCommands.containsKey(command)) { |
91
|
|
|
throw new CommandNotFound(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return this.freeCommands.get(command); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public void setActiveCommand(final Long idChat, final MultistageCommand command) { |
98
|
|
|
this.activeCommand.put(idChat, command); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Gets the active commands for a given user. |
103
|
|
|
* |
104
|
|
|
* @param idChat Chat id of the user. |
105
|
|
|
* @return The active commands. |
106
|
|
|
*/ |
107
|
|
|
public MultistageCommand getActiveCommand(Long idChat) throws CommandNotActive { |
108
|
|
|
if (!hasActiveCommand(idChat)) { |
109
|
|
|
throw new CommandNotActive(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return activeCommand.get(idChat); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Removes the active commands. |
117
|
|
|
* |
118
|
|
|
* @param idChat Chat id of the user. |
119
|
|
|
*/ |
120
|
|
|
public void removeActiveCommand(Long idChat) { |
121
|
|
|
activeCommand.remove(idChat); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Is there an active commands or not? |
126
|
|
|
* |
127
|
|
|
* @param idChat Chat id of the user. |
128
|
|
|
* @return If there is active commands or not. |
129
|
|
|
*/ |
130
|
|
|
public boolean hasActiveCommand(Long idChat) { |
131
|
|
|
return activeCommand.containsKey(idChat); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public Command getCloseCommand() throws CommandNotFound { |
135
|
|
|
return getSecureCommand(this.closeCommand); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public Command getDoneCommand() throws CommandNotFound { |
139
|
|
|
return getSecureCommand(this.doneCommand); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public Command getHelpCommand() throws CommandNotFound { |
143
|
|
|
return getSecureCommand(this.helpCommand); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Returns the list with the available commands. |
148
|
|
|
* |
149
|
|
|
* @return Available commands. |
150
|
|
|
*/ |
151
|
|
|
public SortedMap<String, List<Command>> getCommandsByGroup() { |
152
|
|
|
return commandsByGroup; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|