1
0
mirror of https://github.com/aNNiMON/ffmpegbot synced 2024-09-19 22:54:20 +03:00

Add superuser access configuration for /run command, make /start and /help command accessible for all users

This commit is contained in:
aNNiMON 2023-01-10 23:02:31 +02:00
parent d9906c717c
commit 5e29f677c7
7 changed files with 35 additions and 10 deletions

View File

@ -1,5 +1,7 @@
# Telegram bot token and bot username
botToken: 1234567890:AAAABBBBCCCCDDDDEEEEFF-GGGGHHHHIIII
botUsername: yourbotname
# Superusers can execute /run command
superUsers: [12345]
# Allowed user ids
allowedUsers: [12345, 12346, 12347]
allowedUsers: [12346, 12347]

View File

@ -2,9 +2,11 @@ package com.annimon.ffmpegbot;
import java.util.Set;
public record BotConfig(String botToken, String botUsername, Set<Long> allowedUsers) {
public record BotConfig(String botToken, String botUsername,
Set<Long> superUsers, Set<Long> allowedUsers) {
boolean isUserAllowed(Long userId) {
return allowedUsers().contains(userId);
return superUsers().contains(userId)
|| allowedUsers().contains(userId);
}
}

View File

@ -12,6 +12,9 @@ import com.annimon.tgbotsmodule.commands.authority.For;
import org.jetbrains.annotations.NotNull;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.User;
import java.util.EnumSet;
public class MainBotHandler extends BotHandler {
private final BotConfig botConfig;
@ -20,7 +23,7 @@ public class MainBotHandler extends BotHandler {
public MainBotHandler(BotConfig botConfig) {
this.botConfig = botConfig;
commands = new CommandRegistry<>(this, ((update, user, fors) -> botConfig.isUserAllowed(user.getId())));
commands = new CommandRegistry<>(this, this::checkAccess);
final var sessions = new Sessions();
mediaProcessingBundle = new MediaProcessingBundle(sessions);
commands.registerBundle(mediaProcessingBundle);
@ -50,4 +53,14 @@ public class MainBotHandler extends BotHandler {
public String getBotToken() {
return botConfig.botToken();
}
private boolean checkAccess(Update update, @NotNull User user, @NotNull EnumSet<For> roles) {
final long userId = user.getId();
if (roles.contains(For.CREATOR) && botConfig.superUsers().contains(userId))
return true;
if (roles.contains(For.ADMIN) && botConfig.isUserAllowed(userId))
return true;
return roles.contains(For.USER);
}
}

View File

@ -6,6 +6,7 @@ import com.annimon.tgbotsmodule.commands.context.MessageContext;
import org.jetbrains.annotations.NotNull;
import java.util.EnumSet;
import java.util.Set;
public class HelpCommand implements TextCommand {
@ -14,6 +15,11 @@ public class HelpCommand implements TextCommand {
return "/help";
}
@Override
public Set<String> aliases() {
return Set.of("/start");
}
@SuppressWarnings("unchecked")
@Override
public EnumSet<For> authority() {
@ -33,8 +39,8 @@ public class HelpCommand implements TextCommand {
<b>yt-dlp</b>
/dl link [format] download a media using yt-dlp
link a link to download (it must be supported by yt-dlp)
format (optional) a download format. Can be "audio", "240", "360", "480", "720" or "1080"
`link` a link to download (it must be supported by yt-dlp)
`format` (optional) a download format. Can be "audio", "240", "360", "480", "720" or "1080"
""".stripIndent()).enableHtml().callAsync(ctx.sender);
}
}

View File

@ -27,6 +27,7 @@ public class InputParametersBundle implements CommandBundle<For> {
public void register(@NotNull CommandRegistry commands) {
commands.register(new SimpleRegexCommand(
Pattern.compile("^/(ss|to?) ?(\\d+|\\d{2}:\\d{2}:\\d{2})?$"),
For.ADMIN,
sessionCommand(this::cutCommand)));
}

View File

@ -34,10 +34,10 @@ public class MediaProcessingBundle implements CommandBundle<For> {
@Override
public void register(@NotNull CommandRegistry commands) {
commands.register(new SimpleCallbackQueryCommand(PREV, ctx -> toggleParameter(ctx, true)));
commands.register(new SimpleCallbackQueryCommand(NEXT, ctx -> toggleParameter(ctx, false)));
commands.register(new SimpleCallbackQueryCommand(DETAIL, sessionCommand(this::details)));
commands.register(new SimpleCallbackQueryCommand(PROCESS, sessionCommand(this::process)));
commands.register(new SimpleCallbackQueryCommand(PREV, For.ADMIN, ctx -> toggleParameter(ctx, true)));
commands.register(new SimpleCallbackQueryCommand(NEXT, For.ADMIN, ctx -> toggleParameter(ctx, false)));
commands.register(new SimpleCallbackQueryCommand(DETAIL, For.ADMIN, sessionCommand(this::details)));
commands.register(new SimpleCallbackQueryCommand(PROCESS, For.ADMIN, sessionCommand(this::process)));
}
public void handleMessage(final @NotNull CommonAbsSender sender, final @NotNull Message message) {

View File

@ -17,6 +17,7 @@ public class YtDlpCommandBundle implements CommandBundle<For> {
public void register(@NotNull CommandRegistry commands) {
commands.register(new SimpleRegexCommand(
Pattern.compile("/dl (https?://[^ ]+) ?(audio|\\d+)?p?"),
For.ADMIN,
this::download));
}