Advertisement
Guest User

Untitled

a guest
Sep 19th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.97 KB | None | 0 0
  1. package ru.blackgoodnether.bot;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.net.URL;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.Random;
  12.  
  13. import com.google.gson.JsonElement;
  14. import com.google.gson.JsonIOException;
  15. import com.google.gson.JsonParser;
  16. import com.google.gson.JsonSyntaxException;
  17.  
  18. import ru.blackgoodnether.bot.http.Requests;
  19. import ru.blackgoodnether.bot.objects.Message;
  20.  
  21.  
  22. public class MessageHandler extends Thread {
  23.  
  24. private static HashMap<String, bCommand> messages;
  25.  
  26. static {
  27. MessageHandler.messages = new HashMap<String, bCommand>();
  28. }
  29.  
  30. private Message message;
  31.  
  32. public MessageHandler(Message message) {
  33. this.message = message;
  34. }
  35.  
  36.  
  37. public void handleMessage() {
  38. String[] args = message.getText().toLowerCase().split(" ");
  39. Account acc = AccountManager.getAccount(this.message.authorId());
  40. if(acc == null) {
  41. ArrayList<String> list = new ArrayList();
  42. acc = AccountManager.createAcc(this.message.authorId(), 1500, "Пользователь", false, list, 0, 0);
  43. acc.setAccConfig();
  44. }
  45. Property prop = PropertyManager.getProperty(this.message.authorId());
  46. if(prop == null) {
  47. prop = PropertyManager.createAcc(this.message.authorId(), null, null, null, null);
  48. prop.update();
  49. }
  50. Ferm ferm = FermManager.getFerm(this.message.authorId());
  51. if(ferm == null) {
  52. ferm = FermManager.addFerm(this.message.authorId(), null, 0);
  53. ferm.update();
  54. }
  55. if(Manager.accounts.get(Integer.toString(message.authorId())) == null) {
  56. try {
  57. Manager.setNick(Integer.toString(message.authorId()), this.getName1());
  58. } catch (IOException e) {
  59. // TODO Auto-generated catch block
  60. e.printStackTrace();
  61. }
  62. }
  63. if(MessageHandler.messages.containsKey(args[0].toLowerCase())) {
  64. MessageHandler.messages.get(args[0].toLowerCase()).execute(acc, message.getText().split(" "), this);
  65. }
  66. }
  67.  
  68. public static void registerCommand(String cmd, bCommand clasz) {
  69. MessageHandler.messages.put(cmd, clasz);
  70. }
  71.  
  72. public String readFile2(String path)
  73. {
  74. // Assumes that a file article.rss is available on the SD card
  75. File file = new File(path);
  76. ArrayList<String> list = new ArrayList<>();
  77. if (!file.exists()) {
  78. throw new RuntimeException("File not found");
  79. }
  80. BufferedReader reader = null;
  81. try {
  82. reader = new BufferedReader(new FileReader(file));
  83. String line;
  84. while ((line = reader.readLine()) != null) {
  85. list.add(line);
  86. }
  87. } catch (Exception e) {
  88. e.printStackTrace();
  89. } finally {
  90. if (reader != null) {
  91. try {
  92. reader.close();
  93. } catch (IOException e) {
  94. e.printStackTrace();
  95. }
  96. }
  97. }
  98.  
  99. return list.toString();
  100. }
  101.  
  102. public ArrayList<String> readFile(String path)
  103. {
  104. // Assumes that a file article.rss is available on the SD card
  105. File file = new File(path);
  106. ArrayList<String> list = new ArrayList<>();
  107. if (!file.exists()) {
  108. throw new RuntimeException("File not found");
  109. }
  110. BufferedReader reader = null;
  111. try {
  112. reader = new BufferedReader(new FileReader(file));
  113. String line;
  114. while ((line = reader.readLine()) != null) {
  115. list.add(line);
  116. }
  117. } catch (Exception e) {
  118. e.printStackTrace();
  119. } finally {
  120. if (reader != null) {
  121. try {
  122. reader.close();
  123. } catch (IOException e) {
  124. e.printStackTrace();
  125. }
  126. }
  127. }
  128.  
  129. return list;
  130. }
  131.  
  132. public void sendMSG(String text) {
  133. String[] message = {"\n\n⚠ » Подпишись на нас, чтобы быть в курсе всего!", "\n\n⚠ » Нашли баг/недоработку, то пишите @blackgoodnether (сюда).", "\n\n⚠ » Информация о боте -- пропишите 'бот'.", "\n\n⚠ » Список администраторов -- пропишите 'админы'."};
  134. int random = (int) (0 + (Math.random() * 100));
  135. if(random < 50) {
  136. text = text + message[new Random().nextInt(message.length)];
  137. }
  138. if(this.message.isFromChat()) {
  139. System.out.println(Requests.post("https://api.vk.com/method/messages.send", "peer_id=" + this.message.getChatId() + "&access_token=" + Manager.config.getString("access_token") + "&message=" + text + "&v=5.80"));
  140. return;
  141. }
  142. System.out.println(Requests.post("https://api.vk.com/method/messages.send", "user_id=" + this.message.authorId() + "&access_token=" + Manager.config.getString("access_token") + "&message=" + text + "&v=5.80"));
  143. }
  144.  
  145. public void sendMSG(Integer id, String text) {
  146. String[] message = {"\n\n⚠ » Подпишись на нас, чтобы быть в курсе всего!", "\n\n⚠ » Нашли баг/недоработку, то пишите @blackgoodnether (сюда).", "\n\n⚠ » Информация о боте -- пропишите 'бот'.", "\n\n⚠ » Список администраторов -- пропишите 'админы'."};
  147. int random = (int) (0 + (Math.random() * 100));
  148. if(random < 50) {
  149. text = text + message[new Random().nextInt(message.length)];
  150. }
  151. System.out.println(Requests.post("https://api.vk.com/method/messages.send", "user_id=" + id + "&access_token=" + Manager.config.getString("access_token") + "&message=" + text + "&v=5.80"));
  152. }
  153.  
  154. public void sendMSG(String text, String attachments) {
  155. String[] message = {"\n\n⚠ » Подпишись на нас, чтобы быть в курсе всего!", "\n\n⚠ » Нашли баг/недоработку, то пишите @blackgoodnether (сюда).", "\n\n⚠ » Информация о боте -- пропишите 'бот'.", "\n\n⚠ » Список администраторов -- пропишите 'админы'."};
  156. int random = (int) (0 + (Math.random() * 100));
  157. if(random < 50) {
  158. text = text + message[new Random().nextInt(message.length)];
  159. }
  160. if(this.message.isFromChat()) {
  161. System.out.println(Requests.post("https://api.vk.com/method/messages.send", "peer_id=" + this.message.getChatId() + "&access_token=" + Manager.config.getString("access_token") + "&message=" + text + "&attachment=" + attachments + "&v=5.80"));
  162. return;
  163. }
  164. System.out.println(Requests.post("https://api.vk.com/method/messages.send", "user_id=" + this.message.authorId() + "&access_token=" + Manager.config.getString("access_token") + "&message=" + text + "&attachment=" + attachments + "&v=5.80"));
  165. }
  166.  
  167. public void sendAttachent(String attachments, String id, String text) {
  168. StringBuffer sb = new StringBuffer();
  169. for(String s : Manager.config.getConfigurationSection("addons_messages").getKeys(false)) {
  170. sb.append(s + ",");
  171. }
  172. if(text == null) {
  173. Requests.post("https://api.vk.com/method/messages.send", "user_id=" + id + "&access_token=" + Manager.config.getString("access_token") + "&attachment=" + attachments + "&v=5.80");
  174. } else {
  175. Requests.post("https://api.vk.com/method/messages.send", "user_id=" + id + "&access_token=" + Manager.config.getString("access_token") + "&message=" + text + "&attachment=" + attachments + "&v=5.80");
  176. }
  177. }
  178.  
  179. public void sendAttachentChat(String attachments, String id, String text) {
  180. StringBuffer sb = new StringBuffer();
  181. for(String s : Manager.config.getConfigurationSection("addons_messages").getKeys(false)) {
  182. sb.append(s + ",");
  183. }
  184. if(text == null) {
  185. Requests.post("https://api.vk.com/method/messages.send", "peer_id=" + id + "&access_token=" + Manager.config.getString("access_token") + "&attachment=" + attachments + "&v=5.80");
  186. } else {
  187. Requests.post("https://api.vk.com/method/messages.send", "peer_id=" + id + "&access_token=" + Manager.config.getString("access_token") + "&message=" + text + "&attachment=" + attachments + "&v=5.80");
  188. }
  189. }
  190.  
  191. public void editMessage(String message_id, String text) {
  192. Requests.post("https://api.vk.com/method/messages.edit", "peer_id=" + this.message.authorId() + "&access_token=" + Manager.config.getString("access_token") + "&message_id=" + message_id + "&message=" + text + "&v=5.80");
  193. }
  194.  
  195. public String getFullName(String id) {
  196. JsonElement b = null;
  197. try {
  198. b = new JsonParser().parse(new InputStreamReader(new URL("https://api.vk.com/method/users.get?user_ids=" + id + "&access_token=" + Manager.config.getString("access_token") + "&v=5.80").openStream()));
  199. } catch (JsonIOException | JsonSyntaxException | IOException e) {
  200. // TODO Auto-generated catch block
  201. e.printStackTrace();
  202. System.out.println(e.getLocalizedMessage());
  203. }
  204. return b.getAsJsonObject().get("response").getAsJsonArray().get(0).getAsJsonObject().get("first_name").getAsString() + " " + b.getAsJsonObject().get("response").getAsJsonArray().get(0).getAsJsonObject().get("last_name").getAsString();
  205. }
  206.  
  207. public String getFullName(int id) {
  208. JsonElement b = null;
  209. try {
  210. b = new JsonParser().parse(new InputStreamReader(new URL("https://api.vk.com/method/users.get?user_ids=id" + id + "&access_token=" + Manager.config.getString("access_token") + "&v=5.80").openStream()));
  211. } catch (JsonIOException | JsonSyntaxException | IOException e) {
  212. // TODO Auto-generated catch block
  213. e.printStackTrace();
  214. System.out.println(e.getLocalizedMessage());
  215. }
  216. return b.getAsJsonObject().get("response").getAsJsonArray().get(0).getAsJsonObject().get("first_name").getAsString() + " " + b.getAsJsonObject().get("response").getAsJsonArray().get(0).getAsJsonObject().get("last_name").getAsString();
  217. }
  218.  
  219. public String replacer(long l) {
  220. StringBuffer sb = new StringBuffer(Long.toString(l));
  221. if(l >= 1000000000000000000L) {
  222. sb.insert(1, ".");
  223. sb.insert(5, ".");
  224. sb.insert(9, ".");
  225. sb.insert(13, ".");
  226. sb.insert(17, ".");
  227. sb.insert(21, ".");
  228. return sb.toString();
  229. } else if(l >= 100000000000000000L) {
  230. sb.insert(3, ".");
  231. sb.insert(7, ".");
  232. sb.insert(11, ".");
  233. sb.insert(15, ".");
  234. sb.insert(19, ".");
  235. return sb.toString();
  236. } else if(l >= 10000000000000000L) {
  237. sb.insert(2, ".");
  238. sb.insert(6, ".");
  239. sb.insert(10, ".");
  240. sb.insert(14, ".");
  241. sb.insert(18, ".");
  242. return sb.toString();
  243. } else if(l >= 1000000000000000L) {
  244. sb.insert(1, ".");
  245. sb.insert(5, ".");
  246. sb.insert(9, ".");
  247. sb.insert(13, ".");
  248. sb.insert(17, ".");
  249. return sb.toString();
  250. } else if(l >= 100000000000000L) {
  251. sb.insert(3, ".");
  252. sb.insert(7, ".");
  253. sb.insert(11, ".");
  254. sb.insert(15, ".");
  255. return sb.toString();
  256. } else if(l >= 10000000000000L) {
  257. sb.insert(2, ".");
  258. sb.insert(6, ".");
  259. sb.insert(10, ".");
  260. sb.insert(14, ".");
  261. return sb.toString();
  262. } else if(l >= 1000000000000L) {
  263. sb.insert(1, ".");
  264. sb.insert(5, ".");
  265. sb.insert(9, ".");
  266. sb.insert(13, ".");
  267. return sb.toString();
  268. } else if(l >= 100000000000L) {
  269. sb.insert(3, ".");
  270. sb.insert(7, ".");
  271. sb.insert(11, ".");
  272. return sb.toString();
  273. } else if(l >= 10000000000L) {
  274. sb.insert(2, ".");
  275. sb.insert(6, ".");
  276. sb.insert(10, ".");
  277. return sb.toString();
  278. } else if(l >= 1000000000) {
  279. sb.insert(1, ".");
  280. sb.insert(5, ".");
  281. sb.insert(9, ".");
  282. return sb.toString();
  283. } else if(l >= 100000000) {
  284. sb.insert(3, ".");
  285. sb.insert(7, ".");
  286. return sb.toString();
  287. } else if(l >= 10000000) {
  288. sb.insert(2, ".");
  289. sb.insert(6, ".");
  290. return sb.toString();
  291. } else if(l >= 1000000) {
  292. sb.insert(1, ".");
  293. sb.insert(5, ".");
  294. return sb.toString();
  295. } else if(l >= 100000) {
  296. sb.insert(3, ".");
  297. return sb.toString();
  298. } else if(l >= 10000) {
  299. sb.insert(2, ".");
  300. return sb.toString();
  301. } else if(l >= 1000) {
  302. sb.insert(1, ".");
  303. return sb.toString();
  304. }
  305. return sb.toString();
  306. }
  307.  
  308. public int getIDInt(String id) {
  309. JsonElement b = null;
  310. try {
  311. b = new JsonParser().parse(Requests.get("https://api.vk.com/method/users.get?user_ids=" + id + "&access_token=" + Manager.config.getString("access_token") + "&v=5.80"));
  312. } catch (JsonIOException | JsonSyntaxException e) {
  313. // TODO Auto-generated catch block
  314. e.printStackTrace();
  315. System.out.println(e.getLocalizedMessage());
  316. }
  317. return b.getAsJsonObject().get("response").getAsJsonArray().get(0).getAsJsonObject().get("id").getAsInt();
  318. }
  319.  
  320. public static String ReplacerColor(String msg) {
  321. return msg.replace("§1", "").replace("§2", "").replace("§3", "").replace("§4", "").replace("§5", "").replace("§6", "").replace("§7", "").replace("§8", "").replace("§9", "").replace("§0", "").replace("§a", "").replace("§d", "").replace("§c", "").replace("§e", "").replace("§k", "").replace("§m", "").replace("§n", "").replace("§b", "").replace("§o", "").replace("§r", "").replace("§f", "").replace("§l", "");
  322. }
  323.  
  324. public String getName1() {
  325. JsonElement b = null;
  326. try {
  327. b = new JsonParser().parse(Requests.get("https://api.vk.com/method/users.get?user_ids=" + this.message.authorId() + "&access_token=" + Manager.config.getString("access_token") + "&v=5.80"));
  328. } catch (JsonIOException | JsonSyntaxException e) {
  329. return null;
  330. }
  331. return b.getAsJsonObject().get("response").getAsJsonArray().get(0).getAsJsonObject().get("first_name").getAsString();
  332. }
  333.  
  334. public String getFullName() {
  335. JsonElement b = null;
  336. try {
  337. b = new JsonParser().parse(Requests.get("https://api.vk.com/method/users.get?user_ids=" + this.message.authorId() + "&access_token=" + Manager.config.getString("access_token") + "&v=5.80"));
  338. } catch (JsonIOException | JsonSyntaxException e) {
  339. return null;
  340. }
  341. return b.getAsJsonObject().get("response").getAsJsonArray().get(0).getAsJsonObject().get("first_name").getAsString() + " " + b.getAsJsonObject().get("response").getAsJsonArray().get(0).getAsJsonObject().get("last_name").getAsString();
  342. }
  343.  
  344.  
  345. @Override
  346. public void run() {
  347. try {
  348. if(message.isFromChat()) {
  349. if(!Manager.chats.contains("chats." + message.getChatId())) {
  350. Manager.chats.set("chats." + message.getChatId(), message.getChatId());
  351. Manager.chats.save(new File("chats.yml"));
  352. }
  353. }
  354. String[] args = message.getText().split(" ");
  355. Account acc = AccountManager.getAccount(this.message.authorId());
  356. if(acc == null) {
  357. ArrayList<String> list = new ArrayList();
  358. acc = AccountManager.createAcc(this.message.authorId(), 1500, "Пользователь", false, list, 0, 0);
  359. acc.setAccConfig();
  360. }
  361. Property prop = PropertyManager.getProperty(this.message.authorId());
  362. if(prop == null) {
  363. prop = PropertyManager.createAcc(this.message.authorId(), null, null, null, null);
  364. prop.update();
  365. }
  366. Ferm ferm = FermManager.getFerm(this.message.authorId());
  367. if(ferm == null) {
  368. ferm = FermManager.addFerm(this.message.authorId(), null, 0);
  369. ferm.update();
  370. }
  371. if(Manager.accounts.get(Integer.toString(message.authorId())) == null) {
  372. try {
  373. Manager.setNick(Integer.toString(message.authorId()), this.getName1());
  374. } catch (IOException e) {
  375. // TODO Auto-generated catch block
  376. e.printStackTrace();
  377. }
  378. }
  379. if(MessageHandler.messages.containsKey(args[0].toLowerCase())) {
  380. MessageHandler.messages.get(args[0].toLowerCase()).execute(acc, message.getText().split(" "), this);
  381. } else if(!this.message.isFromChat()) {
  382. this.sendMSG(acc.getNick() + ",\n\n😣 » Я вас не понимаю, пропишите — помощь");
  383. }
  384. } catch (Exception e) {}
  385. }
  386.  
  387. public boolean isFromChat() {
  388. return this.message.isFromChat();
  389. }
  390. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement