mirror of
https://github.com/Eyre-S/Coeur-Morny-Cono.git
synced 2024-11-25 04:27:41 +08:00
添加根包中的javadoc,修复typo,将版本号更新独立出gradle任务
- typo: Log#waring -< Log#warning
This commit is contained in:
parent
5e22dc0b8f
commit
1844c386c8
@ -7,7 +7,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group 'cc.sukazyo'
|
group 'cc.sukazyo'
|
||||||
version '0.3.4'
|
version '0.3.4.1'
|
||||||
project.ext.archiveBaseName = 'Coeur_Morny_Cono'
|
project.ext.archiveBaseName = 'Coeur_Morny_Cono'
|
||||||
project.ext.artifactId = 'morny-coeur'
|
project.ext.artifactId = 'morny-coeur'
|
||||||
mainClassName = 'cc.sukazyo.cono.morny.MornyCoeur'
|
mainClassName = 'cc.sukazyo.cono.morny.MornyCoeur'
|
||||||
@ -27,12 +27,14 @@ dependencies {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
compileJava.doFirst {
|
task updateVersionCode {
|
||||||
ant.replaceregexp(match:'VERSION = ["a-zA-Z0-9.\\-_+@]+;', replace:"VERSION = \"$project.version\";", flags:'g', byline:true) {
|
ant.replaceregexp(match:'VERSION = ["a-zA-Z0-9.\\-_+@]+;', replace:"VERSION = \"$project.version\";", flags:'g', byline:true) {
|
||||||
fileset(dir: 'src/main/java/cc/sukazyo/cono/morny', includes: 'MornySystem.java')
|
fileset(dir: 'src/main/java/cc/sukazyo/cono/morny', includes: 'MornySystem.java')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
compileJava.dependsOn updateVersionCode
|
||||||
|
|
||||||
test {
|
test {
|
||||||
useJUnitPlatform()
|
useJUnitPlatform()
|
||||||
}
|
}
|
||||||
|
@ -4,26 +4,71 @@ import cc.sukazyo.cono.morny.util.StringUtils;
|
|||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Morny 的简单控制台 log 记录器
|
||||||
|
*/
|
||||||
public class Logger {
|
public class Logger {
|
||||||
|
|
||||||
|
/** Morny 的控制台 logger 实例 */
|
||||||
public static final Logger logger = new Logger();
|
public static final Logger logger = new Logger();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [INFO] 级别的 log 消息
|
||||||
|
* @see #formatMessage(String, String) 输出整理规则
|
||||||
|
* @param message 消息文本,支持多行
|
||||||
|
*/
|
||||||
public void info(@Nonnull String message) {
|
public void info(@Nonnull String message) {
|
||||||
System.out.println(formatMessage(message, "INFO"));
|
System.out.println(formatMessage(message, "INFO"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [WARN] 级别的 log 消息<br>
|
||||||
|
* {@link #warning(String)} 的重写
|
||||||
|
* @see #formatMessage(String, String) 输出整理规则
|
||||||
|
* @see #warning(String) 源方法
|
||||||
|
* @param message 消息文本,支持多行
|
||||||
|
*/
|
||||||
public void warn (@Nonnull String message) {
|
public void warn (@Nonnull String message) {
|
||||||
waring(message);
|
warning(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void waring (@Nonnull String message) {
|
/**
|
||||||
|
* [WARN] 级别的 log 消息
|
||||||
|
* @see #formatMessage(String, String) 输出整理规则
|
||||||
|
* @see #warn(String) 别名:warn
|
||||||
|
* @param message 消息文本,支持多行
|
||||||
|
*/
|
||||||
|
public void warning (@Nonnull String message) {
|
||||||
System.out.println(formatMessage(message, "WARN"));
|
System.out.println(formatMessage(message, "WARN"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [ERRO] 级别的 log 消息
|
||||||
|
* @see #formatMessage(String, String) 输出整理规则
|
||||||
|
* @param message 消息文本,支持多行
|
||||||
|
*/
|
||||||
public void error (@Nonnull String message) {
|
public void error (@Nonnull String message) {
|
||||||
System.out.println(formatMessage(message, "ERRO"));
|
System.out.println(formatMessage(message, "ERRO"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将传入的消息和消息元数据重新整理为固定格式<br>
|
||||||
|
* <br>
|
||||||
|
* 这个方法会{@link System#currentTimeMillis() 获取当前时间}和{@link Thread#currentThread() 当前线程}的名称,
|
||||||
|
* 然后将数据整理为 {@code [<timestamp>][<thread-name>][<level>]<data>} 格式。<br>
|
||||||
|
* 如果消息是多行的,则每行的开头都会被加入 {@code [<timestamp>][<thread-name>][<level>]} 这样的前缀,
|
||||||
|
* 不过,前缀中 {@code [<timestamp>][<thread-name>]} 会被转换为等长的 {@code '} 字串。
|
||||||
|
* <br>
|
||||||
|
* 最终的 format 格式将会类似于以下的模样:<pre><code>
|
||||||
|
[1019284827][EVT388223][INFO]Something message got:
|
||||||
|
'''''''''''''''''''''''[INFO] - data source: 19773
|
||||||
|
'''''''''''''''''''''''[INFO] - message raw: noh2q0jwd9j-jn-9jq92-ed
|
||||||
|
* </code></pre>
|
||||||
|
*
|
||||||
|
* @param message 消息文本,支持多行
|
||||||
|
* @param level log级别,考虑到对齐,推荐使用四位窄字元
|
||||||
|
* @return 整理后的字符串
|
||||||
|
*/
|
||||||
@Nonnull
|
@Nonnull
|
||||||
private String formatMessage (@Nonnull String message, @Nonnull String level) {
|
private String formatMessage (@Nonnull String message, @Nonnull String level) {
|
||||||
final String prompt = String.format("[%s][%s]", System.currentTimeMillis(), Thread.currentThread().getName());
|
final String prompt = String.format("[%s][%s]", System.currentTimeMillis(), Thread.currentThread().getName());
|
||||||
|
@ -10,11 +10,34 @@ import javax.annotation.Nonnull;
|
|||||||
|
|
||||||
import static cc.sukazyo.cono.morny.Logger.logger;
|
import static cc.sukazyo.cono.morny.Logger.logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Morny Cono 核心<br>
|
||||||
|
* <br>
|
||||||
|
* - 的程序化入口类,保管着 morny 的核心属性<br>
|
||||||
|
*/
|
||||||
public class MornyCoeur {
|
public class MornyCoeur {
|
||||||
|
|
||||||
|
/** morny 的 bot 账户 */
|
||||||
private static TelegramBot account;
|
private static TelegramBot account;
|
||||||
|
/**
|
||||||
|
* morny 的 bot 账户的用户名<br>
|
||||||
|
* <br>
|
||||||
|
* 出于技术限制,这个字段目前是写死的
|
||||||
|
*/
|
||||||
public static final String USERNAME = "morny_cono_annie_bot";
|
public static final String USERNAME = "morny_cono_annie_bot";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 程序入口<br>
|
||||||
|
* <br>
|
||||||
|
* 会从命令行参数取得初始化数据并初始化程序和bot<br>
|
||||||
|
* <br>
|
||||||
|
* - 第一个参数({@code args[0]})必序传递,值为 telegram-bot 的 api-token<br>
|
||||||
|
* - 第二个参数可选 {@code --no-hello} 和 {@code --only-hello},
|
||||||
|
* 前者表示不输出{@link MornyHello#MORNY_PREVIEW_IMAGE_ASCII 欢迎标语},
|
||||||
|
* 后者表示只输出{@link MornyHello#MORNY_PREVIEW_IMAGE_ASCII 欢迎标语}而不运行程序逻辑
|
||||||
|
*
|
||||||
|
* @param args 程序命令行参数
|
||||||
|
*/
|
||||||
public static void main (@Nonnull String[] args) {
|
public static void main (@Nonnull String[] args) {
|
||||||
|
|
||||||
if (!(args.length > 1 && "--no-hello".equals(args[1])))
|
if (!(args.length > 1 && "--no-hello".equals(args[1])))
|
||||||
@ -40,15 +63,31 @@ public class MornyCoeur {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用于退出时进行缓存的任务处理等进行安全退出
|
||||||
|
*/
|
||||||
private static void exitCleanup () {
|
private static void exitCleanup () {
|
||||||
TrackerDataManager.DAEMON.interrupt();
|
TrackerDataManager.DAEMON.interrupt();
|
||||||
TrackerDataManager.trackingLock.lock();
|
TrackerDataManager.trackingLock.lock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为程序在虚拟机上添加退出钩子
|
||||||
|
*/
|
||||||
private static void configureSafeExit () {
|
private static void configureSafeExit () {
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(MornyCoeur::exitCleanup));
|
Runtime.getRuntime().addShutdownHook(new Thread(MornyCoeur::exitCleanup));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录 bot<br>
|
||||||
|
* <br>
|
||||||
|
* 会反复尝试三次进行登录。如果登录失败,则会直接抛出 RuntimeException 结束处理。
|
||||||
|
* 会通过 GetMe 动作验证是否连接上了 telegram api 服务器,
|
||||||
|
* 同时也要求登录获得的 username 和 {@link #USERNAME} 声明值相等
|
||||||
|
*
|
||||||
|
* @param key bot 的 api-token
|
||||||
|
* @return 成功登录后的 {@link TelegramBot} 对象
|
||||||
|
*/
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public static TelegramBot login (@Nonnull String key) {
|
public static TelegramBot login (@Nonnull String key) {
|
||||||
final TelegramBot account = new TelegramBot(key);
|
final TelegramBot account = new TelegramBot(key);
|
||||||
@ -69,6 +108,11 @@ public class MornyCoeur {
|
|||||||
throw new RuntimeException("Login failed..");
|
throw new RuntimeException("Login failed..");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取登录成功后的 telegram bot 对象
|
||||||
|
*
|
||||||
|
* @return {@link #account MornyCoeur.account}
|
||||||
|
*/
|
||||||
public static TelegramBot getAccount () {
|
public static TelegramBot getAccount () {
|
||||||
return account;
|
return account;
|
||||||
}
|
}
|
||||||
|
File diff suppressed because one or more lines are too long
@ -5,10 +5,28 @@ import cc.sukazyo.cono.morny.util.FileUtils;
|
|||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Morny Cono 的 Coeur 的程序属性存放类
|
||||||
|
*/
|
||||||
public class MornySystem {
|
public class MornySystem {
|
||||||
|
|
||||||
public static final String VERSION = "0.3.4";
|
/**
|
||||||
|
* 程序的语义化版本号<br>
|
||||||
|
* 会由 gradle 任务 {@code updateVersionCode} 更新
|
||||||
|
*/
|
||||||
|
public static final String VERSION = "0.3.4.1";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取程序 jar 文件的 md5-hash 值<br>
|
||||||
|
* <br>
|
||||||
|
* 只支持 jar 文件方式启动的程序 ——
|
||||||
|
* 如果是通过 classpath 来启动,则会返回找不到文件的错误数据<br>
|
||||||
|
* - 或许需要注意,这种情况下会出现程序文件所在的路径<br>
|
||||||
|
* <br>
|
||||||
|
* 值格式为 {@link java.lang.String}
|
||||||
|
*
|
||||||
|
* @return 程序jar文件的 md5-hash 值字符串,或错误信息
|
||||||
|
*/
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public static String getJarMd5() {
|
public static String getJarMd5() {
|
||||||
try {
|
try {
|
||||||
|
@ -3,10 +3,27 @@ package cc.sukazyo.cono.morny;
|
|||||||
import com.pengrad.telegrambot.model.ChatMember;
|
import com.pengrad.telegrambot.model.ChatMember;
|
||||||
import com.pengrad.telegrambot.request.GetChatMember;
|
import com.pengrad.telegrambot.request.GetChatMember;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对用户进行身份权限验证的管理类
|
||||||
|
*/
|
||||||
public class MornyTrusted {
|
public class MornyTrusted {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 群聊id,其指向的群聊指示了哪个群的成员是受信任的
|
||||||
|
* @see #isTrusted(long) 受信检查
|
||||||
|
*/
|
||||||
public static final long TRUSTED_CHAT_ID = -1001541451710L;
|
public static final long TRUSTED_CHAT_ID = -1001541451710L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用于检查一个 telegram-user 是否受信任<br>
|
||||||
|
* <br>
|
||||||
|
* 用户需要受信任才能执行一些对程序甚至是宿主环境而言危险的操作,例如关闭程序<br>
|
||||||
|
* <br>
|
||||||
|
* 它的逻辑(目前)是检查群聊 {@link #TRUSTED_CHAT_ID} 中这个用户是否为群组管理员
|
||||||
|
*
|
||||||
|
* @param userId 需要检查的用户的id
|
||||||
|
* @return 所传递的用户id对应的用户是否受信任
|
||||||
|
*/
|
||||||
public static boolean isTrusted (long userId) {
|
public static boolean isTrusted (long userId) {
|
||||||
final ChatMember chatMember = MornyCoeur.getAccount().execute(new GetChatMember(TRUSTED_CHAT_ID, userId)).chatMember();
|
final ChatMember chatMember = MornyCoeur.getAccount().execute(new GetChatMember(TRUSTED_CHAT_ID, userId)).chatMember();
|
||||||
return (
|
return (
|
||||||
|
Loading…
Reference in New Issue
Block a user