Compare commits

..

No commits in common. "985fde9aa2eff50e8333ef976fbb39c9f4a2e0b8" and "bfacb0d039f46df5bd22e4e276740e40111dfbb6" have entirely different histories.

19 changed files with 71 additions and 198 deletions

View File

@ -4,22 +4,19 @@
[todo]: https://github.com/users/Eyre-S/projects/1
[artifact]: https://mvn.sukazyo.cc/#/releases/cc/sukazyo/morny-coeur
[scala]: https://www.scala-lang.org/
[spotbugs]: https://spotbugs.github.io/
[tg4j]: https://github.com/pengrad/java-telegram-bot-api
[okhttp]: https://square.github.io/okhttp/
[gson]: https://github.com/google/gson
[scalatest]: https://scalatest.org/
[spotbugs]: https://spotbugs.github.io/
[junit5]: https://junit.org/junit5/
<div align=center>
# ~~给所有喜欢morny的大家的~~ Morny Coeur 源代码
~~"and nobody cares."~~
~~"你们又有意见又不发issue这样子我很为难的啊"~~
![social preview card](morny-github-social-preview-card@0.75x.png)
一个 telegram 上的服侍 A.C.Sukazyo Eyre 和它的花宫成员的 bot 内核
一个 telegram 上的服侍 A.C.Sukazyo Eyre 和它的花宫成员的 bot 内核
[Task Listing][todo] | [~~BBS~~][issues] | [Published][artifact]
@ -35,9 +32,6 @@
[Java Telegram Bot API][tg4j]
[okhttp] | [Gson][gson]
[Scala][scala] | [SpotBugs Annotations][spotbugs] | [ScalaTest][scalatest]
[SpotBugs Annotations][spotbugs] | [JUnit 5][junit5]
</div>

View File

