liwgfr

BotCommands

Apr 13th, 2021 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 KB | None | 0 0
  1. import org.telegram.telegrambots.meta.api.objects.User;
  2.  
  3. import java.lang.reflect.Method;
  4. import java.util.Set;
  5.  
  6. public class BotCommands {
  7.     @Command(description = "Помощь")
  8.     public static String help() {
  9.         StringBuilder s = new StringBuilder();
  10.         for (Method m: BotCommands.class.getMethods()) {
  11.             if (m.isAnnotationPresent(Command.class)) {
  12.                 s.append(m.getAnnotation(Command.class).description()).append("\n").append(m.getAnnotation(Command.class).additionalInfo()).append(" (/").append(m.getName()).append(")\n");
  13.             }
  14.         }
  15.         if (s.isEmpty()) {
  16.             s.append("Ничего не умею =(");
  17.         }
  18.         return s.toString();
  19.     }
  20.     public static String help(String str) {
  21.         StringBuilder s = new StringBuilder();
  22.         for (Method m: BotCommands.class.getMethods()) {
  23.             if (m.isAnnotationPresent(Command.class) && m.getName().equals(str)) {
  24.                 s.append(m.getAnnotation(Command.class).additionalInfo()).append(" (/").append(m.getName()).append(")\n");
  25.             }
  26.         }
  27.         if (s.isEmpty()) {
  28.             s.append("Нет описания такой команды");
  29.         }
  30.         return s.toString();
  31.     }
  32.  
  33.     @Command(description = "Пока", additionalInfo = "Бот выводит Пока")
  34.     public static String bye() {
  35.         return "Пока!";
  36.     }
  37.     @Command(description = "Привет", additionalInfo = "Бот выводит Привет")
  38.     public static String hello() { return "Привет!"; }
  39.     @Command(description = "Регистрация", additionalInfo = "Бот регистрирует пользователя. По /reg - по юзернейму, по /reg *name* по кастомному имени")
  40.     public static String reg(String username) {
  41.             return UserController.Register(username);
  42.     }
  43.     @Command(description = "Удаление", additionalInfo = "Бот удаляет пользователя. Работает по принципу регистрации (/del *name*)")
  44.     public static String del(String username) {
  45.         return UserController.Delete(username);
  46.     }
  47.     @Command(description = "Показать всех пользователей", additionalInfo = "Set из всех участников")
  48.     public static Set<String> show() {
  49.         return UserController.set;
  50.     }
  51.  
  52.     public static String proceedCommand(User user, String s) {
  53.         String v = "Нет такой команды";
  54.         String[] ar = s.split(" ");
  55.         switch (ar.length) {
  56.             case 1:
  57.                 switch (ar[0]) {
  58.                     case "/help", "/start" -> v = help();
  59.                     case "/reg" -> v = reg(user.getUserName());
  60.                     case "/del" -> v = del(user.getUserName());
  61.                 }
  62.                 break;
  63.             case 2:
  64.                 switch (ar[0]) {
  65.                     case "/help", "/start" -> v = help(ar[1]);
  66.                 }
  67.                 switch (ar[0]) {
  68.                     case "/reg" -> v = reg(ar[1]);
  69.                     case "/del" -> v = del(ar[1]);
  70.                 }
  71.                 break;
  72.         }
  73.         return v;
  74.     }
  75.  
  76. }
  77.  
Add Comment
Please, Sign In to add comment