105 lines
3.6 KiB
Java
105 lines
3.6 KiB
Java
package Modules;
|
|
|
|
import CORE.Core;
|
|
import Manager.UserManager;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.telegram.telegrambots.meta.api.methods.groupadministration.RestrictChatMember;
|
|
import org.telegram.telegrambots.meta.api.methods.updatingmessages.DeleteMessage;
|
|
import org.telegram.telegrambots.meta.api.objects.Update;
|
|
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
|
|
|
import java.time.Duration;
|
|
import java.util.*;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import static CORE.Core.SLOWMODETIMER;
|
|
|
|
public class SlowMode implements Module{
|
|
|
|
private Core core;
|
|
private static final Logger logger = LoggerFactory.getLogger(SlowMode.class);
|
|
|
|
public SlowMode(Core core){
|
|
|
|
this.core = core;
|
|
|
|
}
|
|
|
|
public void execute(Update update) {
|
|
|
|
logger.debug("Got a Message:\n\t" + update.toString());
|
|
|
|
//Filter for Messages only from Group-Chats and watch chats
|
|
if (update.hasMessage()
|
|
&& (update.getMessage().getChat().isSuperGroupChat() || update.getMessage().getChat().isGroupChat())
|
|
&& core.getWatchingChats().contains(update.getMessage().getChatId())
|
|
&& update.getMessage().getNewChatMembers().size() == 0) {
|
|
|
|
//Check if user as a role
|
|
if (UserManager.loadUser(update.getMessage().getFrom().getId()) == null || UserManager.loadUser(update.getMessage().getFrom().getId()).getRole() == null) {
|
|
|
|
//using restrictions for slowmode
|
|
slowModeActive(update.getMessage().getFrom().getId(), update.getMessage().getChat().getId());
|
|
|
|
if (!update.getMessage().hasText()) {
|
|
|
|
logger.info("DELETE MESSAGE:\n\t" + update.toString());
|
|
|
|
DeleteMessage del = new DeleteMessage()
|
|
.setMessageId(update.getMessage().getMessageId())
|
|
.setChatId(update.getMessage().getChatId());
|
|
|
|
try {
|
|
del.validate();
|
|
core.execute(del);
|
|
} catch (TelegramApiException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
private void slowModeActive(int userID, long chatID){
|
|
RestrictChatMember action = new RestrictChatMember()
|
|
.forTimePeriod(Duration.ofSeconds(SLOWMODETIMER))
|
|
.setCanSendMessages(false)
|
|
.setChatId(chatID)
|
|
.setUserId(userID)
|
|
.setCanAddWebPagePreviews(false)
|
|
.setCanSendMediaMessages(false)
|
|
.setCanSendOtherMessages(false);
|
|
|
|
try {
|
|
action.validate();
|
|
core.execute(action);
|
|
|
|
new Timer().schedule(new TimerTask() {
|
|
@Override
|
|
public void run() {
|
|
RestrictChatMember revoce = new RestrictChatMember()
|
|
.setCanSendMessages(true)
|
|
.setChatId(action.getChatId())
|
|
.setUserId(action.getUserId())
|
|
.setCanAddWebPagePreviews(true)
|
|
.setCanSendMediaMessages(true)
|
|
.setCanSendOtherMessages(true);
|
|
try {
|
|
revoce.validate();
|
|
core.execute(revoce);
|
|
} catch (TelegramApiException e){
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}, TimeUnit.SECONDS.toMillis(SLOWMODETIMER));
|
|
|
|
} catch (TelegramApiException e){
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|