@ -92,10 +92,6 @@ dependencies {
}
java {
withSourcesJar()
}
tasks.withType(JavaCompile).configureEach {
sourceCompatibility proj_java.getMajorVersion()
@ -148,12 +144,9 @@ buildConfig {
}
tasks.withType(Jar).configureEach {
archiveBaseName.set proj_archive_name
}
shadowJar {
archiveBaseName.set proj_archive_name
archiveClassifier.set "fat"
if (project.hasProperty("dockerBuild")) {

View File

@ -7,8 +7,8 @@ MORNY_COMMIT_PATH = https://github.com/Eyre-S/Coeur-Morny-Cono/commit/%s
VERSION = 1.0.0-RC5
USE_DELTA = false
VERSION_DELTA =
USE_DELTA = true
VERSION_DELTA = scala3
CODENAME = beiping

View File

@ -31,12 +31,10 @@ class MornyCoeur (using val config: MornyConfig) {
if config.telegramBotUsername ne null then
logger info s"login as:\n ${config.telegramBotUsername}"
private val __loginResult: LoginResult = login() match
case some: Some[LoginResult] => some.get
case None =>
logger error "Login to bot failed."
System exit -1
throw RuntimeException()
private val __loginResult = login()
if (__loginResult eq null)
logger error "Login to bot failed."
System exit -1
configure_exitCleanup()
@ -65,8 +63,8 @@ class MornyCoeur (using val config: MornyConfig) {
val events: MornyEventListeners = MornyEventListeners(using eventManager)
/** inner value: about why morny exit, used in [[daemon.MornyReport]]. */
private var whileExit_reason: Option[AnyRef] = None
def exitReason: Option[AnyRef] = whileExit_reason
private var whileExit_reason: AnyRef|Null = _
def exitReason: AnyRef|Null = whileExit_reason
val coeurStartTimestamp: Long = ServerMain.systemStartupTime
///>>> BLOCK START instance configure & startup stage 2
@ -103,12 +101,12 @@ class MornyCoeur (using val config: MornyConfig) {
}
def exit (status: Int, reason: AnyRef): Unit =
whileExit_reason = Some(reason)
whileExit_reason = reason
System exit status
private case class LoginResult(account: TelegramBot, username: String, userid: Long)
private def login (): Option[LoginResult] = {
private def login (): LoginResult|Null = {
val builder = TelegramBot.Builder(config.telegramBotKey)
var api_bot = config.telegramBotApiServer
@ -131,7 +129,7 @@ class MornyCoeur (using val config: MornyConfig) {
val account = builder build
logger info "Trying to login..."
boundary[Option[LoginResult]] {
boundary[LoginResult|Null] {
for (i <- 0 to 3) {
if i > 0 then logger info "retrying..."
try {
@ -139,16 +137,16 @@ class MornyCoeur (using val config: MornyConfig) {
if ((config.telegramBotUsername ne null) && config.telegramBotUsername != remote.username)
throw RuntimeException(s"Required the bot @${config.telegramBotUsername} but @${remote.username} logged in")
logger info s"Succeed logged in to @${remote.username}"
break(Some(LoginResult(account, remote.username, remote.id)))
break(LoginResult(account, remote.username, remote.id))
} catch
case r: boundary.Break[Option[LoginResult]] => throw r
case r: boundary.Break[LoginResult|Null] => throw r
case e =>
logger error
s"""${exceptionLog(e)}
|login failed"""
.stripMargin
}
None
null
}
}

View File

@ -10,12 +10,6 @@ import com.pengrad.telegrambot.UpdatesListener
import scala.collection.mutable
import scala.language.postfixOps
/** Contains a [[mutable.Queue]] of [[EventListener]], and delivery telegram [[Update]].
*
* Implemented [[process]] in [[UpdatesListener]] so it can directly used in [[com.pengrad.telegrambot.TelegramBot.setupListener]].
*
* @param coeur the [[MornyCoeur]] context.
*/
class EventListenerManager (using coeur: MornyCoeur) extends UpdatesListener {
private val listeners = mutable.Queue.empty[EventListener]
@ -83,19 +77,9 @@ class EventListenerManager (using coeur: MornyCoeur) extends UpdatesListener {
}
import java.util
import scala.jdk.CollectionConverters.*
/** Delivery the telegram [[Update]]s.
*
* The implementation of [[UpdatesListener]].
*
* For each [[Update]], create an [[EventRunner]] for it, and
* start the it.
*
* @return [[UpdatesListener.CONFIRMED_UPDATES_ALL]], for all Updates
* should be processed in [[EventRunner]] created for it.
*/
override def process (updates: util.List[Update]): Int = {
for (update <- updates.asScala)
EventRunner(using update).start()

View File

@ -1,43 +1,17 @@
package cc.sukazyo.cono.morny.bot.command
/** One alias definition, contains the necessary message of how
* to process the alias.
*/
trait ICommandAlias {
/** The alias name.
*
* same with the command name, it is the unique identifier of this alias.
*/
val name: String
/** If the alias should be listed while list commands to end-user.
*
* The alias can only be listed when the parent command can be listed
* (meanwhile the parent command implemented [[ITelegramCommand]]). If the
* parent command cannot be listed, it will always cannot be listed.
*/
val listed: Boolean
}
/** Default implementations of [[ICommandAlias]]. */
object ICommandAlias {
/** Alias which can be listed to end-user.
*
* the [[ICommandAlias.listed]] value is always true.
*
* @param name The alias name, see more in [[ICommandAlias.name]]
*/
case class ListedAlias (name: String) extends ICommandAlias:
override val listed = true
/** Alias which cannot be listed to end-user.
*
* the [[ICommandAlias.listed]] value is always false.
*
* @param name The alias name, see more in [[ICommandAlias.name]]
*/
case class HiddenAlias (name: String) extends ICommandAlias:
override val listed = false

View File

@ -3,37 +3,11 @@ package cc.sukazyo.cono.morny.bot.command
import cc.sukazyo.cono.morny.util.tgapi.InputCommand
import com.pengrad.telegrambot.model.Update
/** A simple command.
*
* Contains only [[name]] and [[aliases]].
*
* Won't be listed to end-user. if you want the command listed,
* see [[ITelegramCommand]].
*
*/
trait ISimpleCommand {
/** the main name of the command.
*
* must have a value as the unique identifier of this command.
*/
val name: String
/** aliases of the command.
*
* Alias means it is the same to call [[name main name]] when call this.
* There can be multiple aliases. But notice that, although alias is not
* the unique identifier, it uses the same namespace with [[name]], means
* it also cannot be duplicate with other [[name]] or [[aliases]].
*
* It can be [[Null]], means no aliases.
*/
val aliases: Array[ICommandAlias]|Null
/** The work code of this command.
*
* @param command The parsed input command which called this command.
* @param event The raw event which called this command.
*/
def execute (using command: InputCommand, event: Update): Unit
}

View File

@ -1,25 +1,8 @@
package cc.sukazyo.cono.morny.bot.command
/** A complex telegram command.
*
* the extension of [[ISimpleCommand]], with external defines of the necessary
* introduction message ([[paramRule]] and [[description]]).
*
* It can be listed to end-user.
*/
trait ITelegramCommand extends ISimpleCommand {
/** The param rule of this command, used in human-readable command list.
*
* The param rule uses a symbol language to describe how this command
* receives paras.
*
* Set it empty to make this scope not available.
*/
val paramRule: String
/** The description/introduction of this command, used in human-readable
* command list.
*/
val description: String
}

View File

@ -122,7 +122,7 @@ class MornyInformation (using coeur: MornyCoeur) extends ITelegramCommand {
event.message.chat.id,
/* language=html */
s"""system:
|- <code>${h(if getRuntimeHostname nonEmpty then getRuntimeHostname.get else "<unknown-host>")}</code>
|- <code>${h(if (getRuntimeHostname == null) "<unknown-host>" else getRuntimeHostname)}</code>
|- <code>${h(sysprop("os.name"))}</code> <code>${h(sysprop("os.arch"))}</code> <code>${h(sysprop("os.version"))}</code>
|java runtime:
|- <code>${h(sysprop("java.vm.vendor"))}.${h(sysprop("java.vm.name"))}</code>

View File

@ -25,17 +25,19 @@ class Nbnhhsh (using coeur: MornyCoeur) extends ITelegramCommand {
override def execute (using command: InputCommand, event: Update): Unit = {
val queryTarget: String =
val queryTarget: String|Null =
if command.args nonEmpty then
command.args mkString " "
else if (event.message.replyToMessage != null && event.message.replyToMessage.text != null)
event.message.replyToMessage.text
else
coeur.account exec SendSticker(
event.message.chat.id,
TelegramStickers ID_404
).replyToMessageId(event.message.messageId)
return;
else null
if (queryTarget == null)
coeur.account exec SendSticker(
event.message.chat.id,
TelegramStickers ID_404
).replyToMessageId(event.message.messageId)
return;
try {

View File

@ -17,11 +17,11 @@ class OnCallMsgSend (using coeur: MornyCoeur) extends EventListener {
private val REGEX_MSG_SENDREQ_DATA_HEAD: Regex = "^\\*msg(-?\\d+)(\\*\\S+)?(?:\\n([\\s\\S]+))?$"r
case class MessageToSend (
message: String|Null,
entities: Array[MessageEntity]|Null,
parseMode: ParseMode|Null,
targetId: Long
) {
message: String|Null,
entities: Array[MessageEntity]|Null,
parseMode: ParseMode|Null,
targetId: Long
) {
def toSendMessage (target_override: Long|Null = null): SendMessage =
val useTarget = if target_override == null then targetId else target_override
val sendMessage = SendMessage(useTarget, message)
@ -129,8 +129,8 @@ class OnCallMsgSend (using coeur: MornyCoeur) extends EventListener {
}
if messageToSend.message eq null then return true
val testSendResponse = coeur.account execute
messageToSend.toSendMessage(update.message.chat.id).replyToMessageId(update.message.messageId)
val testSendResponse = coeur.account execute messageToSend.toSendMessage(update.message.chat.id)
.replyToMessageId(update.message.messageId)
if (!(testSendResponse isOk))
coeur.account exec SendMessage(
update.message.chat.id,

View File

@ -8,7 +8,6 @@ import com.pengrad.telegrambot.model.Update
import com.pengrad.telegrambot.request.SendMessage
import scala.language.postfixOps
import scala.util.boundary
class OnQuestionMarkReply (using coeur: MornyCoeur) extends EventListener {
@ -35,10 +34,10 @@ object OnQuestionMarkReply {
private val QUESTION_MARKS = Set('?', '', '¿', '⁈', '⁇', '‽', '❔', '❓')
def isAllMessageMark (using text: String): Boolean = {
boundary[Boolean] {
for (c <- text) if QUESTION_MARKS contains c then boundary.break(false)
true
}
var isAll = true
for (c <- text)
if !(QUESTION_MARKS contains c) then isAll = false
isAll
}
}

View File

@ -3,7 +3,6 @@ package cc.sukazyo.cono.morny.bot.query
import cc.sukazyo.cono.morny.Log.logger
import cc.sukazyo.cono.morny.util.tgapi.formatting.NamingUtils.inlineQueryId
import cc.sukazyo.cono.morny.util.BiliTool
import cc.sukazyo.cono.morny.util.UseSelect.select
import com.pengrad.telegrambot.model.Update
import com.pengrad.telegrambot.model.request.{InlineQueryResultArticle, InputTextMessageContent, ParseMode}
@ -25,23 +24,23 @@ class ShareToolBilibili extends ITelegramQuery {
if (event.inlineQuery.query == null) return null
event.inlineQuery.query match
case REGEX_BILI_VIDEO(_url_v, _url_av, _url_bv, _url_param, _url_v_part, _raw_av, _raw_bv) =>
case REGEX_BILI_VIDEO(_1, _2, _3, _4, _5, _6, _7) =>
logger debug
s"""====== Share Tool Bilibili Catch ok
|1: ${_url_v}
|2: ${_url_av}
|3: ${_url_bv}
|4: ${_url_param}
|5: ${_url_v_part}
|6: ${_raw_av}
|7: ${_raw_bv}"""
|1: ${_1}
|2: ${_2}
|3: ${_3}
|4: ${_4}
|5: ${_5}
|6: ${_6}
|7: ${_7}"""
.stripMargin
var av = select(_url_av, _raw_av)
var bv = select(_url_bv, _raw_bv)
var av = if (_2 != null) _2 else if (_6 != null) _6 else null
var bv = if (_3!=null) _3 else if (_7!=null) _7 else null
logger trace s"catch id av[$av] bv[$bv]"
val part: Int|Null = if (_url_v_part!=null) _url_v_part toInt else null
val part: Int|Null = if (_5!=null) _5 toInt else null
logger trace s"catch video part[$part]"
if (av == null) {

View File

@ -21,15 +21,15 @@ class ShareToolTwitter extends ITelegramQuery {
event.inlineQuery.query match
case REGEX_TWEET_LINK(_, _path_data, _, _, _, _) =>
case REGEX_TWEET_LINK(_, _2, _, _, _, _) =>
List(
InlineQueryUnit(InlineQueryResultArticle(
inlineQueryId(ID_PREFIX_VX+event.inlineQuery.query), TITLE_VX,
s"https://vxtwitter.com/$_path_data"
s"https://vxtwitter.com/$_2"
)),
InlineQueryUnit(InlineQueryResultArticle(
inlineQueryId(ID_PREFIX_VX_COMBINED+event.inlineQuery.query), TITLE_VX_COMBINED,
s"https://c.vxtwitter.com/$_path_data"
s"https://c.vxtwitter.com/$_2"
))
)

View File

@ -24,7 +24,7 @@ class MedicationTimer (using coeur: MornyCoeur) extends Thread {
this.setName(DAEMON_THREAD_NAME_DEF)
private var lastNotify_messageId: Option[Int] = None
private var lastNotify_messageId: Int|Null = _
override def run (): Unit = {
logger info "Medication Timer started."
@ -51,8 +51,8 @@ class MedicationTimer (using coeur: MornyCoeur) extends Thread {
private def sendNotification(): Unit = {
val sendResponse: SendResponse = coeur.account exec SendMessage(notify_toChat, NOTIFY_MESSAGE)
if sendResponse isOk then lastNotify_messageId = Some(sendResponse.message.messageId)
else lastNotify_messageId = None
if sendResponse isOk then lastNotify_messageId = sendResponse.message.messageId
else lastNotify_messageId = null
}
@throws[InterruptedException | IllegalArgumentException]
@ -61,7 +61,7 @@ class MedicationTimer (using coeur: MornyCoeur) extends Thread {
}
def refreshNotificationWrite (edited: Message): Unit = {
if (lastNotify_messageId isEmpty) || (lastNotify_messageId.get != (edited.messageId toInt)) then return
if lastNotify_messageId != (edited.messageId toInt) then return
import cc.sukazyo.cono.morny.util.CommonFormat.formatDate
val editTime = formatDate(edited.editDate*1000, use_timeZone.getTotalSeconds/60/60)
val entities = ArrayBuffer.empty[MessageEntity]
@ -72,7 +72,7 @@ class MedicationTimer (using coeur: MornyCoeur) extends Thread {
edited.messageId,
edited.text + s"\n-- $editTime --"
).entities(entities toArray:_*)
lastNotify_messageId = None
lastNotify_messageId = null
}
}

View File

@ -101,9 +101,9 @@ class MornyReport (using coeur: MornyCoeur) {
def reportCoeurExit (): Unit = {
val causedTag = coeur.exitReason match
case None => "UNKNOWN reason"
case u: Some[User] => u.get.fullnameRefHTML
case a: Some[_] => /*language=html*/ s"<code>${h(a.get.toString)}</code>"
case u: User => u.fullnameRefHTML
case n if n == null => "UNKNOWN reason"
case a: AnyRef => /*language=html*/ s"<code>${h(a.toString)}</code>"
executeReport(SendMessage(
coeur.config.reportToChat,
// language=html

View File

@ -29,9 +29,9 @@ object MornyInformation {
}
//noinspection ScalaWeakerAccess
def getRuntimeHostname: Option[String] = {
try Some(InetAddress.getLocalHost.getHostName)
catch case _: UnknownHostException => None
def getRuntimeHostname: String | Null = {
try InetAddress.getLocalHost.getHostName
catch case _: UnknownHostException => null
}
def getAboutPic: Array[Byte] = TelegramImages.IMG_ABOUT get

View File

@ -13,17 +13,17 @@ object TelegramImages {
class AssetsFileImage (assetsPath: String) {
private var cache: Option[Array[Byte]] = None
private var cache: Array[Byte]|Null = _
@throws[AssetsException]
def get:Array[Byte] =
if cache isEmpty then read()
cache.get
if cache eq null then read()
cache
@throws[AssetsException]
private def read (): Unit = {
Using ((MornyAssets.pack getResource assetsPath)read) { stream =>
try { this.cache = Some(stream.readAllBytes()) }
try { this.cache = stream.readAllBytes() }
catch case e: IOException => {
throw AssetsException(e)
}

View File

@ -1,27 +0,0 @@
package cc.sukazyo.cono.morny.util
import scala.util.boundary
/** Useful utils of select one specific value in the given values.
*
* contains:
* - [[select()]] can select one value which is not [[Null]].
*
*/
object UseSelect {
/** Select the non-null value in the given values.
*
* @tparam T The value's type.
* @param values Given values, may be a T value or [[Null]].
* @return The first non-null value in the given values, or [[Null]] if
* there's no non-null value.
*/
def select [T] (values: T|Null*): T|Null = {
boundary[T|Null] {
for (i <- values) if i != null then boundary.break(i)
null
}
}
}