Skip to content

Commit

Permalink
更新
Browse files Browse the repository at this point in the history
  • Loading branch information
mcchampions committed Jan 14, 2024
1 parent 6c95c0f commit e2b98ca
Show file tree
Hide file tree
Showing 18 changed files with 514 additions and 242 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ replay_pid*
.idea/
*.iml
target/
test/
src/test/
31 changes: 14 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,47 @@ JavaDoc:https://qscraft.top/javadoc
Dodo开放平台:https://open.imdodo.com/

### 代码范例

```java
package io.github.minecraftchampions.dodoopenjava.test;

import io.github.minecraftchampions.dodoopenjava.Bot;
import io.github.minecraftchampions.dodoopenjava.DodoOpenJava;
import io.github.minecraftchampions.dodoopenjava.command.CommandExecutor;
import io.github.minecraftchampions.dodoopenjava.command.CommandSender;
import io.github.minecraftchampions.dodoopenjava.api.CommandSender;
import io.github.minecraftchampions.dodoopenjava.event.EventHandler;
import io.github.minecraftchampions.dodoopenjava.event.Listener;
import io.github.minecraftchampions.dodoopenjava.event.events.v2.channelmessage.MessageEvent;

import java.io.IOException;

public class Main implements CommandExecutor,Listener{
public class Main implements CommandExecutor, Listener {
public static Bot bot;

public static void main(String... args) {
//创建机器人实例
bot = DodoOpenJava.createBot("111111","token");
bot = DodoOpenJava.createBot("111111", "token");
//启用日志记录功能
bot.enableApiResultsLogger();
//注册事件监听器
bot.registerListener(new Main());
//注册命令处理器
bot.registerCommand(new Main());
System.out.println(bot.getApi().V2.eventApi.getWebSocketConnection().getJSONObjectData().getString("endpoint"));
// bot.getCommandManager().getCommandTrigger().setCommandHeader("/");;
System.out.println(bot.getApi().V2.eventApi.getWebSocketConnection().getJSONObjectData().getJSONObject("data").getString("endpoint"));
bot.getApi().V2.botApi.getBotInfo().ifSuccess(result -> {
System.out.println("成功");
System.out.println(result.getJSONObjectData());
});

bot.getApi().V2.channelMessageApi.sendTextMessage("1252355","测试");
bot.getApi().V2.channelMessageApi.sendTextMessage("1252355", "测试");
System.out.println(bot.getApiResultsLogger());
}

@EventHandler
public void onEvent(MessageEvent e) {
System.out.println(e.getEventName());
System.out.println(e.getMessageId());
bot.getApi().V2.channelMessageApi.sendTextMessage(e.getChannelId(),"你发送了" + e.getMessageBody());
bot.getApi().V2.channelMessageApi.sendTextMessage(e.getChannelId(), "你发送了" + e.getMessageBody());
}

@Override
Expand All @@ -60,14 +62,9 @@ public class Main implements CommandExecutor,Listener{

@Override
public void onCommand(CommandSender commandSender, String[] strings) {
System.out.println(commandSender.getSenderName());
try {
commandSender.editSenderNickName("测试名字");
} catch (IOException e) {
throw new RuntimeException(e);
}
bot.getApi().V2.channelMessageApi.sendTextMessage(commandSender.getChannelId(),"测试成功");

System.out.println(commandSender.getName());
commandSender.editNickName("测试名字");
bot.getApi().V2.channelMessageApi.sendTextMessage(commandSender.getChannelId(), "测试成功");
}
}

Expand Down Expand Up @@ -96,7 +93,7 @@ public class Main implements CommandExecutor,Listener{
<dependency>
<groupId>top.qscraft</groupId>
<artifactId>dodoopenjava</artifactId>
<version>3.2.5-SNAPSHOT</version>
<version>3.2.5</version>
</dependency>
</dependencies>
```
Expand All @@ -109,7 +106,7 @@ public class Main implements CommandExecutor,Listener{
}
dependencies {
implementation 'top.qscraft:dodoopenjava:3.2.5-SNAPSHOT'
implementation 'top.qscraft:dodoopenjava:3.2.5'
}
```
### 教程(过于古老,无参考价值,改日重写)
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.2.5-SNAPSHOT</version>
<version>3.2.5</version>
<packaging>jar</packaging>
<name>DodoOpenJava</name>

Expand Down
19 changes: 18 additions & 1 deletion src/main/java/io/github/minecraftchampions/dodoopenjava/Bot.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package io.github.minecraftchampions.dodoopenjava;

import io.github.minecraftchampions.dodoopenjava.api.User;
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.AbstractEventTrigger;
import io.github.minecraftchampions.dodoopenjava.event.EventManager;
import io.github.minecraftchampions.dodoopenjava.event.Listener;
import io.github.minecraftchampions.dodoopenjava.event.WebSocketEventTrigger;
import io.github.minecraftchampions.dodoopenjava.impl.DodoUserImpl;
import io.github.minecraftchampions.dodoopenjava.message.Message;
import io.github.minecraftchampions.dodoopenjava.message.card.CardMessage;
import io.github.minecraftchampions.dodoopenjava.message.text.TextMessage;
Expand Down Expand Up @@ -539,6 +541,11 @@ public Result getMemberInfo(String islandSourceId, String dodoSourceId) {
return io.github.minecraftchampions.dodoopenjava.api.v2.MemberApi.getMemberInfo(bot.getAuthorization(), islandSourceId, dodoSourceId);
}

@SneakyThrows
public User getUser(String islandSourceId, String dodoSourceId) {
return new DodoUserImpl(dodoSourceId, islandSourceId, bot);
}

@SneakyThrows
public Result editMemberNickName(String islandSourceId, String dodoSourceId, String nickName) {
return io.github.minecraftchampions.dodoopenjava.api.v2.MemberApi.editMemberNickName(bot.getAuthorization(), islandSourceId, dodoSourceId, nickName);
Expand Down Expand Up @@ -569,6 +576,12 @@ public Result addMemberReasonBan(String islandSourceId, String dodoSourceId, Str
return io.github.minecraftchampions.dodoopenjava.api.v2.MemberApi.addMemberReasonBan(bot.getAuthorization(), islandSourceId, dodoSourceId, reason);
}

@SneakyThrows
public Result addMemberReasonChannelBan(String islandSourceId, String dodoSourceId, String reason, String noticeChannelId) {
return io.github.minecraftchampions.dodoopenjava.api.v2.MemberApi.addMemberReasonChannelBan(bot.getAuthorization(), islandSourceId, dodoSourceId, reason, noticeChannelId);
}


@SneakyThrows
public Result getMemberDodoIdMapList(List<String> strList) {
return io.github.minecraftchampions.dodoopenjava.api.v2.MemberApi.getMemberDodoIdMapList(bot.getAuthorization(), strList);
Expand Down Expand Up @@ -623,6 +636,10 @@ public Result sendDodoVideoMessage(String islandSourceId, String dodoSourceId, S
return io.github.minecraftchampions.dodoopenjava.api.v2.PersonalApi.sendDodoVideoMessage(bot.getAuthorization(), islandSourceId, dodoSourceId, u);
}

@SneakyThrows
public Result sendMessage(String islandSourceId, String dodoSourceId, Message message) {
return io.github.minecraftchampions.dodoopenjava.api.v2.PersonalApi.sendMessage(bot.getAuthorization(), islandSourceId, dodoSourceId, message);
}
}

public class RoleApi {
Expand Down
40 changes: 19 additions & 21 deletions src/main/java/io/github/minecraftchampions/dodoopenjava/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import lombok.Getter;
import lombok.NonNull;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.function.Consumer;
import java.util.function.Function;

/**
* api结果
Expand Down Expand Up @@ -91,18 +91,13 @@ public class Result {

private final long timestamp;

private final JSONObject jsonObjectData;

private Object data;
private final JSONObject JSONObjectData;

private Result(@NonNull JSONObject jsonObject) {
this.jsonObjectData = jsonObject;
this.JSONObjectData = jsonObject;
this.statusCode = jsonObject.getInt("status");
this.message = jsonObject.getString("message");
this.timestamp = System.currentTimeMillis();
if (statusCode == 0) {
this.data = jsonObject.get("data");
}
}

/**
Expand Down Expand Up @@ -142,33 +137,36 @@ public Result ifFailure(@NonNull Consumer<Result> consumer) {
}

/**
* 获取
* 如果成功
*
* @return jsonObject
* @param function function
* @return this
*/
public JSONObject getJSONObjectData() {
if (data instanceof JSONObject jsonObject) {
return jsonObject;
public <T> T ifSuccess(@NonNull Function<Result, T> function) {
if (statusCode == 0) {
return function.apply(this);
} else {
return new JSONObject();
return null;
}
}

/**
* 获取
* 如果失败
*
* @return jsonArray
* @param function function
* @return this
*/
public JSONArray getJSONArrayData() {
if (data instanceof JSONArray jsonArray) {
return jsonArray;
public <T> T ifFailure(@NonNull Function<Result, T> function) {
if (statusCode != 0) {
return function.apply(this);
} else {
return new JSONArray();
return null;
}
}


@Override
public String toString() {
return jsonObjectData.toString();
return getJSONObjectData().toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.github.minecraftchampions.dodoopenjava.api;

import io.github.minecraftchampions.dodoopenjava.message.text.TextMessage;

/**
* 命令发送者接口
*/
public interface CommandSender extends User {
void editMessage(TextMessage message);

String getChannelId();

String getMessageId();

boolean isPersonalChat();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.github.minecraftchampions.dodoopenjava.api;

import io.github.minecraftchampions.dodoopenjava.message.Message;
import lombok.NonNull;

/**
* 用户接口
*/
public interface User {
String getIslandSourceId();

String getDodoSourceId();

String getNickName();

String getName();

String getAvatarUrl();

String getJoinTime();

int getSex();

int getLevel();

int getIsBot();

int getOnlineDevice();

int getOnlineStatus();

String getInviterDodoSourceId();

void editNickName(@NonNull String nickName);

void mute(int mills);

void mute(int mills, @NonNull String reason);

int getInvitationCount();

/**
* 封禁成员,传null视作不传参
*/
void ban(String reason, String noticeChannelId);

void unmute();

void unban();

void addRole(@NonNull String roleId);

void removeRole(@NonNull String roleId);

long getIntegralBalance();

void editIntegral(int type, long integral);

void addIntegral(long integral);

void removeIntegral(long integral);

String sendPersonalMessage(@NonNull Message message);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
* Dodo开放平台各个版本的接口
* API
*/
package io.github.minecraftchampions.dodoopenjava.api;
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,44 @@ public static Result addMemberReasonBan(String authorization, String islandSourc

}

/**
* 永久封禁成员
*
* @param clientId 机器人唯一标识
* @param token 机器人鉴权Token
* @param islandSourceId 群号
* @param noticeChannelId 提醒频道ID
* @param dodoSourceId Dodo号
* @param reason 封禁理由
* @return JSON对象
* @throws IOException 失败后抛出
*/
public static Result addMemberReasonChannelBan(String clientId, String token, String islandSourceId, String dodoSourceId, String reason, String noticeChannelId) throws IOException {
return addMemberReasonChannelBan(BaseUtil.Authorization(clientId, token), islandSourceId, dodoSourceId, reason, noticeChannelId);
}

/**
* 永久封禁成员
*
* @param authorization authorization
* @param islandSourceId 群号
* @param dodoSourceId Dodo号
* @param reason 封禁理由
* @param noticeChannelId 提醒频道ID
* @return JSON对象
* @throws IOException 失败后抛出
*/
public static Result addMemberReasonChannelBan(String authorization, String islandSourceId, String dodoSourceId, String reason, String noticeChannelId) throws IOException {
String url = DodoOpenJava.BASEURL + "member/ban/add";
JSONObject jsonObject = new JSONObject();
jsonObject.put("islandSourceId", islandSourceId)
.put("dodoSourceId", dodoSourceId)
.put("reason", reason)
.put("noticeChannelId", noticeChannelId);
return NetUtil.sendRequest(jsonObject.toString(), url, authorization);

}

/**
* 永久封禁成员
*
Expand Down Expand Up @@ -447,10 +485,9 @@ public static Result removeMemberBan(String authorization, String islandSourceId
/**
* 获取成员DoDo号映射列表
*
* @param clientId 机器人唯一标识
* @param token 机器人鉴权Token
* @param dodoIdList dodoId
* @param clientId 机器人唯一标识
* @param token 机器人鉴权Token
* @param dodoIdList dodoId
* @return JSON对象
* @throws IOException 失败后抛出
*/
Expand All @@ -461,8 +498,8 @@ public static Result getMemberDodoIdMapList(String clientId, String token, List<
/**
* 获取成员DoDo号映射列表
*
* @param authorization authorization
* @param dodoIdList dodoId
* @param authorization authorization
* @param dodoIdList dodoId
* @return JSON对象
* @throws IOException 失败后抛出
*/
Expand Down
Loading

0 comments on commit e2b98ca

Please sign in to comment.