Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
mcchampions committed Jan 2, 2024
1 parent f00e703 commit 75b7710
Show file tree
Hide file tree
Showing 58 changed files with 912 additions and 406 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public class Main implements CommandExecutor,Listener{
<dependency>
<groupId>top.qscraft</groupId>
<artifactId>dodoopenjava</artifactId>
<version>3.1.13-SNAPSHOT-1</version>
<version>3.2-SNAPSHOT</version>
</dependency>
</dependencies>
```
Expand All @@ -109,7 +109,7 @@ public class Main implements CommandExecutor,Listener{
}
dependencies {
implementation 'top.qscraft:dodoopenjava:3.1.13-SNAPSHOT-1'
implementation 'top.qscraft:dodoopenjava:3.2-SNAPSHOT'
}
```
### 教程(过于古老,无参考价值,改日重写)
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<groupId>io.github.minecraftchampions.dodoopenjava</groupId>
<artifactId>DodoOpenJava</artifactId>
<version>3.1.13-SNAPSHOT</version>
<version>3.2-SNAPSHOT</version>
<packaging>jar</packaging>
<name>DodoOpenJava</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentSkipListMap;

/**
Expand All @@ -21,23 +22,42 @@ public class ApiResultsLogger {

private final ConcurrentSkipListMap<Long, LinkedHashSet<Result>> resultsLogMap = new ConcurrentSkipListMap<>();

/**
* 添加API执行结果记录
*
* @param result result
*/
public void addResult(@NonNull Result result) {
long timestamp = result.getTimestamp();
Set<Result> results = resultsLogMap.get(timestamp);
if (results == null) {
resultsLogMap.put(timestamp, new LinkedHashSet<>(Set.of(result)));
return;
}
synchronized (results) {
results.add(result);
}
CompletableFuture.runAsync(() -> {
long timestamp = result.getTimestamp();
Set<Result> results = resultsLogMap.get(timestamp);
if (results == null) {
resultsLogMap.put(timestamp, new LinkedHashSet<>(Set.of(result)));
return;
}
synchronized (results) {
results.add(result);
}
});
}

public ConcurrentSkipListMap<Long, LinkedHashSet<Result>> getAllLogs() {
/**
* 获取日志
*
* @return 日志
*/
public CompletableFuture<ConcurrentSkipListMap<Long, LinkedHashSet<Result>>> getAllLogs() {
return getLogs(null, null);
}

public ConcurrentSkipListMap<Long, LinkedHashSet<Result>> getLogs(Date startDate, Date endDate) {
/**
* 获取指定时间段到另一个指定时间段的日志
*
* @param startDate 开始
* @param endDate 结束
* @return 日志
*/
public CompletableFuture<ConcurrentSkipListMap<Long, LinkedHashSet<Result>>> getLogs(Date startDate, Date endDate) {
long startTimestamp;
long endTimestamp;
if (startDate == null) {
Expand All @@ -50,19 +70,27 @@ public ConcurrentSkipListMap<Long, LinkedHashSet<Result>> getLogs(Date startDate
} else {
endTimestamp = endDate.getTime();
}
return getLogs(startTimestamp,endTimestamp);
return getLogs(startTimestamp, endTimestamp);
}

public ConcurrentSkipListMap<Long, LinkedHashSet<Result>> getLogs(long startTimestamp, long endTimestamp) {
Map<Long, LinkedHashSet<Result>> subMap = resultsLogMap.subMap(startTimestamp, true, endTimestamp, true);
ConcurrentSkipListMap<Long, LinkedHashSet<Result>> deepCopiedMap = new ConcurrentSkipListMap<>();
for (Map.Entry<Long, LinkedHashSet<Result>> entry : subMap.entrySet()) {
deepCopiedMap.put(entry.getKey(), (LinkedHashSet<Result>)entry.getValue().clone());
}
return deepCopiedMap;
/**
* 获取指定时间段到另一个指定时间段的日志
*
* @param startTimestamp 开始
* @param endTimestamp 结束
* @return 日志
*/
public CompletableFuture<ConcurrentSkipListMap<Long, LinkedHashSet<Result>>> getLogs(long startTimestamp, long endTimestamp) {
return CompletableFuture.supplyAsync(() -> {
Map<Long, LinkedHashSet<Result>> subMap = resultsLogMap.subMap(startTimestamp, true, endTimestamp, true);
ConcurrentSkipListMap<Long, LinkedHashSet<Result>> deepCopiedMap = new ConcurrentSkipListMap<>();
for (Map.Entry<Long, LinkedHashSet<Result>> entry : subMap.entrySet()) {
deepCopiedMap.put(entry.getKey(), (LinkedHashSet<Result>) entry.getValue().clone());
}
return deepCopiedMap;
});
}


@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Expand Down
52 changes: 40 additions & 12 deletions src/main/java/io/github/minecraftchampions/dodoopenjava/Bot.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package io.github.minecraftchampions.dodoopenjava;

import io.github.minecraftchampions.dodoopenjava.card.Card;
import io.github.minecraftchampions.dodoopenjava.command.CommandExecutor;
import io.github.minecraftchampions.dodoopenjava.command.CommandManager;
import io.github.minecraftchampions.dodoopenjava.event.EventManager;
import io.github.minecraftchampions.dodoopenjava.event.EventTrigger;
import io.github.minecraftchampions.dodoopenjava.event.Listener;
import io.github.minecraftchampions.dodoopenjava.event.WebSocketEventTrigger;
import io.github.minecraftchampions.dodoopenjava.message.Message;
import io.github.minecraftchampions.dodoopenjava.message.card.CardMessage;
import io.github.minecraftchampions.dodoopenjava.message.text.TextMessage;
import io.github.minecraftchampions.dodoopenjava.utils.BaseUtil;
import lombok.Getter;
import lombok.NonNull;
Expand Down Expand Up @@ -52,10 +53,20 @@ public String getAuthorization() {
return BaseUtil.Authorization(clientId, token);
}

/**
* 启用日志记录器
*/
public void enableApiResultsLogger() {
DodoOpenJava.enableApiResultsLogger(this);
}

/**
* 卸载日志记录器
*/
public void disableApiResultsLogger() {
DodoOpenJava.disableApiResultsLogger(this);
}

/**
* 注册事件监听器
*
Expand All @@ -71,6 +82,9 @@ public synchronized void registerListener(@NonNull Listener listener) {
getEventManager().registerListener(listener);
}

/**
* 移除事件监听器
*/
public synchronized void removeEventTrigger() {
if (eventTrigger != null) {
eventTrigger.close();
Expand Down Expand Up @@ -223,7 +237,6 @@ public Result removeBotInvite(String dodoSourceId) {
public Result setBotIslandLeave(String islandSourceId) {
return io.github.minecraftchampions.dodoopenjava.api.v2.BotApi.setBotIslandLeave(bot.getAuthorization(), islandSourceId);
}

}

public class ChannelApi {
Expand Down Expand Up @@ -251,7 +264,6 @@ public Result getChannelInfo(String channelId) {
public Result deleteChannel(String islandSourceId, String channelId) {
return io.github.minecraftchampions.dodoopenjava.api.v2.ChannelApi.deleteChannel(bot.getAuthorization(), islandSourceId, channelId);
}

}

public class ChannelArticleApi {
Expand All @@ -264,7 +276,6 @@ public Result removeChannelArticle(int type, String id, String channelId) {
public Result addChannelArticle(String channelId, String title, String content, String imageUrl) {
return io.github.minecraftchampions.dodoopenjava.api.v2.ChannelArticleApi.addChannelArticle(bot.getAuthorization(), channelId, title, content, imageUrl);
}

}

public class ChannelMessageApi {
Expand Down Expand Up @@ -329,7 +340,7 @@ public Result addChannelMessageReaction(String messageId, String id) {
}

@SneakyThrows
public Result editChannelCardMessage(String messageId, Card messageBody) {
public Result editChannelCardMessage(String messageId, CardMessage messageBody) {
return io.github.minecraftchampions.dodoopenjava.api.v2.ChannelMessageApi.editChannelCardMessage(bot.getAuthorization(), messageId, messageBody);
}

Expand All @@ -354,8 +365,8 @@ public Result sendTextMessage(String channelId, String content) {
}

@SneakyThrows
public Result sendTextMessage(String channelId, Message message) {
return io.github.minecraftchampions.dodoopenjava.api.v2.ChannelMessageApi.sendTextMessage(bot.getAuthorization(), channelId, message);
public Result sendTextMessage(String channelId, TextMessage textMessage) {
return io.github.minecraftchampions.dodoopenjava.api.v2.ChannelMessageApi.sendTextMessage(bot.getAuthorization(), channelId, textMessage);
}

@SneakyThrows
Expand All @@ -369,7 +380,7 @@ public Result sendAtTextMessage(String channelId, String dodoId, String message)
}

@SneakyThrows
public Result sendCardMessage(String channelId, Card messageBody) {
public Result sendCardMessage(String channelId, CardMessage messageBody) {
return io.github.minecraftchampions.dodoopenjava.api.v2.ChannelMessageApi.sendCardMessage(bot.getAuthorization(), channelId, messageBody);
}

Expand All @@ -378,17 +389,37 @@ public Result getChannelMessageReactionMemberList(String messageId, int type, St
return io.github.minecraftchampions.dodoopenjava.api.v2.ChannelMessageApi.getChannelMessageReactionMemberList(bot.getAuthorization(), messageId, type, id, pageSize, maxId);
}

@SneakyThrows
public Result sendMessage(String channelId, Message messageBody) {
return io.github.minecraftchampions.dodoopenjava.api.v2.ChannelMessageApi.sendMessage(bot.getAuthorization(), channelId, messageBody);
}
}

public class ChannelVoiceApi {
@SneakyThrows
public Result getChannelVoiceMemberStatus(String islandSourceId, String dodoSourceId) {
return io.github.minecraftchampions.dodoopenjava.api.v2.ChannelVoiceApi.getChannelVoiceMemberStatus(bot.getAuthorization(), islandSourceId, dodoSourceId);

}

@SneakyThrows
public Result moveChannelVoiceMember(String islandSourceId, String dodoSourceId, String channelId) {
return io.github.minecraftchampions.dodoopenjava.api.v2.ChannelVoiceApi.moveChannelVoiceMember(bot.getAuthorization(), islandSourceId, dodoSourceId, channelId);

}

@SneakyThrows
public Result editChannelVoiceMember(int operateType, String dodoSourceId, String channelId) {
return io.github.minecraftchampions.dodoopenjava.api.v2.ChannelVoiceApi.editChannelVoiceMember(bot.getAuthorization(), operateType, dodoSourceId, channelId);

}
}

public class EventApi {
@SneakyThrows
public Result getWebSocketConnection() {
return io.github.minecraftchampions.dodoopenjava.api.v2.EventApi.getWebSocketConnection(bot.getAuthorization());
}

}

public class GiftApi {
Expand Down Expand Up @@ -416,7 +447,6 @@ public Result getGiftMemberList(String targetId, int targetType, String giftId,
public Result getGiftAccount(String islandSourceId) {
return io.github.minecraftchampions.dodoopenjava.api.v2.GiftApi.getGiftAccount(bot.getAuthorization(), islandSourceId);
}

}

public class IntegralApi {
Expand All @@ -429,7 +459,6 @@ public Result getIntegralInfo(String islandSourceId, String dodoSourceId) {
public Result setIntegralEdit(String islandSourceId, String dodoSourceId, int operateType, long integral) {
return io.github.minecraftchampions.dodoopenjava.api.v2.IntegralApi.setIntegralEdit(bot.getAuthorization(), islandSourceId, dodoSourceId, operateType, integral);
}

}

public class IslandApi {
Expand Down Expand Up @@ -457,7 +486,6 @@ public Result getIslandMuteList(String islandSourceId) {
public Result getIslandBanList(String islandSourceId) {
return io.github.minecraftchampions.dodoopenjava.api.v2.IslandApi.getIslandBanList(bot.getAuthorization(), islandSourceId);
}

}

public class MemberApi {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
* API
Expand All @@ -18,7 +18,7 @@ public class DodoOpenJava {
private static final HashSet<Bot> bots = new HashSet<>();

@Getter
private static final HashMap<String, ApiResultsLogger> logMap = new HashMap<>();
private static final ConcurrentHashMap<String, ApiResultsLogger> logMap = new ConcurrentHashMap<>();

public static final Logger LOGGER = LoggerFactory.getLogger(DodoOpenJava.class);

Expand All @@ -39,23 +39,60 @@ public static Set<Bot> getBots() {
return (HashSet<Bot>) bots.clone();
}

/**
* 卸载Bot
*
* @param bot bot
*/
public synchronized static void disableBot(Bot bot) {
bots.remove(bot);
bot.getCommandManager().unregisterAllCommands();
bot.getEventManager().unregisterAllListeners();
bot.removeEventTrigger();
logMap.remove(bot.getAuthorization());
bot.disableApiResultsLogger();
}

/**
* 启用日志服务
*
* @param bot bot
*/
public static void enableApiResultsLogger(Bot bot) {
enableApiResultsLogger(bot.getAuthorization());
}

public static synchronized void enableApiResultsLogger(String authorization) {
if (logMap.containsKey(authorization)) {
LOGGER.warn("已经调用过DodoOpenJava#enableApiResultsLogger");
return;
/**
* 启用日志服务
*
* @param authorization authorization
*/
public static void enableApiResultsLogger(String authorization) {
synchronized (logMap) {
if (logMap.containsKey(authorization)) {
LOGGER.warn("已经调用过DodoOpenJava#enableApiResultsLogger");
return;
}
logMap.put(authorization, new ApiResultsLogger(authorization));
}
}

/**
* 卸载日志服务
*
* @param bot bot
*/
public static void disableApiResultsLogger(Bot bot) {
enableApiResultsLogger(bot.getAuthorization());
}

/**
* 卸载日志服务
*
* @param authorization authorization
*/
public static void disableApiResultsLogger(String authorization) {
synchronized (logMap) {
logMap.remove(authorization);
}
logMap.put(authorization, new ApiResultsLogger(authorization));
}
}
Loading

0 comments on commit 75b7710

Please sign in to comment.