-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
475 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>theoni.vkbot</groupId> | ||
<artifactId>vkbot</artifactId> | ||
<name>VkBot</name> | ||
<version>1.0</version> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-shade-plugin</artifactId> | ||
<version>2.1</version> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>shade</goal> | ||
</goals> | ||
<configuration> | ||
<transformers> | ||
<transformer> | ||
<mainClass>theoni.vkbot.Bot</mainClass> | ||
</transformer> | ||
</transformers> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>1.18.20</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jetbrains</groupId> | ||
<artifactId>annotations</artifactId> | ||
<version>24.0.0</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
<properties> | ||
<maven.compiler.target>17</maven.compiler.target> | ||
<maven.compiler.source>17</maven.compiler.source> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
</project> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>theoni.vkbot</groupId> | ||
<artifactId>vkbot</artifactId> | ||
<version>1.0</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>VkBot</name> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>17</maven.compiler.source> | ||
<maven.compiler.target>17</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<version>1.18.20</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.vk.api</groupId> | ||
<artifactId>sdk</artifactId> | ||
<version>1.0.14</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.yaml</groupId> | ||
<artifactId>snakeyaml</artifactId> | ||
<version>1.21</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.github.t9t.minecraft-rcon-client</groupId> | ||
<artifactId>minecraft-rcon-client</artifactId> | ||
<version>1.0.0</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-shade-plugin</artifactId> | ||
<version>2.1</version> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>shade</goal> | ||
</goals> | ||
<configuration> | ||
<transformers> | ||
<transformer | ||
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> | ||
<mainClass>theoni.vkbot.Bot</mainClass> | ||
</transformer> | ||
</transformers> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package theoni.vkbot; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
public class Bot { | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
Path currentPath = Paths.get("").toAbsolutePath(); | ||
String configPath = "/config.yml"; | ||
File file = new File(currentPath.toString(), "config.yml"); | ||
|
||
if (!file.exists()) { | ||
try { | ||
|
||
InputStream inputStream = Bot.class.getResourceAsStream(configPath); | ||
OutputStream outputStream = new FileOutputStream(file); | ||
|
||
byte[] buffer = new byte[1024]; | ||
int bytesRead; | ||
while ((bytesRead = inputStream.read(buffer)) != -1) { | ||
outputStream.write(buffer, 0, bytesRead); | ||
} | ||
|
||
inputStream.close(); | ||
outputStream.close(); | ||
|
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
new BotHandler().startBot(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package theoni.vkbot; | ||
|
||
import com.vk.api.sdk.client.actors.GroupActor; | ||
import com.vk.api.sdk.exceptions.ApiException; | ||
import com.vk.api.sdk.exceptions.ClientException; | ||
import com.vk.api.sdk.objects.messages.*; | ||
import com.vk.api.sdk.queries.messages.MessagesGetLongPollHistoryQuery; | ||
|
||
import theoni.vkbot.managers.BotManager; | ||
import theoni.vkbot.managers.ConfigManager; | ||
import theoni.vkbot.utils.Keyboards; | ||
import theoni.vkbot.utils.Rcon; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class BotHandler { | ||
|
||
private ConfigManager config; | ||
private List<Integer> rconUsers = new ArrayList<>(); | ||
|
||
public BotHandler() { | ||
this.config = new ConfigManager(new File("config.yml")); | ||
} | ||
|
||
public void startBot() { | ||
|
||
Thread thread = new Thread(new Runnable() { | ||
|
||
public void run() { | ||
|
||
GroupActor actor = new GroupActor(config.getInt("groupId"), config.getString("token")); | ||
BotManager manager = new BotManager(actor); | ||
|
||
Integer ts = manager.getTs(); | ||
|
||
while (true){ | ||
try { | ||
|
||
MessagesGetLongPollHistoryQuery historyQuery = manager.getHistoryQuery(ts); | ||
List<Message> messages = historyQuery.execute().getMessages().getItems(); | ||
|
||
if (!messages.isEmpty()){ | ||
messages.forEach(message -> { | ||
|
||
String text = message.getText().replace("/", ""); | ||
|
||
if (text.equalsIgnoreCase("Начать")){ | ||
manager.sendMessage(Messages.START.getText(), message, Keyboards.rconKeyboard()); | ||
} | ||
|
||
else if (text.equalsIgnoreCase("Rcon")){ | ||
if (config.getList("allowed-users").contains(message.getFromId())) { | ||
if (config.getList("fast-commands").size() != 0) { | ||
manager.sendMessage(Messages.RCON_WITH_COMMANDS.getText(), message, Keyboards.commandsKeyboard()); | ||
} else { | ||
manager.sendMessage(Messages.RCON.getText(), message, Keyboards.commandsKeyboard()); | ||
} | ||
rconUsers.add(message.getFromId()); | ||
} | ||
} | ||
|
||
else if (rconUsers.contains(message.getFromId())) { | ||
if (config.getList("blocked-commands").contains(text)) { | ||
manager.sendMessage(Messages.COMMAND_BLOCKED.getText(), message); | ||
return; | ||
} | ||
manager.sendMessage("Команда отправлена.\nОтвет сервера: " + Rcon.command(text), message); | ||
} | ||
}); | ||
} | ||
|
||
ts = manager.getTs(); | ||
Thread.sleep(500); | ||
|
||
} catch (InterruptedException | ClientException | ApiException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
}); | ||
thread.start(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package theoni.vkbot; | ||
|
||
import lombok.Getter; | ||
|
||
public enum Messages { | ||
START("Введите Rcon или используйте кнопку ниже."), | ||
RCON("Ведите команду для отправки на сервер."), | ||
RCON_WITH_COMMANDS("Ведите команду для отправки на сервер.\n\nБыстрые команды:"), | ||
COMMAND_BLOCKED("Данная команда заблокирована."); | ||
|
||
@Getter private String text; | ||
|
||
Messages(String text) { | ||
this.text = text; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package theoni.vkbot.managers; | ||
|
||
import java.util.Random; | ||
import com.vk.api.sdk.client.VkApiClient; | ||
import com.vk.api.sdk.client.actors.GroupActor; | ||
import com.vk.api.sdk.exceptions.ApiException; | ||
import com.vk.api.sdk.exceptions.ClientException; | ||
import com.vk.api.sdk.httpclient.HttpTransportClient; | ||
import com.vk.api.sdk.objects.messages.*; | ||
import com.vk.api.sdk.queries.messages.MessagesGetLongPollHistoryQuery; | ||
|
||
public class BotManager { | ||
|
||
private VkApiClient vk; | ||
private GroupActor actor; | ||
private Random random; | ||
|
||
public BotManager(GroupActor actor) { | ||
this.vk = new VkApiClient(new HttpTransportClient()); | ||
this.actor = actor; | ||
this.random = new Random(); | ||
} | ||
|
||
public void sendMessage(String text, Message message, Keyboard keyboard) { | ||
try { | ||
vk.messages().send(actor).message(text).userId(message.getFromId()).randomId(random.nextInt(10000)).keyboard(keyboard).execute(); | ||
} catch (ApiException | ClientException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void sendMessage(String text, Message message) { | ||
try { | ||
vk.messages().send(actor).message(text).userId(message.getFromId()).randomId(random.nextInt(10000)).execute(); | ||
} catch (ApiException | ClientException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void sendMessage(String text, Integer id, Keyboard keyboard) { | ||
try { | ||
vk.messages().send(actor).message(text).userId(id).randomId(random.nextInt(10000)).keyboard(keyboard).execute(); | ||
} catch (ApiException | ClientException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void sendMessage(String text, Integer id) { | ||
try { | ||
vk.messages().send(actor).message(text).userId(id).randomId(random.nextInt(10000)).execute(); | ||
} catch (ApiException | ClientException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public Integer getTs() { | ||
try { | ||
Integer ts = vk.messages().getLongPollServer(actor).execute().getTs(); | ||
return ts; | ||
} catch (ApiException | ClientException e) { | ||
e.printStackTrace(); | ||
} | ||
return 0; | ||
} | ||
|
||
public MessagesGetLongPollHistoryQuery getHistoryQuery(Integer ts) { | ||
MessagesGetLongPollHistoryQuery historyQuery = vk.messages().getLongPollHistory(actor).ts(ts); | ||
return historyQuery; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package theoni.vkbot.managers; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.yaml.snakeyaml.Yaml; | ||
|
||
public class ConfigManager { | ||
|
||
private Yaml yaml; | ||
private Map<String, Object> config; | ||
|
||
public ConfigManager(File configFile) { | ||
try { | ||
this.yaml = new Yaml(); | ||
this.config = yaml.load(new FileInputStream(configFile)); | ||
} catch(FileNotFoundException e) {} | ||
} | ||
|
||
public String getString(String key) { | ||
return (String) getValue(key); | ||
} | ||
|
||
public int getInt(String key) { | ||
return (int) getValue(key); | ||
} | ||
|
||
public boolean getBoolean(String key) { | ||
return (boolean) getValue(key); | ||
} | ||
|
||
public List<?> getList(String key) { | ||
return (List<?>) getValue(key); | ||
} | ||
|
||
private Object getValue(String key) { | ||
String[] keys = key.split("\\."); | ||
Map<String, Object> value = config; | ||
for (int i = 0; i < keys.length - 1; i++) { | ||
value = (Map<String, Object>) value.get(keys[i]); | ||
} | ||
return value.get(keys[keys.length - 1]); | ||
} | ||
} |
Oops, something went wrong.