Skip to content

Commit

Permalink
Add Message related contents to VoiceChannel (#71)
Browse files Browse the repository at this point in the history
* Add Message related contents to VoiceChannel

* Bring getTextChannelMessage back
  • Loading branch information
xiaoACE6716 authored Apr 15, 2024
1 parent 268cd85 commit ed39bd8
Show file tree
Hide file tree
Showing 17 changed files with 294 additions and 188 deletions.
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,15 @@
</repositories>

<dependencies>
<!-- <dependency>-->
<!-- <groupId>com.github.SNWCreations</groupId>-->
<!-- <artifactId>JKook</artifactId>-->
<!-- <version>${jkook.version}</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.github.SNWCreations</groupId>
<groupId>com.github.xiaoACE6716</groupId>
<artifactId>JKook</artifactId>
<version>${jkook.version}</version>
<version>95ce364469</version>
</dependency>

<!--
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/snw/kookbc/impl/HttpAPIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@
import snw.jkook.entity.User;
import snw.jkook.entity.channel.Category;
import snw.jkook.entity.channel.Channel;
import snw.jkook.entity.channel.NonCategoryChannel;
import snw.jkook.entity.channel.TextChannel;
import snw.jkook.message.ChannelMessage;
import snw.jkook.message.PrivateMessage;
import snw.jkook.message.TextChannelMessage;
import snw.jkook.message.component.BaseComponent;
import snw.jkook.util.PageIterator;
import snw.jkook.util.Validate;
import snw.kookbc.impl.message.ChannelMessageImpl;
import snw.kookbc.impl.message.PrivateMessageImpl;
import snw.kookbc.impl.message.TextChannelMessageImpl;
import snw.kookbc.impl.network.HttpAPIRoute;
Expand Down Expand Up @@ -251,6 +254,34 @@ public TextChannelMessage getTextChannelMessage(String id) throws NoSuchElementE
return new TextChannelMessageImpl(client, id, sender, component, timeStamp, quote, channel);
}

@Override
public ChannelMessage getChannelMessage(String id) throws NoSuchElementException {
final JsonObject object;
try {
object = client.getNetworkClient()
.get(HttpAPIRoute.CHANNEL_MESSAGE_INFO.toFullURL() + "?msg_id=" + id);
} catch (BadResponseException e) {
if (e.getCode() == 40000) {
throw (NoSuchElementException) // force casting is required because Throwable#initCause return Throwable
new NoSuchElementException("No message object with provided ID " + id + " found")
.initCause(e);
}
throw e;
}
JsonObject rawSender = get(object, "author").getAsJsonObject();
User sender = client.getStorage().getUser(get(rawSender, "id").getAsString(), rawSender);
final BaseComponent component = client.getMessageBuilder().buildComponent(object);
long timeStamp = get(object, "create_at").getAsLong();
ChannelMessage quote = null;
if (has(object, "quote")) {
final JsonObject rawQuote = get(object, "quote").getAsJsonObject();
final String quoteId = get(rawQuote, "id").getAsString();
quote = getChannelMessage(quoteId);
}
final NonCategoryChannel channel = (NonCategoryChannel) getChannel(get(object, "channel_id").getAsString());
return new ChannelMessageImpl(client, id, sender, component, timeStamp, quote, channel);
}

@Override
public PrivateMessage getPrivateMessage(User user, String id) throws NoSuchElementException {
final String chatCode = get(client.getNetworkClient()
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/snw/kookbc/impl/UnsafeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
import snw.jkook.Unsafe;
import snw.jkook.entity.CustomEmoji;
import snw.jkook.entity.Game;
import snw.jkook.message.ChannelMessage;
import snw.jkook.message.PrivateMessage;
import snw.jkook.message.TextChannelMessage;
import snw.kookbc.impl.entity.CustomEmojiImpl;
import snw.kookbc.impl.entity.GameImpl;
import snw.kookbc.impl.message.ChannelMessageImpl;
import snw.kookbc.impl.message.PrivateMessageImpl;
import snw.kookbc.impl.message.TextChannelMessageImpl;

Expand All @@ -38,11 +40,17 @@ public UnsafeImpl(KBCClient client) {
this.client = client;
}


@Override
public TextChannelMessage getTextChannelMessage(String id) {
return new TextChannelMessageImpl(client, id, null, null, -1, null, null);
}

@Override
public ChannelMessage getChannelMessage(String id) {
return new ChannelMessageImpl(client, id, null, null, -1, null, null);
}

@Override
public PrivateMessage getPrivateMessage(String id) {
return new PrivateMessageImpl(client, id, null, null, -1, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public Channel buildChannel(JsonObject object) {
topic
);
} else if (type == 2) { // VoiceChannel
int chatLimitTime = get(object, "slow_mode").getAsInt();
boolean hasPassword = object.has("has_password") && get(object, "has_password").getAsBoolean();
int size = get(object, "limit_amount").getAsInt();
int quality = get(object, "voice_quality").getAsInt();
Expand All @@ -159,7 +160,8 @@ public Channel buildChannel(JsonObject object) {
level,
hasPassword,
size,
quality
quality,
chatLimitTime
);
}
}
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/snw/kookbc/impl/entity/builder/MessageBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import snw.jkook.entity.User;
import snw.jkook.entity.channel.Channel;
import snw.jkook.entity.channel.NonCategoryChannel;
import snw.jkook.entity.channel.TextChannel;
import snw.jkook.message.ChannelMessage;
import snw.jkook.message.Message;
import snw.jkook.message.PrivateMessage;
import snw.jkook.message.TextChannelMessage;
Expand All @@ -35,6 +38,7 @@
import snw.jkook.message.component.card.Theme;
import snw.jkook.message.component.card.module.FileModule;
import snw.kookbc.impl.KBCClient;
import snw.kookbc.impl.message.ChannelMessageImpl;
import snw.kookbc.impl.message.PrivateMessageImpl;
import snw.kookbc.impl.message.QuoteImpl;
import snw.kookbc.impl.message.TextChannelMessageImpl;
Expand Down Expand Up @@ -115,12 +119,12 @@ public PrivateMessage buildPrivateMessage(JsonObject object) {
return new PrivateMessageImpl(client, id, author, buildComponent(object), timeStamp, quoteObject);
}

public TextChannelMessage buildTextChannelMessage(JsonObject object) {
public ChannelMessage buildChannelMessage(JsonObject object) {
String id = get(object, "msg_id").getAsString();
final JsonObject extra = get(object, "extra").getAsJsonObject();
JsonObject authorObj = get(extra, "author").getAsJsonObject();
User author = client.getStorage().getUser(get(authorObj, "id").getAsString(), authorObj);
TextChannel channel = (TextChannel) client.getStorage().getChannel(get(object, "target_id").getAsString());
Channel channel = client.getStorage().getChannel(get(object, "target_id").getAsString());
long timeStamp = get(object, "msg_timestamp").getAsLong();
final JsonObject quote;
JsonObject quote1;
Expand All @@ -131,22 +135,22 @@ public TextChannelMessage buildTextChannelMessage(JsonObject object) {
}
quote = quote1;
if (quote == null) {
return new TextChannelMessageImpl(client, id, author, buildComponent(object), timeStamp, null, channel);
return new ChannelMessageImpl(client, id, author, buildComponent(object), timeStamp, null, (NonCategoryChannel) channel);
}
final String quoteId = get(quote, "rong_id").getAsString();
Message quoteObject;
Message quoteFromCache = client.getStorage().getMessage(quoteId);
if (quoteFromCache != null) {
quoteObject = quoteFromCache; // prevent resource leak
Message quoteFormCache = client.getStorage().getMessage(quoteId);
if (quoteFormCache != null) {
quoteObject = quoteFormCache;
} else {
try {
quoteObject = client.getCore().getHttpAPI().getTextChannelMessage(quoteId);
quoteObject = client.getCore().getHttpAPI().getChannelMessage(quoteId);
} catch (NoSuchElementException e) {
quoteObject = buildQuote(quote);
}
}

return new TextChannelMessageImpl(client, id, author, buildComponent(object), timeStamp, quoteObject, channel);
return new ChannelMessageImpl(client, id, author, buildComponent(object), timeStamp, quoteObject, (NonCategoryChannel) channel);
}

public Message buildQuote(JsonObject object) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@
import snw.jkook.entity.User;
import snw.jkook.entity.channel.Category;
import snw.jkook.entity.channel.NonCategoryChannel;
import snw.jkook.message.ChannelMessage;
import snw.jkook.message.Message;
import snw.jkook.message.component.BaseComponent;
import snw.jkook.message.component.MarkdownComponent;
import snw.jkook.util.PageIterator;
import snw.kookbc.impl.KBCClient;
import snw.kookbc.impl.entity.builder.MessageBuilder;
import snw.kookbc.impl.network.HttpAPIRoute;
import snw.kookbc.impl.network.exceptions.BadResponseException;
import snw.kookbc.impl.pageiter.ChannelInvitationIterator;
import snw.kookbc.util.MapBuilder;

Expand All @@ -39,9 +45,11 @@

public abstract class NonCategoryChannelImpl extends ChannelImpl implements NonCategoryChannel {
private Category parent;
private int chatLimitTime;

protected NonCategoryChannelImpl(KBCClient client, String id, User master, Guild guild, boolean permSync, Category parent, String name, Collection<RolePermissionOverwrite> rpo, Collection<UserPermissionOverwrite> upo, int level) {
protected NonCategoryChannelImpl(KBCClient client, String id, User master, Guild guild, boolean permSync, Category parent, String name, Collection<RolePermissionOverwrite> rpo, Collection<UserPermissionOverwrite> upo, int level, int chatLimitTime) {
super(client, id, master, guild, permSync, name, rpo, upo, level);
this.chatLimitTime = chatLimitTime;
this.parent = parent;
}

Expand Down Expand Up @@ -81,6 +89,64 @@ public PageIterator<Set<Invitation>> getInvitations() {
return new ChannelInvitationIterator(client, this);
}


@Override
public String sendComponent(String message) {
return sendComponent(new MarkdownComponent(message));
}

@Override
public String sendComponent(String message, @Nullable ChannelMessage quote, @Nullable User tempTarget) {
return sendComponent(new MarkdownComponent(message), quote, tempTarget);
}

@Override
public String sendComponent(BaseComponent baseComponent) {
return sendComponent(baseComponent, null, null);
}

@Override
public String sendComponent(BaseComponent component, @Nullable ChannelMessage quote, @Nullable User tempTarget) {
Object[] result = MessageBuilder.serialize(component);
Map<String, Object> body = new MapBuilder()
.put("target_id", getId())
.put("type", result[0])
.put("content", result[1])
.putIfNotNull("quote", quote, Message::getId)
.putIfNotNull("temp_target_id", tempTarget, User::getId)
.build();
try {
return client.getNetworkClient().post(HttpAPIRoute.CHANNEL_MESSAGE_SEND.toFullURL(), body).get("msg_id").getAsString();
} catch (BadResponseException e) {
if ("资源不存在".equals(e.getRawMessage())) {
// 2023/1/17: special case for the resources that aren't created by Bots.
// Thanks: Edint386@Github
throw new IllegalArgumentException("Unable to send component. Is the resource created by Bot?", e);
} else {
throw e;
}
}
}

@Override
public int getChatLimitTime() {
return chatLimitTime;
}

@Override
public void setChatLimitTime(int chatLimitTime) {
Map<String, Object> body = new MapBuilder()
.put("channel_id", getId())
.put("slow_mode", chatLimitTime)
.build();
client.getNetworkClient().post(HttpAPIRoute.CHANNEL_UPDATE.toFullURL(), body);
setChatLimitTime0(chatLimitTime);
}

public void setChatLimitTime0(int chatLimitTime) {
this.chatLimitTime = chatLimitTime;
}

@Override
public synchronized void update(JsonObject data) {
super.update(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,11 @@
import snw.jkook.entity.User;
import snw.jkook.entity.channel.Category;
import snw.jkook.entity.channel.TextChannel;
import snw.jkook.message.Message;
import snw.jkook.message.TextChannelMessage;
import snw.jkook.message.component.BaseComponent;
import snw.jkook.message.component.MarkdownComponent;
import snw.jkook.util.PageIterator;
import snw.jkook.util.Validate;
import snw.kookbc.impl.KBCClient;
import snw.kookbc.impl.entity.builder.MessageBuilder;
import snw.kookbc.impl.network.HttpAPIRoute;
import snw.kookbc.impl.network.exceptions.BadResponseException;
import snw.kookbc.impl.pageiter.TextChannelMessageIterator;
import snw.kookbc.util.MapBuilder;

Expand All @@ -48,7 +43,7 @@ public class TextChannelImpl extends NonCategoryChannelImpl implements TextChann
private String topic;

public TextChannelImpl(KBCClient client, String id, User master, Guild guild, boolean permSync, Category parent, String name, Collection<RolePermissionOverwrite> rpo, Collection<UserPermissionOverwrite> upo, int level, int chatLimitTime, String topic) {
super(client, id, master, guild, permSync, parent, name, rpo, upo, level);
super(client, id, master, guild, permSync, parent, name, rpo, upo, level, chatLimitTime);
this.chatLimitTime = chatLimitTime;
this.topic = topic;
}
Expand Down Expand Up @@ -78,63 +73,6 @@ public PageIterator<Collection<TextChannelMessage>> getMessages(@Nullable String
return new TextChannelMessageIterator(client, this, refer, isPin, queryMode);
}

@Override
public String sendComponent(String message) {
return sendComponent(new MarkdownComponent(message));
}

@Override
public String sendComponent(String message, @Nullable TextChannelMessage quote, @Nullable User tempTarget) {
return sendComponent(new MarkdownComponent(message), quote, tempTarget);
}

@Override
public String sendComponent(BaseComponent baseComponent) {
return sendComponent(baseComponent, null, null);
}

@Override
public String sendComponent(BaseComponent component, @Nullable TextChannelMessage quote, @Nullable User tempTarget) {
Object[] result = MessageBuilder.serialize(component);
Map<String, Object> body = new MapBuilder()
.put("target_id", getId())
.put("type", result[0])
.put("content", result[1])
.putIfNotNull("quote", quote, Message::getId)
.putIfNotNull("temp_target_id", tempTarget, User::getId)
.build();
try {
return client.getNetworkClient().post(HttpAPIRoute.CHANNEL_MESSAGE_SEND.toFullURL(), body).get("msg_id").getAsString();
} catch (BadResponseException e) {
if ("资源不存在".equals(e.getRawMessage())) {
// 2023/1/17: special case for the resources that aren't created by Bots.
// Thanks: Edint386@Github
throw new IllegalArgumentException("Unable to send component. Is the resource created by Bot?", e);
} else {
throw e;
}
}
}

@Override
public int getChatLimitTime() {
return chatLimitTime;
}

@Override
public void setChatLimitTime(int chatLimitTime) {
Map<String, Object> body = new MapBuilder()
.put("channel_id", getId())
.put("slow_mode", chatLimitTime)
.build();
client.getNetworkClient().post(HttpAPIRoute.CHANNEL_UPDATE.toFullURL(), body);
setChatLimitTime0(chatLimitTime);
}

public void setChatLimitTime0(int chatLimitTime) {
this.chatLimitTime = chatLimitTime;
}

@Override
public void update(JsonObject data) {
synchronized (this) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ public class VoiceChannelImpl extends NonCategoryChannelImpl implements VoiceCha
private boolean passwordProtected;
private int maxSize;
private int quality;
private int chatLimitTime;

public VoiceChannelImpl(KBCClient client, String id, User master, Guild guild, boolean permSync, Category parent, String name, Collection<RolePermissionOverwrite> rpo, Collection<UserPermissionOverwrite> upo, int level, boolean passwordProtected, int maxSize, int quality) {
super(client, id, master, guild, permSync, parent, name, rpo, upo, level);
public VoiceChannelImpl(KBCClient client, String id, User master, Guild guild, boolean permSync, Category parent, String name, Collection<RolePermissionOverwrite> rpo, Collection<UserPermissionOverwrite> upo, int level, boolean passwordProtected, int maxSize, int quality, int chatLimitTime) {
super(client, id, master, guild, permSync, parent, name, rpo, upo, level, chatLimitTime);
this.passwordProtected = passwordProtected;
this.maxSize = maxSize;
this.quality = quality;
this.chatLimitTime = chatLimitTime;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void event(UserClickButtonEvent event) {
Message message = this.client.getCore().getUnsafe().getPrivateMessage(event.getMessageId());
message.setComponent(finalComponent);
} else if (messageType.equals("CM")) {
Message message = this.client.getCore().getUnsafe().getTextChannelMessage(event.getMessageId());
Message message = this.client.getCore().getUnsafe().getChannelMessage(event.getMessageId());
message.setComponent(finalComponent);
}

Expand Down
Loading

0 comments on commit ed39bd8

Please sign in to comment.