抽离 常用key到 mj constants 里面

This commit is contained in:
cherishsince 2024-04-07 09:43:05 +08:00
parent defe628026
commit 3fe04ac98e
2 changed files with 43 additions and 14 deletions

View File

@ -2,7 +2,36 @@ package cn.iocoder.yudao.framework.ai.midjourney.constants;
public final class MjConstants {
public static final String CHANNEL_ID = "channel_id";
/**
* 消息 - 编号
*/
public static final String MSG_ID = "id";
/**
* 消息 - 类型
* 现在已知
* 0我们发送的消息和指令
* 20: mj生成图片发送过程中
* 19: 选择了某一张图片后的通知
*/
public static final String MSG_TYPE = "type";
/**
* 平道id
*/
public static final String MSG_CHANNEL_ID = "channel_id";
/**
* 内容
*
* "**南极应该是什么样子? --v 6.0 --style raw** - <@972721304891453450> (32%) (fast, stealth)",
*/
public static final String MSG_CONTENT = "content";
/**
* 组件(图片生成好之后才有)
*/
public static final String MSG_COMPONENTS = "components";
/**
* 附件(生成中比较模糊的图片)
*/
public static final String MSG_ATTACHMENTS = "attachments";
}

View File

@ -12,12 +12,9 @@ import cn.iocoder.yudao.framework.ai.midjourney.constants.MjGennerateStatusEnum;
import cn.iocoder.yudao.framework.ai.midjourney.constants.MjMessageTypeEnum;
import cn.iocoder.yudao.framework.ai.midjourney.util.MjUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.utils.data.DataObject;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;
@Slf4j
@ -39,25 +36,28 @@ public class MjMessageListener {
return;
}
// 转换几个重要的信息
MjMessage mjMessage = new MjMessage();
mjMessage.setId(data.getString("id"));
mjMessage.setType(data.getInt("type"));
mjMessage.setId(data.getString(MjConstants.MSG_ID));
mjMessage.setType(data.getInt(MjConstants.MSG_TYPE));
mjMessage.setRawData(StrUtil.str(raw.toJson(), "UTF-8"));
mjMessage.setContent(MjUtil.parseContent(data.getString("content")));
if (!data.getArray("components").isEmpty()) {
String componentsJson = StrUtil.str(data.getArray("components").toJson(), "UTF-8");
mjMessage.setContent(MjUtil.parseContent(data.getString(MjConstants.MSG_CONTENT)));
// 转换 components
if (!data.getArray(MjConstants.MSG_COMPONENTS).isEmpty()) {
String componentsJson = StrUtil.str(data.getArray(MjConstants.MSG_COMPONENTS).toJson(), "UTF-8");
List<MjMessage.ComponentType> components = JSON.parseArray(componentsJson, MjMessage.ComponentType.class);
mjMessage.setComponents(components);
}
if (!data.getArray("attachments").isEmpty()) {
String attachmentsJson = StrUtil.str(data.getArray("attachments").toJson(), "UTF-8");
// 转换附件
if (!data.getArray(MjConstants.MSG_ATTACHMENTS).isEmpty()) {
String attachmentsJson = StrUtil.str(data.getArray(MjConstants.MSG_ATTACHMENTS).toJson(), "UTF-8");
List<MjMessage.Attachment> attachments = JSON.parseArray(attachmentsJson, MjMessage.Attachment.class);
mjMessage.setAttachments(attachments);
}
// 转换状态
convertGenerateStatus(mjMessage);
//
log.info("message 信息 {}", JSONUtil.toJsonPrettyStr(mjMessage));
System.err.println(JSONUtil.toJsonPrettyStr(mjMessage));
}
@ -72,7 +72,7 @@ public class MjMessageListener {
}
private boolean ignoreAndLogMessage(DataObject data, MjMessageTypeEnum messageType) {
String channelId = data.getString(MjConstants.CHANNEL_ID);
String channelId = data.getString(MjConstants.MSG_CHANNEL_ID);
if (!CharSequenceUtil.equals(channelId, midjourneyConfig.getChannelId())) {
return true;
}