mirror of
https://github.com/Eyre-S/Coeur-Morny-Cono.git
synced 2024-11-22 19:24:53 +08:00
添加了 MornyConfig 以及修改代码基于 MornyConfig
This commit is contained in:
parent
d47a865094
commit
c2de1b8748
@ -1,12 +1,12 @@
|
|||||||
## Core
|
## Core
|
||||||
|
|
||||||
VERSION = 0.8.0.11
|
VERSION = 1.0.0-alpha1
|
||||||
|
|
||||||
CODENAME = putian
|
CODENAME = beiping
|
||||||
|
|
||||||
# dependencies
|
# dependencies
|
||||||
|
|
||||||
libSpotbugsVersion = 4.7.2
|
libSpotbugsVersion = 4.7.3
|
||||||
|
|
||||||
libMessivaVersion = 0.1.0.1
|
libMessivaVersion = 0.1.0.1
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@ package cc.sukazyo.cono.morny;
|
|||||||
* the final field that will be updated by gradle automatically.
|
* the final field that will be updated by gradle automatically.
|
||||||
*/
|
*/
|
||||||
public class GradleProjectConfigures {
|
public class GradleProjectConfigures {
|
||||||
public static final String VERSION = "0.8.0.11";
|
public static final String VERSION = "1.0.0-alpha1";
|
||||||
public static final String CODENAME = "putian";
|
public static final String CODENAME = "beiping";
|
||||||
public static final long COMPILE_TIMESTAMP = 1667376095614L;
|
public static final long COMPILE_TIMESTAMP = 1667581079623L;
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,6 @@ import com.pengrad.telegrambot.request.GetMe;
|
|||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import static cc.sukazyo.cono.morny.Log.logger;
|
import static cc.sukazyo.cono.morny.Log.logger;
|
||||||
|
|
||||||
@ -27,6 +26,9 @@ public class MornyCoeur {
|
|||||||
/** 当前程序的 Morny Coeur 实例 */
|
/** 当前程序的 Morny Coeur 实例 */
|
||||||
private static MornyCoeur INSTANCE;
|
private static MornyCoeur INSTANCE;
|
||||||
|
|
||||||
|
/** 当前 Morny 的启动配置 */
|
||||||
|
public final MornyConfig config;
|
||||||
|
|
||||||
/** 当前 Morny 的{@link MornyTrusted 信任验证机}实例 */
|
/** 当前 Morny 的{@link MornyTrusted 信任验证机}实例 */
|
||||||
private final MornyTrusted trusted;
|
private final MornyTrusted trusted;
|
||||||
/** 当前 Morny 的 telegram 命令管理器 */
|
/** 当前 Morny 的 telegram 命令管理器 */
|
||||||
@ -36,7 +38,6 @@ public class MornyCoeur {
|
|||||||
/** morny 的 bot 账户 */
|
/** morny 的 bot 账户 */
|
||||||
private final TelegramBot account;
|
private final TelegramBot account;
|
||||||
private final ExtraAction extraActionInstance;
|
private final ExtraAction extraActionInstance;
|
||||||
private final boolean isRemoveCommandListWhenExit;
|
|
||||||
/**
|
/**
|
||||||
* morny 的 bot 账户的用户名<br>
|
* morny 的 bot 账户的用户名<br>
|
||||||
* <br>
|
* <br>
|
||||||
@ -53,58 +54,36 @@ public class MornyCoeur {
|
|||||||
* 这个字段将会在登陆成功后赋值为登录到的 bot 的 id。
|
* 这个字段将会在登陆成功后赋值为登录到的 bot 的 id。
|
||||||
*/
|
*/
|
||||||
public final long userid;
|
public final long userid;
|
||||||
/**
|
|
||||||
* morny 的事件忽略前缀时间<br>
|
|
||||||
* <br>
|
|
||||||
* {@link cc.sukazyo.cono.morny.bot.event.OnUpdateTimestampOffsetLock}
|
|
||||||
* 会根据这里定义的时间戳取消掉比此时间更早的事件链
|
|
||||||
*/
|
|
||||||
public final long latestEventTimestamp;
|
|
||||||
/**
|
/**
|
||||||
* morny 主程序启动时间<br>
|
* morny 主程序启动时间<br>
|
||||||
* 用于统计数据
|
* 用于统计数据
|
||||||
*/
|
*/
|
||||||
public static final long coeurStartTimestamp = System.currentTimeMillis();
|
public static final long coeurStartTimestamp = ServerMain.systemStartupTime;
|
||||||
|
|
||||||
public static final long DINNER_CHAT_ID = -1001707106392L;
|
|
||||||
|
|
||||||
private record LogInResult(TelegramBot account, String username, long userid) { }
|
private record LogInResult(TelegramBot account, String username, long userid) { }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行 bot 初始化
|
* 执行 bot 初始化
|
||||||
*
|
*
|
||||||
* @param botKey bot 的 telegram bot api token
|
* @param config Morny 实例的配置选项数据
|
||||||
* @param botUsername bot 的 username 限定。如果为 null 则表示不限定,
|
|
||||||
* 如果指定,则登录时会检查所登陆的 bot 的用户名是否与此相等
|
|
||||||
* @param master morny 实例所信任的主人的 id。用于初始化 {@link #trusted}
|
|
||||||
* @param trustedChat morny 实例所信任的群组的 id。用于初始化 {@link #trusted}
|
|
||||||
* @param latestEventTimestamp 事件处理器会处理事件的最早时间戳 ——
|
|
||||||
* 只有限定的 message 事件会受此影响。
|
|
||||||
* 单位为毫秒
|
|
||||||
*/
|
*/
|
||||||
private MornyCoeur (
|
private MornyCoeur (MornyConfig config) {
|
||||||
@Nullable String botApi, @Nullable String botApi4File,
|
|
||||||
@Nonnull String botKey, @Nullable String botUsername,
|
this.config = config;
|
||||||
long master, long trustedChat, Set<Long> trustedRDinner,
|
|
||||||
long latestEventTimestamp,
|
|
||||||
boolean isRemoveCommandListWhenExit
|
|
||||||
) {
|
|
||||||
|
|
||||||
this.latestEventTimestamp = latestEventTimestamp;
|
|
||||||
this.isRemoveCommandListWhenExit = isRemoveCommandListWhenExit;
|
|
||||||
configureSafeExit();
|
configureSafeExit();
|
||||||
|
|
||||||
logger.info("args key:\n " + botKey);
|
logger.info("args key:\n " + config.telegramBotKey);
|
||||||
if (botUsername != null) {
|
if (config.telegramBotUsername != null) {
|
||||||
logger.info("login as:\n " + botUsername);
|
logger.info("login as:\n " + config.telegramBotUsername);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final LogInResult loginResult = login(botApi, botApi4File, botKey, botUsername);
|
final LogInResult loginResult = login(config.telegramBotApiServer, config.telegramBotApiServer4File, config.telegramBotKey, config.telegramBotUsername);
|
||||||
this.account = loginResult.account;
|
this.account = loginResult.account;
|
||||||
this.username = loginResult.username;
|
this.username = loginResult.username;
|
||||||
this.userid = loginResult.userid;
|
this.userid = loginResult.userid;
|
||||||
this.trusted = new MornyTrusted(master, trustedChat, trustedRDinner);
|
this.trusted = new MornyTrusted(this);
|
||||||
StringBuilder trustedReadersDinnerIds = new StringBuilder();
|
StringBuilder trustedReadersDinnerIds = new StringBuilder();
|
||||||
trusted.getTrustedReadersOfDinnerSet().forEach(id -> trustedReadersDinnerIds.append("\n ").append(id));
|
trusted.getTrustedReadersOfDinnerSet().forEach(id -> trustedReadersDinnerIds.append("\n ").append(id));
|
||||||
logger.info(String.format("""
|
logger.info(String.format("""
|
||||||
@ -114,7 +93,7 @@ public class MornyCoeur {
|
|||||||
- trusted chat (id)
|
- trusted chat (id)
|
||||||
%d
|
%d
|
||||||
- trusted reader-of-dinner (id)%s""",
|
- trusted reader-of-dinner (id)%s""",
|
||||||
master, trustedChat, trustedReadersDinnerIds
|
config.trustedMaster, config.trustedChat, trustedReadersDinnerIds
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
@ -135,32 +114,28 @@ public class MornyCoeur {
|
|||||||
* 如果 morny 已经初始化,则不会进行初始化,抛出错误消息并直接退出方法。
|
* 如果 morny 已经初始化,则不会进行初始化,抛出错误消息并直接退出方法。
|
||||||
*
|
*
|
||||||
* @see #MornyCoeur 程序初始化方法
|
* @see #MornyCoeur 程序初始化方法
|
||||||
|
* @param config morny 实例的配置选项数据
|
||||||
*/
|
*/
|
||||||
public static void main (
|
public static void main (MornyConfig config) {
|
||||||
@Nullable String botApi, @Nullable String botApi4File,
|
|
||||||
@Nonnull String botKey, @Nullable String botUsername,
|
|
||||||
long master, long trustedChat, Set<Long> trustedRDinner, long latestEventTimestamp,
|
|
||||||
boolean isAutomaticResetCommandList, boolean isRemoveCommandListWhenExit
|
|
||||||
) {
|
|
||||||
if (INSTANCE == null) {
|
if (INSTANCE == null) {
|
||||||
|
|
||||||
logger.info("Coeur Starting");
|
logger.info("Coeur Starting");
|
||||||
INSTANCE = new MornyCoeur(
|
INSTANCE = new MornyCoeur(config);
|
||||||
botApi, botApi4File,
|
|
||||||
botKey, botUsername,
|
|
||||||
master, trustedChat, trustedRDinner,
|
|
||||||
latestEventTimestamp,
|
|
||||||
isRemoveCommandListWhenExit
|
|
||||||
);
|
|
||||||
MornyDaemons.start();
|
MornyDaemons.start();
|
||||||
|
|
||||||
logger.info("start telegram events listening");
|
logger.info("start telegram events listening");
|
||||||
EventListeners.registerAllListeners();
|
EventListeners.registerAllListeners();
|
||||||
INSTANCE.account.setUpdatesListener(OnUpdate::onNormalUpdate);
|
INSTANCE.account.setUpdatesListener(OnUpdate::onNormalUpdate);
|
||||||
if (isAutomaticResetCommandList) {
|
|
||||||
|
if (config.commandLoginRefresh) {
|
||||||
logger.info("resetting telegram command list");
|
logger.info("resetting telegram command list");
|
||||||
commandManager().automaticUpdateList();
|
commandManager().automaticUpdateList();
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info("Coeur start complete");
|
logger.info("Coeur start complete");
|
||||||
return;
|
return;
|
||||||
|
|
||||||
}
|
}
|
||||||
logger.error("Coeur already started!!!");
|
logger.error("Coeur already started!!!");
|
||||||
}
|
}
|
||||||
@ -179,7 +154,7 @@ public class MornyCoeur {
|
|||||||
private void exitCleanup () {
|
private void exitCleanup () {
|
||||||
logger.info("clean:save tracker data.");
|
logger.info("clean:save tracker data.");
|
||||||
MornyDaemons.stop();
|
MornyDaemons.stop();
|
||||||
if (isRemoveCommandListWhenExit) {
|
if (config.commandLogoutClear) {
|
||||||
commandManager.automaticRemoveList();
|
commandManager.automaticRemoveList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -192,16 +167,20 @@ public class MornyCoeur {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 登录 bot<br>
|
* 登录 bot.
|
||||||
* <br>
|
* <p>
|
||||||
* 会反复尝试三次进行登录。如果登录失败,则会直接抛出 RuntimeException 结束处理。
|
* 会反复尝试三次进行登录。如果登录失败,则会直接抛出 RuntimeException 结束处理。
|
||||||
* 会通过 GetMe 动作验证是否连接上了 telegram api 服务器,
|
* 会通过 GetMe 动作验证是否连接上了 telegram api 服务器,
|
||||||
* 同时也要求登录获得的 username 和 {@link #username} 声明值相等
|
* 同时也要求登录获得的 username 和 {@link #username} 声明值相等
|
||||||
*
|
*
|
||||||
* @param api bot client 将会连接到的 telegram bot api 位置
|
* @param api bot client 将会连接到的 telegram bot api 位置。
|
||||||
* @param api4File bot client 将会连接到的 telegram file api 位置,如果不指定则会跟随 {@code api} 选项的设定
|
* 填入 {@code null} 则使用默认的 {@code "https://api.telegram.org/bot"}
|
||||||
* @param key bot 的 api-token
|
* @param api4File bot client 将会连接到的 telegram file api 位置。
|
||||||
* @param requireName 要求登录到的需要的 username,如果登陆后的 username 与此不同则会报错退出
|
* 如果传入 {@code null} 则会跟随 {@param api} 选项的设定(具体为在 {@param api} 路径的后面添加 {@code /file} 路径)。
|
||||||
|
* 如果两者都为 {@code null},则跟随默认的 {@value FileApi#FILE_API}
|
||||||
|
* @param key bot 的 api-token. 必要值
|
||||||
|
* @param requireName 要求登录到的需要的 username,如果登陆后的 username 与此不同则会报错退出。
|
||||||
|
* 填入 {@code null} 则表示不对 username 作要求
|
||||||
* @return 成功登录后的 {@link TelegramBot} 对象
|
* @return 成功登录后的 {@link TelegramBot} 对象
|
||||||
*/
|
*/
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@ -281,13 +260,13 @@ public class MornyCoeur {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* 获取当前 morny 的配置数据
|
||||||
*
|
*
|
||||||
* 获取忽略时间点
|
* @return {@link #config MornyCoeur.config}
|
||||||
*
|
|
||||||
* @return {@link #latestEventTimestamp MornyCoeur.latestEventTimestamp}
|
|
||||||
*/
|
*/
|
||||||
public static long getLatestEventTimestamp () {
|
@Nonnull
|
||||||
return INSTANCE.latestEventTimestamp;
|
public static MornyConfig config () {
|
||||||
|
return INSTANCE.config;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
137
src/main/java/cc/sukazyo/cono/morny/MornyConfig.java
Normal file
137
src/main/java/cc/sukazyo/cono/morny/MornyConfig.java
Normal file
@ -0,0 +1,137 @@
|
|||||||
|
package cc.sukazyo.cono.morny;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class MornyConfig {
|
||||||
|
|
||||||
|
/* ======================================= *
|
||||||
|
* Config props Names Definition *
|
||||||
|
* ======================================= */
|
||||||
|
|
||||||
|
public static final String PROP_TOKEN_KEY_DEFAULT = "TELEGRAM_BOT_API_TOKEN";
|
||||||
|
public static final String PROP_TOKEN_MORNY_KEY = "MORNY_TG_TOKEN";
|
||||||
|
public static final String[] PROP_TOKEN_KEY = {PROP_TOKEN_KEY_DEFAULT, PROP_TOKEN_MORNY_KEY};
|
||||||
|
|
||||||
|
/* ======================================= *
|
||||||
|
* telegram bot login config *
|
||||||
|
* ======================================= */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Morny Telegram 使用的 API 服务器.
|
||||||
|
* <p>
|
||||||
|
* 不设定的话,默认将会使用 {@code https://api.telegram.org/bot}
|
||||||
|
*/
|
||||||
|
@Nullable public final String telegramBotApiServer;
|
||||||
|
/**
|
||||||
|
* Morny Telegram 使用的 API 服务器的 file 服务路径.
|
||||||
|
* <p>
|
||||||
|
* 不设定的话,默认将会使用 {@value com.pengrad.telegrambot.impl.FileApi#FILE_API}
|
||||||
|
*/
|
||||||
|
@Nullable public final String telegramBotApiServer4File;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* morny 使用的 telegram bot 的 bot api token.
|
||||||
|
* <p>
|
||||||
|
* 这个值必须设定。
|
||||||
|
*/
|
||||||
|
@Nonnull public final String telegramBotKey;
|
||||||
|
/**
|
||||||
|
* morny 所使用的 bot 的 username.
|
||||||
|
* <p>
|
||||||
|
* 如果设定了这个值,则在 morny 登录 bot 时将会检查所登录的 bot 的 username 是否和这里设定的 username 匹配。
|
||||||
|
* 如果不匹配,则会拒绝登录然后报错。
|
||||||
|
* <p>
|
||||||
|
* 如果没有设定这个值,则不会对登录 bot 的 username 进行限制。
|
||||||
|
*/
|
||||||
|
@Nullable public final String telegramBotUsername;
|
||||||
|
|
||||||
|
/* ======================================= *
|
||||||
|
* morny trusted config *
|
||||||
|
* ======================================= */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* morny 的主人.
|
||||||
|
* <p>
|
||||||
|
* 这项值的对象总是会被{@link MornyTrusted 信任管理器}认为是可信任的
|
||||||
|
*/
|
||||||
|
public final long trustedMaster;
|
||||||
|
/**
|
||||||
|
* morny 可信群聊的 id.
|
||||||
|
* <p>
|
||||||
|
* {@link MornyTrusted 信任管理器}将会认为这个群聊中的所有拥有
|
||||||
|
* {@link com.pengrad.telegrambot.model.ChatMember.Status#administrator administrator} 权限的成员是可信任的。
|
||||||
|
* <p>
|
||||||
|
* id 需要符合 bot api 标准。
|
||||||
|
*/
|
||||||
|
public final long trustedChat;
|
||||||
|
|
||||||
|
/* ======================================= *
|
||||||
|
* system: event ignore *
|
||||||
|
* ======================================= */
|
||||||
|
|
||||||
|
public final boolean eventIgnoreOutdated;
|
||||||
|
/**
|
||||||
|
* morny 的事件忽略前缀时间<br>
|
||||||
|
* <br>
|
||||||
|
* {@link cc.sukazyo.cono.morny.bot.event.OnUpdateTimestampOffsetLock}
|
||||||
|
* 会根据这里定义的时间戳取消掉比此时间更早的事件链
|
||||||
|
*/
|
||||||
|
public final long eventOutdatedTimestamp;
|
||||||
|
|
||||||
|
/* ======================================= *
|
||||||
|
* system: command list automation *
|
||||||
|
* ======================================= */
|
||||||
|
|
||||||
|
public final boolean commandLoginRefresh;
|
||||||
|
public final boolean commandLogoutClear;
|
||||||
|
|
||||||
|
/* ======================================= *
|
||||||
|
* function: dinner query tool *
|
||||||
|
* ======================================= */
|
||||||
|
|
||||||
|
@Nonnull public final Set<Long> dinnerTrustedReaders;
|
||||||
|
public final long dinnerChatId;
|
||||||
|
|
||||||
|
public MornyConfig (@Nonnull Prototype prototype) throws CheckFailure {
|
||||||
|
this.telegramBotApiServer = prototype.telegramBotApiServer;
|
||||||
|
this.telegramBotApiServer4File = prototype.telegramBotApiServer4File;
|
||||||
|
if (prototype.telegramBotKey == null) throw new CheckFailure.NullTelegramBotKey();
|
||||||
|
this.telegramBotKey = prototype.telegramBotKey;
|
||||||
|
this.telegramBotUsername = prototype.telegramBotUsername;
|
||||||
|
this.trustedMaster = prototype.trustedMaster;
|
||||||
|
this.trustedChat = prototype.trustedChat;
|
||||||
|
this.eventIgnoreOutdated = prototype.eventIgnoreOutdated;
|
||||||
|
if (prototype.eventOutdatedTimestamp < 1) throw new CheckFailure.UnsetEventOutdatedTimestamp();
|
||||||
|
this.eventOutdatedTimestamp = prototype.eventOutdatedTimestamp;
|
||||||
|
this.commandLoginRefresh = prototype.commandLoginRefresh;
|
||||||
|
this.commandLogoutClear = prototype.commandLogoutClear;
|
||||||
|
this.dinnerTrustedReaders = prototype.dinnerTrustedReaders;
|
||||||
|
this.dinnerChatId = prototype.dinnerChatId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class CheckFailure extends Exception {
|
||||||
|
public static class NullTelegramBotKey extends CheckFailure {}
|
||||||
|
public static class UnsetEventOutdatedTimestamp extends CheckFailure {}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Prototype {
|
||||||
|
|
||||||
|
@Nullable public String telegramBotApiServer = null;
|
||||||
|
@Nullable public String telegramBotApiServer4File = null;
|
||||||
|
@Nullable public String telegramBotKey = null;
|
||||||
|
@Nullable public String telegramBotUsername = null;
|
||||||
|
public long trustedMaster = 793274677L;
|
||||||
|
public long trustedChat = -1001541451710L;
|
||||||
|
public boolean eventIgnoreOutdated = false;
|
||||||
|
public long eventOutdatedTimestamp = -1;
|
||||||
|
public boolean commandLoginRefresh = false;
|
||||||
|
public boolean commandLogoutClear = false;
|
||||||
|
@Nonnull public Set<Long> dinnerTrustedReaders = new HashSet<>();
|
||||||
|
public long dinnerChatId = -1001707106392L;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package cc.sukazyo.cono.morny;
|
package cc.sukazyo.cono.morny;
|
||||||
|
|
||||||
import com.pengrad.telegrambot.model.ChatMember.Status;
|
import com.pengrad.telegrambot.model.ChatMember.Status;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -9,27 +9,10 @@ import java.util.Set;
|
|||||||
*/
|
*/
|
||||||
public class MornyTrusted {
|
public class MornyTrusted {
|
||||||
|
|
||||||
/**
|
private final MornyCoeur instance;
|
||||||
* 群聊id,其指向的群聊指示了哪个群的成员是受信任的
|
|
||||||
* @see #isTrusted(long) 受信检查
|
|
||||||
*/
|
|
||||||
public final Long TRUSTED_CHAT_ID;
|
|
||||||
|
|
||||||
/**
|
public MornyTrusted (MornyCoeur instance) {
|
||||||
* morny 的主人<br>
|
this.instance = instance;
|
||||||
* 这项值的对象总是会被认为是可信任的
|
|
||||||
*/
|
|
||||||
public final long MASTER;
|
|
||||||
|
|
||||||
private final Set<Long> TRUSTED_READERS_OF_DINNER;
|
|
||||||
|
|
||||||
public MornyTrusted (long master, long trustedChatId, Set<Long> trustedRDinner) {
|
|
||||||
this.TRUSTED_CHAT_ID = trustedChatId;
|
|
||||||
this.MASTER = master;
|
|
||||||
this.TRUSTED_READERS_OF_DINNER = new HashSet<>(){{
|
|
||||||
this.add(master);
|
|
||||||
this.addAll(trustedRDinner);
|
|
||||||
}};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -37,22 +20,22 @@ public class MornyTrusted {
|
|||||||
* <br>
|
* <br>
|
||||||
* 用户需要受信任才能执行一些对程序甚至是宿主环境而言危险的操作,例如关闭程序<br>
|
* 用户需要受信任才能执行一些对程序甚至是宿主环境而言危险的操作,例如关闭程序<br>
|
||||||
* <br>
|
* <br>
|
||||||
* 它的逻辑(目前)是检查群聊 {@link #TRUSTED_CHAT_ID} 中这个用户是否为群组管理员
|
* 它的逻辑(目前)是检查群聊 {@link MornyConfig#trustedChat} 中这个用户是否为群组管理员
|
||||||
*
|
*
|
||||||
* @param userId 需要检查的用户的id
|
* @param userId 需要检查的用户的id
|
||||||
* @return 所传递的用户id对应的用户是否受信任
|
* @return 所传递的用户id对应的用户是否受信任
|
||||||
*/
|
*/
|
||||||
public boolean isTrusted (long userId) {
|
public boolean isTrusted (long userId) {
|
||||||
if (userId == MASTER) return true;
|
if (userId == instance.config.trustedMaster) return true;
|
||||||
return MornyCoeur.extra().isUserInGroup(userId, TRUSTED_CHAT_ID, Status.administrator);
|
return MornyCoeur.extra().isUserInGroup(userId, instance.config.trustedChat, Status.administrator);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isTrustedForDinnerRead (long userId) {
|
public boolean isTrustedForDinnerRead (long userId) {
|
||||||
return TRUSTED_READERS_OF_DINNER.contains(userId);
|
return instance.config.dinnerTrustedReaders.contains(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<Long> getTrustedReadersOfDinnerSet () {
|
public Set<Long> getTrustedReadersOfDinnerSet () {
|
||||||
return Set.copyOf(TRUSTED_READERS_OF_DINNER);
|
return Set.copyOf(instance.config.dinnerTrustedReaders);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,9 +4,6 @@ import cc.sukazyo.cono.morny.util.CommonFormat;
|
|||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import static cc.sukazyo.cono.morny.Log.logger;
|
import static cc.sukazyo.cono.morny.Log.logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -18,8 +15,7 @@ import static cc.sukazyo.cono.morny.Log.logger;
|
|||||||
*/
|
*/
|
||||||
public class ServerMain {
|
public class ServerMain {
|
||||||
|
|
||||||
public static final String PROP_TOKEN_KEY = "TELEGRAM_BOT_API_TOKEN";
|
public static final long systemStartupTime = System.currentTimeMillis();
|
||||||
public static final String PROP_TOKEN_MORNY_KEY = "MORNY_TG_TOKEN";
|
|
||||||
|
|
||||||
private static final String THREAD_MORNY_INIT = "morny-init";
|
private static final String THREAD_MORNY_INIT = "morny-init";
|
||||||
|
|
||||||
@ -57,7 +53,7 @@ public class ServerMain {
|
|||||||
* 与 {@code --only-hello} 参数不兼容 —— 会导致程序完全没有任何输出
|
* 与 {@code --only-hello} 参数不兼容 —— 会导致程序完全没有任何输出
|
||||||
* </li>
|
* </li>
|
||||||
* <li>
|
* <li>
|
||||||
* {@code --outdated-block} 会使得 {@link MornyCoeur#latestEventTimestamp}
|
* {@code --outdated-block} 会使得 {@link MornyConfig#eventIgnoreOutdated}
|
||||||
* 赋值为程序启动的时间,从而造成阻挡程序启动之前的消息事件处理效果。
|
* 赋值为程序启动的时间,从而造成阻挡程序启动之前的消息事件处理效果。
|
||||||
* </li>
|
* </li>
|
||||||
* <li>
|
* <li>
|
||||||
@ -85,20 +81,12 @@ public class ServerMain {
|
|||||||
//#
|
//#
|
||||||
//# 启动参数设置区块
|
//# 启动参数设置区块
|
||||||
//#
|
//#
|
||||||
|
final MornyConfig.Prototype config = new MornyConfig.Prototype();
|
||||||
boolean versionEchoMode = false;
|
boolean versionEchoMode = false;
|
||||||
boolean welcomeEchoMode = false;
|
boolean welcomeEchoMode = false;
|
||||||
boolean showWelcome = true;
|
boolean showWelcome = true;
|
||||||
String key = null;
|
|
||||||
String username = null;
|
config.eventOutdatedTimestamp = systemStartupTime;
|
||||||
boolean outdatedBlock = false;
|
|
||||||
long master = 793274677L;
|
|
||||||
Set<Long> trustedReadersOfDinner = new HashSet<>();
|
|
||||||
long trustedChat = -1001541451710L;
|
|
||||||
boolean autoCmdList = false;
|
|
||||||
boolean autoCmdRemove = false;
|
|
||||||
String api = null;
|
|
||||||
String api4File = null;
|
|
||||||
|
|
||||||
for (int i = 0; i < args.length; i++) {
|
for (int i = 0; i < args.length; i++) {
|
||||||
|
|
||||||
@ -106,7 +94,7 @@ public class ServerMain {
|
|||||||
|
|
||||||
switch (args[i]) {
|
switch (args[i]) {
|
||||||
case "--outdated-block", "-ob" -> {
|
case "--outdated-block", "-ob" -> {
|
||||||
outdatedBlock = true;
|
config.eventIgnoreOutdated = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
case "--no-hello", "-hf", "--quiet", "-q" -> {
|
case "--no-hello", "-hf", "--quiet", "-q" -> {
|
||||||
@ -123,51 +111,51 @@ public class ServerMain {
|
|||||||
}
|
}
|
||||||
case "--token", "-t" -> {
|
case "--token", "-t" -> {
|
||||||
i++;
|
i++;
|
||||||
key = args[i];
|
config.telegramBotKey = args[i];
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
case "--username", "-u" -> {
|
case "--username", "-u" -> {
|
||||||
i++;
|
i++;
|
||||||
username = args[i];
|
config.telegramBotUsername = args[i];
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
case "--master", "-mm" -> {
|
case "--master", "-mm" -> {
|
||||||
i++;
|
i++;
|
||||||
master = Long.parseLong(args[i]);
|
config.trustedMaster = Long.parseLong(args[i]);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
case "--trusted-chat", "-trs" -> {
|
case "--trusted-chat", "-trs" -> {
|
||||||
i++;
|
i++;
|
||||||
trustedChat = Long.parseLong(args[i]);
|
config.trustedChat = Long.parseLong(args[i]);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
//noinspection SpellCheckingInspection
|
//noinspection SpellCheckingInspection
|
||||||
case "--trusted-reader-dinner", "-trsd" -> {
|
case "--trusted-reader-dinner", "-trsd" -> {
|
||||||
i++;
|
i++;
|
||||||
trustedReadersOfDinner.add(Long.parseLong(args[i]));
|
config.dinnerTrustedReaders.add(Long.parseLong(args[i]));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
case "--auto-cmd", "-cmd", "-c" -> {
|
case "--auto-cmd", "-cmd", "-c" -> {
|
||||||
autoCmdList = true;
|
config.commandLoginRefresh = true;
|
||||||
autoCmdRemove = true;
|
config.commandLogoutClear = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
case "--auto-cmd-list", "-ca" -> {
|
case "--auto-cmd-list", "-ca" -> {
|
||||||
autoCmdList = true;
|
config.commandLoginRefresh = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
case "--auto-cmd-remove", "-cr" -> {
|
case "--auto-cmd-remove", "-cr" -> {
|
||||||
autoCmdRemove = true;
|
config.commandLogoutClear = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
case "--api", "-a" -> {
|
case "--api", "-a" -> {
|
||||||
i++;
|
i++;
|
||||||
api = args[i];
|
config.telegramBotApiServer = args[i];
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
case "--api-files", "files-api", "-af" -> {
|
case "--api-files", "files-api", "-af" -> {
|
||||||
i++;
|
i++;
|
||||||
api4File = args[i];
|
config.telegramBotApiServer4File = args[i];
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -180,7 +168,7 @@ public class ServerMain {
|
|||||||
|
|
||||||
String propToken = null;
|
String propToken = null;
|
||||||
String propTokenKey = null;
|
String propTokenKey = null;
|
||||||
for (String iKey : new String[]{PROP_TOKEN_KEY, PROP_TOKEN_MORNY_KEY}) {
|
for (String iKey : MornyConfig.PROP_TOKEN_KEY) {
|
||||||
if (System.getenv(iKey) != null) {
|
if (System.getenv(iKey) != null) {
|
||||||
propToken = System.getenv(iKey);
|
propToken = System.getenv(iKey);
|
||||||
propTokenKey = iKey;
|
propTokenKey = iKey;
|
||||||
@ -228,21 +216,19 @@ public class ServerMain {
|
|||||||
//#
|
//#
|
||||||
|
|
||||||
if (propToken != null) {
|
if (propToken != null) {
|
||||||
key = propToken;
|
config.telegramBotKey = propToken;
|
||||||
logger.info("Parameter <token> set by EnvVar $"+propTokenKey);
|
logger.info("Parameter <token> set by EnvVar $"+propTokenKey);
|
||||||
}
|
}
|
||||||
if (key == null) {
|
|
||||||
logger.info("Parameter required has no value:\n --token.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Thread.currentThread().setName(THREAD_MORNY_INIT);
|
Thread.currentThread().setName(THREAD_MORNY_INIT);
|
||||||
MornyCoeur.main(
|
try {
|
||||||
api, api4File,
|
MornyCoeur.main(new MornyConfig(config));
|
||||||
key, username,
|
} catch (MornyConfig.CheckFailure.NullTelegramBotKey ignore) {
|
||||||
master, trustedChat, trustedReadersOfDinner,
|
logger.info("Parameter required has no value:\n --token.");
|
||||||
outdatedBlock?System.currentTimeMillis():0,
|
} catch (MornyConfig.CheckFailure e) {
|
||||||
autoCmdList, autoCmdRemove
|
logger.error("Unknown failure occurred while starting ServerMain!:");
|
||||||
);
|
e.printStackTrace(System.out);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package cc.sukazyo.cono.morny.bot.event;
|
package cc.sukazyo.cono.morny.bot.event;
|
||||||
|
|
||||||
import cc.sukazyo.cono.morny.MornyCoeur;
|
import cc.sukazyo.cono.morny.MornyCoeur;
|
||||||
import cc.sukazyo.cono.morny.MornyTrusted;
|
|
||||||
import cc.sukazyo.cono.morny.bot.api.EventListener;
|
import cc.sukazyo.cono.morny.bot.api.EventListener;
|
||||||
import cc.sukazyo.cono.morny.data.TelegramStickers;
|
import cc.sukazyo.cono.morny.data.TelegramStickers;
|
||||||
import cc.sukazyo.cono.morny.util.CommonFormat;
|
import cc.sukazyo.cono.morny.util.CommonFormat;
|
||||||
@ -27,10 +26,10 @@ public class OnCallMe extends EventListener {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 主人的 telegram user id,同时被用于 chat id<br>
|
* 主人的 telegram user id,同时被用于 chat id<br>
|
||||||
* 跟随 {@link MornyTrusted#MASTER} 的值
|
* 跟随 {@link cc.sukazyo.cono.morny.MornyConfig#trustedMaster} 的值
|
||||||
* @since 0.4.2.1
|
* @since 0.4.2.1
|
||||||
*/
|
*/
|
||||||
private static final long ME = MornyCoeur.trustedInstance().MASTER;
|
private static final long ME = MornyCoeur.config().trustedMaster;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 监听私聊 bot 的消息进行呼叫关键字匹配。
|
* 监听私聊 bot 的消息进行呼叫关键字匹配。
|
||||||
@ -112,7 +111,7 @@ public class OnCallMe extends EventListener {
|
|||||||
boolean isAllowed = false;
|
boolean isAllowed = false;
|
||||||
Message lastDinnerData = null;
|
Message lastDinnerData = null;
|
||||||
if (MornyCoeur.trustedInstance().isTrustedForDinnerRead(event.message().from().id())) {
|
if (MornyCoeur.trustedInstance().isTrustedForDinnerRead(event.message().from().id())) {
|
||||||
lastDinnerData = MornyCoeur.extra().exec(new GetChat(MornyCoeur.DINNER_CHAT_ID)).chat().pinnedMessage();
|
lastDinnerData = MornyCoeur.extra().exec(new GetChat(MornyCoeur.config().dinnerChatId)).chat().pinnedMessage();
|
||||||
SendResponse sendResp = MornyCoeur.extra().exec(new ForwardMessage(
|
SendResponse sendResp = MornyCoeur.extra().exec(new ForwardMessage(
|
||||||
event.message().from().id(),
|
event.message().from().id(),
|
||||||
lastDinnerData.forwardFromChat().id(),
|
lastDinnerData.forwardFromChat().id(),
|
||||||
|
@ -7,7 +7,7 @@ import com.pengrad.telegrambot.model.Update;
|
|||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 阻止 {@link MornyCoeur#latestEventTimestamp 指定时间} 之前的事件处理.
|
* 阻止 {@link cc.sukazyo.cono.morny.MornyConfig#eventOutdatedTimestamp 指定时间} 之前的事件处理.
|
||||||
* <p>
|
* <p>
|
||||||
* 只支持以下事件
|
* 只支持以下事件
|
||||||
* <ul>
|
* <ul>
|
||||||
@ -27,7 +27,7 @@ public class OnUpdateTimestampOffsetLock extends EventListener {
|
|||||||
* @since 0.4.2.7
|
* @since 0.4.2.7
|
||||||
*/
|
*/
|
||||||
public boolean isOutdated(long timestamp) {
|
public boolean isOutdated(long timestamp) {
|
||||||
return timestamp < MornyCoeur.getLatestEventTimestamp()/1000;
|
return timestamp < MornyCoeur.config().eventOutdatedTimestamp/1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user