Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加@MessageHandler注解和sendMessage方法 #24

Closed
kzw200015 opened this issue Nov 25, 2021 · 6 comments
Closed

添加@MessageHandler注解和sendMessage方法 #24

kzw200015 opened this issue Nov 25, 2021 · 6 comments
Labels
enhancement New feature or request

Comments

@kzw200015
Copy link

kzw200015 commented Nov 25, 2021

功能描述
添加@MessageHandler注解和sendMessage方法
@MessageHandler同时监听PrivateMessage和GroupMessage,sendMessage方法根据Message类型和来源发送消息。比如是PrivateMessage就sendPrivateMessage到Event的userId

@kzw200015 kzw200015 added the enhancement New feature or request label Nov 25, 2021
@MisakaTAT
Copy link
Owner

之前有考虑过,后面太忙就鸽了,可以考虑添加,我这两天写一下,感谢建议。

@MisakaTAT
Copy link
Owner

已实现,版本为 v1.1.8,目前 Release 到 Maven 仓库了,需要过几个小时等待同步。

@kzw200015
Copy link
Author

新的sendMsg方法感觉有点不合理,targetId仍然需要调用方判断消息类型,传入相应的userId或者groupId。这样的话方法内的消息类型判断就没什么意义了,实际使用起来和之前相比并没有更加方便
类似这样的实现会不会更好

fun WholeMessageEvent.sendMsg(bot: Bot, msg: String, autoEscape: Boolean): ActionData<MsgId>? {
    return when (this.messageType) {
        "group" -> bot.sendGroupMsg(this.groupId, msg, autoEscape)
        "private" -> bot.sendPrivateMsg(this.userId, msg, autoEscape)
        else -> null
    }
}

@MisakaTAT
Copy link
Owner

感谢指正,目前改了一版,你觉得如何?

@MessageHandler
fun test(bot: Bot, event: WholeMessageEvent) {
    bot.sendMsg(event, "hello", false)
}

/**
* 发送消息
*
* @param event {@link WholeMessageEvent}
* @param msg 要发送的内容
* @param autoEscape 消息内容是否作为纯文本发送 ( 即不解析 CQ 码 ) , 只在 message 字段是字符串时有效
* @return {@link ActionData} of {@link MsgId}
*/
public ActionData<MsgId> sendMsg(WholeMessageEvent event, String msg, boolean autoEscape) {
switch (event.getMessageType()) {
case "private": {
return sendPrivateMsg(event.getUserId(), msg, autoEscape);
}
case "group": {
return sendGroupMsg(event.getGroupId(), msg, autoEscape);
}
default:
}
return null;
}

@kzw200015
Copy link
Author

挺好的,这样就更加方便了

@MisakaTAT
Copy link
Owner

MisakaTAT commented Nov 28, 2021

今天晚上会再发一版,v1.1.9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants