|
| 1 | +package main.core.commands.commands; |
| 2 | + |
| 3 | +import com.github.theholywaffle.teamspeak3.TS3ApiAsync; |
| 4 | +import com.github.theholywaffle.teamspeak3.api.event.TextMessageEvent; |
| 5 | +import com.github.theholywaffle.teamspeak3.api.wrapper.ClientInfo; |
| 6 | +import java.util.concurrent.TimeUnit; |
| 7 | +import java.util.concurrent.TimeoutException; |
| 8 | +import main.conf.ConfigHandler; |
| 9 | +import main.core.Executor; |
| 10 | +import main.core.commands.AccessManager; |
| 11 | +import main.server.ServerConnectionManager; |
| 12 | +import main.util.MessageHandler; |
| 13 | +import main.util.Messages; |
| 14 | +import main.util.enums.AccessLevel; |
| 15 | +import main.util.exception.AuthorizationException; |
| 16 | + |
| 17 | +/** |
| 18 | + * Command used to move the bot to the caller's channel. |
| 19 | + */ |
| 20 | +public class ComeHereCommand { |
| 21 | + |
| 22 | + private TextMessageEvent event; |
| 23 | + private TS3ApiAsync api; |
| 24 | + |
| 25 | + /** |
| 26 | + * Create a ComeHereCommand instance to handle client executions. |
| 27 | + * |
| 28 | + * @param event the {@link TextMessageEvent} that triggered the ComeHereCommand |
| 29 | + * @throws AuthorizationException is the invoker of this command does not have authorization |
| 30 | + * to execute it. |
| 31 | + */ |
| 32 | + public ComeHereCommand(TextMessageEvent event) throws AuthorizationException { |
| 33 | + this.event = event; |
| 34 | + ServerConnectionManager instance = Executor.getServer("testInstance"); |
| 35 | + api = instance.getApiAsync(); |
| 36 | + |
| 37 | + AccessManager accessManager = new AccessManager(new ConfigHandler(), AccessLevel.DEFAULT); |
| 38 | + AccessLevel invokerAccessLevel = accessManager.getAccessLevel(api.getClientInfo(event |
| 39 | + .getInvokerId()).getUninterruptibly().getServerGroups()); |
| 40 | + |
| 41 | + try { |
| 42 | + accessManager.checkAccess(invokerAccessLevel); |
| 43 | + this.handle(); |
| 44 | + } catch (AuthorizationException e) { |
| 45 | + throw new AuthorizationException(invokerAccessLevel, "!comehere"); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private void handle() { |
| 50 | + final String RETURN_TEXT = "You rang?"; |
| 51 | + |
| 52 | + try { |
| 53 | + ClientInfo invoker = api.getClientInfo(event.getInvokerId()).get(2500, TimeUnit |
| 54 | + .MILLISECONDS); |
| 55 | + Integer channelId = invoker.getChannelId(); |
| 56 | + |
| 57 | + api.moveQuery(channelId); |
| 58 | + new MessageHandler(RETURN_TEXT).sendToChannel(); |
| 59 | + } catch (InterruptedException | TimeoutException e) { |
| 60 | + new MessageHandler(String.format(Messages.ERROR_COMMAND_TIMEOUT, "ComeHere")) |
| 61 | + .returnToSender(event); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments