-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[5.3] 抽象化配置项模型, 调整依赖关系, 支持gocq-http框架
- Loading branch information
Showing
35 changed files
with
1,307 additions
and
481 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package moe.dituon.petpet; | ||
|
||
import moe.dituon.petpet.server.WebServer; | ||
import moe.dituon.petpet.websocket.gocq.GoCQPetpet; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
if (args.length == 0) { | ||
WebServer server = new WebServer(); | ||
return; | ||
} | ||
List<String> param = Arrays.asList(args); | ||
if (param.contains("-gocq")) { | ||
try { | ||
GoCQPetpet goCQPetpet = GoCQPetpet.getInstance(); | ||
} catch (NoClassDefFoundError e) { | ||
e.printStackTrace(); | ||
System.err.println("无法加载WebSocket依赖, 请使用分发版本"); | ||
} | ||
} | ||
} | ||
} |
160 changes: 160 additions & 0 deletions
160
src/main/java/moe/dituon/petpet/mirai/MiraiPetService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package moe.dituon.petpet.mirai; | ||
|
||
import moe.dituon.petpet.plugin.*; | ||
import moe.dituon.petpet.share.BaseConfigFactory; | ||
import moe.dituon.petpet.share.GifAvatarExtraDataProvider; | ||
import moe.dituon.petpet.share.TextExtraData; | ||
import net.mamoe.mirai.Bot; | ||
import net.mamoe.mirai.contact.Contact; | ||
import net.mamoe.mirai.contact.Group; | ||
import net.mamoe.mirai.contact.Member; | ||
import net.mamoe.mirai.contact.NormalMember; | ||
import net.mamoe.mirai.message.data.Image; | ||
import net.mamoe.mirai.utils.ExternalResource; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.stream.Collectors; | ||
|
||
public class MiraiPetService extends PluginPetService{ | ||
|
||
public byte probability = 30; | ||
public boolean respondSelfNudge = false; | ||
public int coolDown = Cooler.DEFAULT_USER_COOLDOWN; | ||
public int groupCoolDown = Cooler.DEFAULT_GROUP_COOLDOWN; | ||
public String inCoolDownMessage = Cooler.DEFAULT_MESSAGE; | ||
public boolean inCoolDownNudge = false; | ||
public boolean devMode = false; | ||
public boolean messageHook = false; | ||
public boolean nudgeCanBeDisabled = true; | ||
public boolean messageCanBeDisabled = false; | ||
public boolean autoUpdate = true; | ||
public String repositoryUrl = DataUpdater.DEFAULT_REPO_URL; | ||
|
||
public void readConfigByPluginAutoSave() { | ||
MiraiPluginConfig config = MiraiPluginConfig.INSTANCE; | ||
|
||
probability = (byte) config.getProbability(); | ||
respondSelfNudge = config.getRespondSelfNudge(); | ||
|
||
autoUpdate = config.getAutoUpdate(); | ||
repositoryUrl = config.getRepositoryUrl(); | ||
disabledGroups = config.getDisabledGroups(); | ||
coolDown = config.getCoolDown(); | ||
groupCoolDown = config.getGroupCoolDown(); | ||
inCoolDownMessage = config.getInCoolDownMessage().isBlank() ? | ||
null : config.getInCoolDownMessage(); | ||
if ("[nudge]".equals(inCoolDownMessage)) inCoolDownNudge = true; | ||
|
||
devMode = config.getDevMode(); | ||
messageHook = config.getMessageHook(); | ||
|
||
switch (config.getDisablePolicy()) { | ||
case NONE: | ||
nudgeCanBeDisabled = false; | ||
messageCanBeDisabled = false; | ||
break; | ||
case NUDGE: | ||
nudgeCanBeDisabled = true; | ||
messageCanBeDisabled = false; | ||
break; | ||
case MESSAGE: | ||
nudgeCanBeDisabled = false; | ||
messageCanBeDisabled = true; | ||
break; | ||
case FULL: | ||
nudgeCanBeDisabled = true; | ||
messageCanBeDisabled = true; | ||
break; | ||
} | ||
|
||
super.readPluginServiceConfig(config.toPluginServiceConfig()); | ||
} | ||
|
||
@Override | ||
public void readData(File dir){ | ||
if (dir.listFiles() == null) { | ||
System.out.println(autoUpdate ? | ||
"o((>ω< ))o 你这头懒猪, 没有下载petData!\n\\^o^/ 还好我冰雪聪明, 帮你自动更新了⭐" : | ||
"(゚Д゚*)ノ 没有petData! 你自己手动更新吧x\n(☆-v-) 笨蛋! 让你不开自动更新⭐"); | ||
return; | ||
} | ||
super.readData(dir); | ||
} | ||
|
||
/** | ||
* 发送随机图片 | ||
*/ | ||
public void sendImage(Group group, Member from, Member to) { //发送随机图片 | ||
sendImage(group, from, to, randomableList.get(MiraiPetpet.random.nextInt(randomableList.size()))); | ||
} | ||
|
||
/** | ||
* 有概率发送随机图片 | ||
*/ | ||
public void sendImage(Group group, Member from, Member to, boolean random) { | ||
if (!random) { | ||
sendImage(group, from, to); | ||
return; | ||
} | ||
int r = MiraiPetpet.random.nextInt(99) + 1; //不能为0 | ||
if (r >= probability) return; | ||
sendImage(group, from, to); | ||
} | ||
|
||
/** | ||
* 用key发送图片(无otherText) | ||
*/ | ||
@Deprecated | ||
public void sendImage(Group group, Member from, Member to, String key) { | ||
sendImage(group, from, to, key, null); | ||
} | ||
|
||
/** | ||
* 用key发送图片,指定otherText | ||
*/ | ||
@Deprecated | ||
public void sendImage(Group group, Member from, Member to, String key, String otherText) { | ||
TextExtraData textExtraData = new TextExtraData( | ||
from.getNameCard().isEmpty() ? from.getNick() : from.getNameCard(), | ||
to.getNameCard().isEmpty() ? to.getNick() : to.getNameCard(), | ||
group.getName(), | ||
otherText == null || otherText.equals("") ? new ArrayList<>() : | ||
new ArrayList<>(Arrays.asList(otherText.split("\\s+"))) | ||
); | ||
GifAvatarExtraDataProvider gifAvatarExtraDataProvider = BaseConfigFactory.getGifAvatarExtraDataFromUrls( | ||
from.getAvatarUrl(), to.getAvatarUrl(), group.getAvatarUrl(), group.getBotAsMember().getAvatarUrl(), | ||
group.getMembers().stream().map(NormalMember::getAvatarUrl).collect(Collectors.toList()) | ||
); | ||
sendImage(group, key, gifAvatarExtraDataProvider, textExtraData); | ||
} | ||
|
||
public void sendImage(Group group, String key, | ||
GifAvatarExtraDataProvider gifAvatarExtraDataProvider, TextExtraData textExtraData) { | ||
try { | ||
group.sendMessage( | ||
inputStreamToImage(generateImage( | ||
key, | ||
gifAvatarExtraDataProvider, | ||
textExtraData, | ||
null | ||
).getFirst(), group) | ||
); | ||
} catch (Exception ex) { | ||
System.out.println("发送图片时出错:" + ex.getMessage()); | ||
ex.printStackTrace(); | ||
} | ||
} | ||
|
||
public Image inputStreamToImage(@NotNull InputStream input, Contact contact) throws IOException { | ||
ExternalResource resource = ExternalResource.create(input); | ||
if (contact == null) contact = Bot.getInstances().get(0).getAsFriend(); | ||
Image image = contact.uploadImage(resource); | ||
resource.close(); | ||
return image; | ||
} | ||
} |
Oops, something went wrong.