Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.blackgoodnether.bot;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Random;
- import com.google.gson.JsonElement;
- import com.google.gson.JsonIOException;
- import com.google.gson.JsonParser;
- import com.google.gson.JsonSyntaxException;
- import ru.blackgoodnether.bot.http.Requests;
- import ru.blackgoodnether.bot.objects.Message;
- public class MessageHandler extends Thread {
- private static HashMap<String, bCommand> messages;
- static {
- MessageHandler.messages = new HashMap<String, bCommand>();
- }
- private Message message;
- public MessageHandler(Message message) {
- this.message = message;
- }
- public void handleMessage() {
- String[] args = message.getText().toLowerCase().split(" ");
- Account acc = AccountManager.getAccount(this.message.authorId());
- if(acc == null) {
- ArrayList<String> list = new ArrayList();
- acc = AccountManager.createAcc(this.message.authorId(), 1500, "Пользователь", false, list, 0, 0);
- acc.setAccConfig();
- }
- Property prop = PropertyManager.getProperty(this.message.authorId());
- if(prop == null) {
- prop = PropertyManager.createAcc(this.message.authorId(), null, null, null, null);
- prop.update();
- }
- Ferm ferm = FermManager.getFerm(this.message.authorId());
- if(ferm == null) {
- ferm = FermManager.addFerm(this.message.authorId(), null, 0);
- ferm.update();
- }
- if(Manager.accounts.get(Integer.toString(message.authorId())) == null) {
- try {
- Manager.setNick(Integer.toString(message.authorId()), this.getName1());
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- if(MessageHandler.messages.containsKey(args[0].toLowerCase())) {
- MessageHandler.messages.get(args[0].toLowerCase()).execute(acc, message.getText().split(" "), this);
- }
- }
- public static void registerCommand(String cmd, bCommand clasz) {
- MessageHandler.messages.put(cmd, clasz);
- }
- public String readFile2(String path)
- {
- // Assumes that a file article.rss is available on the SD card
- File file = new File(path);
- ArrayList<String> list = new ArrayList<>();
- if (!file.exists()) {
- throw new RuntimeException("File not found");
- }
- BufferedReader reader = null;
- try {
- reader = new BufferedReader(new FileReader(file));
- String line;
- while ((line = reader.readLine()) != null) {
- list.add(line);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (reader != null) {
- try {
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return list.toString();
- }
- public ArrayList<String> readFile(String path)
- {
- // Assumes that a file article.rss is available on the SD card
- File file = new File(path);
- ArrayList<String> list = new ArrayList<>();
- if (!file.exists()) {
- throw new RuntimeException("File not found");
- }
- BufferedReader reader = null;
- try {
- reader = new BufferedReader(new FileReader(file));
- String line;
- while ((line = reader.readLine()) != null) {
- list.add(line);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (reader != null) {
- try {
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return list;
- }
- public void sendMSG(String text) {
- String[] message = {"\n\n⚠ » Подпишись на нас, чтобы быть в курсе всего!", "\n\n⚠ » Нашли баг/недоработку, то пишите @blackgoodnether (сюда).", "\n\n⚠ » Информация о боте -- пропишите 'бот'.", "\n\n⚠ » Список администраторов -- пропишите 'админы'."};
- int random = (int) (0 + (Math.random() * 100));
- if(random < 50) {
- text = text + message[new Random().nextInt(message.length)];
- }
- if(this.message.isFromChat()) {
- 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"));
- return;
- }
- 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"));
- }
- public void sendMSG(Integer id, String text) {
- String[] message = {"\n\n⚠ » Подпишись на нас, чтобы быть в курсе всего!", "\n\n⚠ » Нашли баг/недоработку, то пишите @blackgoodnether (сюда).", "\n\n⚠ » Информация о боте -- пропишите 'бот'.", "\n\n⚠ » Список администраторов -- пропишите 'админы'."};
- int random = (int) (0 + (Math.random() * 100));
- if(random < 50) {
- text = text + message[new Random().nextInt(message.length)];
- }
- 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"));
- }
- public void sendMSG(String text, String attachments) {
- String[] message = {"\n\n⚠ » Подпишись на нас, чтобы быть в курсе всего!", "\n\n⚠ » Нашли баг/недоработку, то пишите @blackgoodnether (сюда).", "\n\n⚠ » Информация о боте -- пропишите 'бот'.", "\n\n⚠ » Список администраторов -- пропишите 'админы'."};
- int random = (int) (0 + (Math.random() * 100));
- if(random < 50) {
- text = text + message[new Random().nextInt(message.length)];
- }
- if(this.message.isFromChat()) {
- 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"));
- return;
- }
- 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"));
- }
- public void sendAttachent(String attachments, String id, String text) {
- StringBuffer sb = new StringBuffer();
- for(String s : Manager.config.getConfigurationSection("addons_messages").getKeys(false)) {
- sb.append(s + ",");
- }
- if(text == null) {
- Requests.post("https://api.vk.com/method/messages.send", "user_id=" + id + "&access_token=" + Manager.config.getString("access_token") + "&attachment=" + attachments + "&v=5.80");
- } else {
- 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");
- }
- }
- public void sendAttachentChat(String attachments, String id, String text) {
- StringBuffer sb = new StringBuffer();
- for(String s : Manager.config.getConfigurationSection("addons_messages").getKeys(false)) {
- sb.append(s + ",");
- }
- if(text == null) {
- Requests.post("https://api.vk.com/method/messages.send", "peer_id=" + id + "&access_token=" + Manager.config.getString("access_token") + "&attachment=" + attachments + "&v=5.80");
- } else {
- 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");
- }
- }
- public void editMessage(String message_id, String text) {
- 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");
- }
- public String getFullName(String id) {
- JsonElement b = null;
- try {
- 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()));
- } catch (JsonIOException | JsonSyntaxException | IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- System.out.println(e.getLocalizedMessage());
- }
- 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();
- }
- public String getFullName(int id) {
- JsonElement b = null;
- try {
- 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()));
- } catch (JsonIOException | JsonSyntaxException | IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- System.out.println(e.getLocalizedMessage());
- }
- 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();
- }
- public String replacer(long l) {
- StringBuffer sb = new StringBuffer(Long.toString(l));
- if(l >= 1000000000000000000L) {
- sb.insert(1, ".");
- sb.insert(5, ".");
- sb.insert(9, ".");
- sb.insert(13, ".");
- sb.insert(17, ".");
- sb.insert(21, ".");
- return sb.toString();
- } else if(l >= 100000000000000000L) {
- sb.insert(3, ".");
- sb.insert(7, ".");
- sb.insert(11, ".");
- sb.insert(15, ".");
- sb.insert(19, ".");
- return sb.toString();
- } else if(l >= 10000000000000000L) {
- sb.insert(2, ".");
- sb.insert(6, ".");
- sb.insert(10, ".");
- sb.insert(14, ".");
- sb.insert(18, ".");
- return sb.toString();
- } else if(l >= 1000000000000000L) {
- sb.insert(1, ".");
- sb.insert(5, ".");
- sb.insert(9, ".");
- sb.insert(13, ".");
- sb.insert(17, ".");
- return sb.toString();
- } else if(l >= 100000000000000L) {
- sb.insert(3, ".");
- sb.insert(7, ".");
- sb.insert(11, ".");
- sb.insert(15, ".");
- return sb.toString();
- } else if(l >= 10000000000000L) {
- sb.insert(2, ".");
- sb.insert(6, ".");
- sb.insert(10, ".");
- sb.insert(14, ".");
- return sb.toString();
- } else if(l >= 1000000000000L) {
- sb.insert(1, ".");
- sb.insert(5, ".");
- sb.insert(9, ".");
- sb.insert(13, ".");
- return sb.toString();
- } else if(l >= 100000000000L) {
- sb.insert(3, ".");
- sb.insert(7, ".");
- sb.insert(11, ".");
- return sb.toString();
- } else if(l >= 10000000000L) {
- sb.insert(2, ".");
- sb.insert(6, ".");
- sb.insert(10, ".");
- return sb.toString();
- } else if(l >= 1000000000) {
- sb.insert(1, ".");
- sb.insert(5, ".");
- sb.insert(9, ".");
- return sb.toString();
- } else if(l >= 100000000) {
- sb.insert(3, ".");
- sb.insert(7, ".");
- return sb.toString();
- } else if(l >= 10000000) {
- sb.insert(2, ".");
- sb.insert(6, ".");
- return sb.toString();
- } else if(l >= 1000000) {
- sb.insert(1, ".");
- sb.insert(5, ".");
- return sb.toString();
- } else if(l >= 100000) {
- sb.insert(3, ".");
- return sb.toString();
- } else if(l >= 10000) {
- sb.insert(2, ".");
- return sb.toString();
- } else if(l >= 1000) {
- sb.insert(1, ".");
- return sb.toString();
- }
- return sb.toString();
- }
- public int getIDInt(String id) {
- JsonElement b = null;
- try {
- 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"));
- } catch (JsonIOException | JsonSyntaxException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- System.out.println(e.getLocalizedMessage());
- }
- return b.getAsJsonObject().get("response").getAsJsonArray().get(0).getAsJsonObject().get("id").getAsInt();
- }
- public static String ReplacerColor(String msg) {
- 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", "");
- }
- public String getName1() {
- JsonElement b = null;
- try {
- 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"));
- } catch (JsonIOException | JsonSyntaxException e) {
- return null;
- }
- return b.getAsJsonObject().get("response").getAsJsonArray().get(0).getAsJsonObject().get("first_name").getAsString();
- }
- public String getFullName() {
- JsonElement b = null;
- try {
- 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"));
- } catch (JsonIOException | JsonSyntaxException e) {
- return null;
- }
- 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();
- }
- @Override
- public void run() {
- try {
- if(message.isFromChat()) {
- if(!Manager.chats.contains("chats." + message.getChatId())) {
- Manager.chats.set("chats." + message.getChatId(), message.getChatId());
- Manager.chats.save(new File("chats.yml"));
- }
- }
- String[] args = message.getText().split(" ");
- Account acc = AccountManager.getAccount(this.message.authorId());
- if(acc == null) {
- ArrayList<String> list = new ArrayList();
- acc = AccountManager.createAcc(this.message.authorId(), 1500, "Пользователь", false, list, 0, 0);
- acc.setAccConfig();
- }
- Property prop = PropertyManager.getProperty(this.message.authorId());
- if(prop == null) {
- prop = PropertyManager.createAcc(this.message.authorId(), null, null, null, null);
- prop.update();
- }
- Ferm ferm = FermManager.getFerm(this.message.authorId());
- if(ferm == null) {
- ferm = FermManager.addFerm(this.message.authorId(), null, 0);
- ferm.update();
- }
- if(Manager.accounts.get(Integer.toString(message.authorId())) == null) {
- try {
- Manager.setNick(Integer.toString(message.authorId()), this.getName1());
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- if(MessageHandler.messages.containsKey(args[0].toLowerCase())) {
- MessageHandler.messages.get(args[0].toLowerCase()).execute(acc, message.getText().split(" "), this);
- } else if(!this.message.isFromChat()) {
- this.sendMSG(acc.getNick() + ",\n\n😣 » Я вас не понимаю, пропишите — помощь");
- }
- } catch (Exception e) {}
- }
- public boolean isFromChat() {
- return this.message.isFromChat();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement