Convert Discord ids to longs

This commit is contained in:
Vankka 2021-09-07 21:57:55 +03:00
parent 627b538456
commit 4f6ee80a53
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
17 changed files with 74 additions and 78 deletions

View File

@ -46,7 +46,7 @@ public interface DiscordAPI {
* @return the message channel
*/
@NotNull
Optional<? extends DiscordMessageChannel> getMessageChannelById(@NotNull String id);
Optional<? extends DiscordMessageChannel> getMessageChannelById(long id);
/**
* Gets a Discord direct message channel by id, the provided entity can be cached and will not update if it changes on Discord.
@ -54,7 +54,7 @@ public interface DiscordAPI {
* @return the direct message channel
*/
@NotNull
Optional<DiscordDMChannel> getDirectMessageChannelById(@NotNull String id);
Optional<DiscordDMChannel> getDirectMessageChannelById(long id);
/**
* Gets a Discord text channel by id, the provided entity can be cached and will not update if it changes on Discord.
@ -62,7 +62,7 @@ public interface DiscordAPI {
* @return the text channel
*/
@NotNull
Optional<DiscordTextChannel> getTextChannelById(@NotNull String id);
Optional<DiscordTextChannel> getTextChannelById(long id);
/**
* Gets a Discord server by id, the provided entity can be cached and will not update if it changes on Discord.
@ -70,7 +70,7 @@ public interface DiscordAPI {
* @return the Discord server
*/
@NotNull
Optional<DiscordGuild> getGuildById(@NotNull String id);
Optional<DiscordGuild> getGuildById(long id);
/**
* Gets a Discord user by id, the provided entity can be cached and will not update if it changes on Discord.
@ -78,5 +78,5 @@ public interface DiscordAPI {
* @return the Discord user
*/
@NotNull
Optional<DiscordUser> getUserById(@NotNull String id);
Optional<DiscordUser> getUserById(long id);
}

View File

@ -23,8 +23,6 @@
package com.discordsrv.api.discord.api.entity;
import org.jetbrains.annotations.NotNull;
/**
* A snowflake identifier.
*/
@ -34,6 +32,5 @@ public interface Snowflake {
* Gets the id of this entity.
* @return the id of this entity
*/
@NotNull
String getId();
long getId();
}

View File

@ -51,7 +51,7 @@ public interface DiscordMessageChannel extends Snowflake {
* @param id the id of the message to delete
* @return a future that will fail if the request fails
*/
CompletableFuture<Void> deleteMessageById(String id);
CompletableFuture<Void> deleteMessageById(long id);
/**
* Edits the message identified by the id.
@ -62,6 +62,6 @@ public interface DiscordMessageChannel extends Snowflake {
* @throws com.discordsrv.api.discord.api.exception.NotReadyException if DiscordSRV is not ready, {@link com.discordsrv.api.DiscordSRVApi#isReady()}
*/
@NotNull
CompletableFuture<ReceivedDiscordMessage> editMessageById(String id, SendableDiscordMessage message);
CompletableFuture<ReceivedDiscordMessage> editMessageById(long id, SendableDiscordMessage message);
}

View File

@ -47,13 +47,13 @@ public interface DiscordGuild {
* @param id the id for the Discord guild member
* @return the Discord guild member from the cache
*/
Optional<DiscordGuildMember> getMemberById(String id);
Optional<DiscordGuildMember> getMemberById(long id);
/**
* Gets a Discord role by id from the cache, the provided entity can be cached and will not update if it changes on Discord.
* @param id the id for the Discord role
* @return the Discord role from the cache
*/
Optional<DiscordRole> getRoleById(String id);
Optional<DiscordRole> getRoleById(long id);
}

View File

@ -51,7 +51,7 @@ public interface AllowedMention {
* @param id the id of the role
* @return a {@link AllowedMention} object
*/
static AllowedMention role(String id) {
static AllowedMention role(long id) {
return new Snowflake(id, false);
}
@ -60,7 +60,7 @@ public interface AllowedMention {
* @param id the id of the user
* @return a {@link AllowedMention} object
*/
static AllowedMention user(String id) {
static AllowedMention user(long id) {
return new Snowflake(id, true);
}
@ -84,15 +84,15 @@ public interface AllowedMention {
class Snowflake implements AllowedMention {
private final String id;
private final long id;
private final boolean user;
public Snowflake(String id, boolean user) {
public Snowflake(long id, boolean user) {
this.id = id;
this.user = user;
}
public String getId() {
public long getId() {
return id;
}

View File

@ -42,7 +42,7 @@ public class ChannelConfigHelper {
private final DiscordSRV discordSRV;
private final LoadingCache<String, GameChannel> nameToChannelCache;
private final Map<String, Pair<String, ChannelConfig>> discordToConfigMap;
private final Map<Long, Pair<String, ChannelConfig>> discordToConfigMap;
public ChannelConfigHelper(DiscordSRV discordSRV) {
this.discordSRV = discordSRV;
@ -74,13 +74,13 @@ public class ChannelConfigHelper {
return;
}
Map<String, Pair<String, ChannelConfig>> newMap = new HashMap<>();
Map<Long, Pair<String, ChannelConfig>> newMap = new HashMap<>();
for (Map.Entry<String, BaseChannelConfig> entry : channels().entrySet()) {
String channelName = entry.getKey();
BaseChannelConfig value = entry.getValue();
if (value instanceof ChannelConfig) {
ChannelConfig channelConfig = (ChannelConfig) value;
for (String channelId : channelConfig.channelIds) {
for (long channelId : channelConfig.channelIds) {
newMap.put(channelId, Pair.of(channelName, channelConfig));
}
}
@ -88,7 +88,7 @@ public class ChannelConfigHelper {
synchronized (discordToConfigMap) {
discordToConfigMap.clear();
for (Map.Entry<String, Pair<String, ChannelConfig>> entry : newMap.entrySet()) {
for (Map.Entry<Long, Pair<String, ChannelConfig>> entry : newMap.entrySet()) {
discordToConfigMap.put(entry.getKey(), entry.getValue());
}
}

View File

@ -50,6 +50,6 @@ public class ChannelConfig extends BaseChannelConfig {
}
@Comment("The channels this in-game channel will forward to in Discord")
public List<String> channelIds = new ArrayList<>(Collections.singletonList("channel-id-here"));
public List<Long> channelIds = new ArrayList<>(Collections.singletonList(0L));
}

View File

@ -58,12 +58,12 @@ public class DiscordAPIImpl implements DiscordAPI {
private final DiscordSRV discordSRV;
private final AsyncLoadingCache<String, WebhookClient> cachedClients;
private final AsyncLoadingCache<Long, WebhookClient> cachedClients;
public DiscordAPIImpl(DiscordSRV discordSRV) {
this.discordSRV = discordSRV;
this.cachedClients = discordSRV.caffeineBuilder()
.removalListener((RemovalListener<String, WebhookClient>) (id, client, cause) -> {
.removalListener((RemovalListener<Long, WebhookClient>) (id, client, cause) -> {
if (client != null) {
client.close();
}
@ -72,7 +72,7 @@ public class DiscordAPIImpl implements DiscordAPI {
.buildAsync(new WebhookCacheLoader());
}
public CompletableFuture<WebhookClient> queryWebhookClient(String channelId) {
public CompletableFuture<WebhookClient> queryWebhookClient(long channelId) {
return cachedClients.get(channelId);
}
@ -82,7 +82,7 @@ public class DiscordAPIImpl implements DiscordAPI {
}
@Override
public @NotNull Optional<? extends DiscordMessageChannel> getMessageChannelById(@NotNull String id) {
public @NotNull Optional<? extends DiscordMessageChannel> getMessageChannelById(long id) {
Optional<DiscordTextChannel> textChannel = getTextChannelById(id);
if (textChannel.isPresent()) {
return textChannel;
@ -92,37 +92,37 @@ public class DiscordAPIImpl implements DiscordAPI {
}
@Override
public @NotNull Optional<DiscordDMChannel> getDirectMessageChannelById(@NotNull String id) {
public @NotNull Optional<DiscordDMChannel> getDirectMessageChannelById(long id) {
return discordSRV.jda()
.map(jda -> jda.getPrivateChannelById(id))
.map(privateChannel -> new DiscordDMChannelImpl(discordSRV, privateChannel));
}
@Override
public @NotNull Optional<DiscordTextChannel> getTextChannelById(@NotNull String id) {
public @NotNull Optional<DiscordTextChannel> getTextChannelById(long id) {
return discordSRV.jda()
.map(jda -> jda.getTextChannelById(id))
.map(textChannel -> new DiscordTextChannelImpl(discordSRV, textChannel));
}
@Override
public @NotNull Optional<DiscordGuild> getGuildById(@NotNull String id) {
public @NotNull Optional<DiscordGuild> getGuildById(long id) {
return discordSRV.jda()
.map(jda -> jda.getGuildById(id))
.map(guild -> new DiscordGuildImpl(discordSRV, guild));
}
@Override
public @NotNull Optional<DiscordUser> getUserById(@NotNull String id) {
public @NotNull Optional<DiscordUser> getUserById(long id) {
return discordSRV.jda()
.map(jda -> jda.getUserById(id))
.map(DiscordUserImpl::new);
}
private class WebhookCacheLoader implements AsyncCacheLoader<String, WebhookClient> {
private class WebhookCacheLoader implements AsyncCacheLoader<Long, WebhookClient> {
@Override
public @NonNull CompletableFuture<WebhookClient> asyncLoad(@NonNull String channelId, @NonNull Executor executor) {
public @NonNull CompletableFuture<WebhookClient> asyncLoad(@NonNull Long channelId, @NonNull Executor executor) {
CompletableFuture<WebhookClient> future = new CompletableFuture<>();
JDA jda = discordSRV.jda().orElse(null);
@ -169,9 +169,9 @@ public class DiscordAPIImpl implements DiscordAPI {
}
}
private class WebhookCacheExpiry implements Expiry<String, WebhookClient> {
private class WebhookCacheExpiry implements Expiry<Long, WebhookClient> {
private boolean isConfiguredChannel(String channelId) {
private boolean isConfiguredChannel(Long channelId) {
for (BaseChannelConfig config : discordSRV.config().channels.values()) {
if (config instanceof ChannelConfig
&& ((ChannelConfig) config).channelIds.contains(channelId)) {
@ -181,22 +181,22 @@ public class DiscordAPIImpl implements DiscordAPI {
return false;
}
private long expireAfterWrite(String channelId) {
private long expireAfterWrite(Long channelId) {
return isConfiguredChannel(channelId) ? Long.MAX_VALUE : TimeUnit.MINUTES.toNanos(15);
}
@Override
public long expireAfterCreate(@NonNull String channelId, @NonNull WebhookClient webhookClient, long currentTime) {
public long expireAfterCreate(@NonNull Long channelId, @NonNull WebhookClient webhookClient, long currentTime) {
return expireAfterWrite(channelId);
}
@Override
public long expireAfterUpdate(@NonNull String channelId, @NonNull WebhookClient webhookClient, long currentTime, @NonNegative long currentDuration) {
public long expireAfterUpdate(@NonNull Long channelId, @NonNull WebhookClient webhookClient, long currentTime, @NonNegative long currentDuration) {
return expireAfterWrite(channelId);
}
@Override
public long expireAfterRead(@NonNull String channelId, @NonNull WebhookClient webhookClient, long currentTime, @NonNegative long currentDuration) {
public long expireAfterRead(@NonNull Long channelId, @NonNull WebhookClient webhookClient, long currentTime, @NonNegative long currentDuration) {
return isConfiguredChannel(channelId) ? Long.MAX_VALUE : TimeUnit.MINUTES.toNanos(10);
}
}

View File

@ -37,12 +37,12 @@ import java.util.concurrent.CompletableFuture;
public class DiscordDMChannelImpl extends DiscordMessageChannelImpl implements DiscordDMChannel {
private final DiscordSRV discordSRV;
private final String id;
private final long id;
private final DiscordUser user;
public DiscordDMChannelImpl(DiscordSRV discordSRV, PrivateChannel privateChannel) {
this.discordSRV = discordSRV;
this.id = privateChannel.getId();
this.id = privateChannel.getIdLong();
this.user = new DiscordUserImpl(privateChannel.getUser());
}
@ -61,7 +61,7 @@ public class DiscordDMChannelImpl extends DiscordMessageChannelImpl implements D
}
@Override
public @NotNull String getId() {
public long getId() {
return id;
}
@ -84,7 +84,7 @@ public class DiscordDMChannelImpl extends DiscordMessageChannelImpl implements D
}
@Override
public CompletableFuture<Void> deleteMessageById(String id) {
public CompletableFuture<Void> deleteMessageById(long id) {
CompletableFuture<Void> future = privateChannel()
.deleteMessageById(id)
.submit();
@ -92,7 +92,7 @@ public class DiscordDMChannelImpl extends DiscordMessageChannelImpl implements D
}
@Override
public @NotNull CompletableFuture<ReceivedDiscordMessage> editMessageById(String id, SendableDiscordMessage message) {
public @NotNull CompletableFuture<ReceivedDiscordMessage> editMessageById(long id, SendableDiscordMessage message) {
if (message.isWebhookMessage()) {
throw new IllegalArgumentException("Cannot send webhook messages to DMChannels");
}

View File

@ -36,7 +36,6 @@ import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.requests.restaction.MessageAction;
import net.dv8tion.jda.api.utils.MiscUtil;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.CompletableFuture;
@ -45,21 +44,21 @@ import java.util.function.BiFunction;
public class DiscordTextChannelImpl extends DiscordMessageChannelImpl implements DiscordTextChannel {
private final DiscordSRV discordSRV;
private final String id;
private final long id;
private final String name;
private final String topic;
private final DiscordGuild guild;
public DiscordTextChannelImpl(DiscordSRV discordSRV, TextChannel textChannel) {
this.discordSRV = discordSRV;
this.id = textChannel.getId();
this.id = textChannel.getIdLong();
this.name = textChannel.getName();
this.topic = textChannel.getTopic();
this.guild = new DiscordGuildImpl(discordSRV, textChannel.getGuild());
}
@Override
public @NotNull String getId() {
public long getId() {
return id;
}
@ -84,15 +83,15 @@ public class DiscordTextChannelImpl extends DiscordMessageChannelImpl implements
}
@Override
public CompletableFuture<Void> deleteMessageById(String id) {
public CompletableFuture<Void> deleteMessageById(long id) {
return null; // TODO
}
@Override
public @NotNull CompletableFuture<ReceivedDiscordMessage> editMessageById(String id, SendableDiscordMessage message) {
public @NotNull CompletableFuture<ReceivedDiscordMessage> editMessageById(long id, SendableDiscordMessage message) {
return message(
message,
(client, msg) -> client.edit(MiscUtil.parseLong(id), msg),
(client, msg) -> client.edit(id, msg),
(textChannel, msg) -> textChannel.editMessageById(id, msg)
);
}

View File

@ -54,14 +54,14 @@ public class DiscordGuildImpl implements DiscordGuild {
}
@Override
public Optional<DiscordGuildMember> getMemberById(String id) {
public Optional<DiscordGuildMember> getMemberById(long id) {
return guild()
.map(guild -> guild.getMemberById(id))
.map(DiscordGuildMemberImpl::new);
}
@Override
public Optional<DiscordRole> getRoleById(String id) {
public Optional<DiscordRole> getRoleById(long id) {
return guild()
.map(guild -> guild.getRoleById(id))
.map(DiscordRoleImpl::new);

View File

@ -24,16 +24,16 @@ import org.jetbrains.annotations.NotNull;
public class DiscordRoleImpl implements DiscordRole {
private final String id;
private final long id;
private final String name;
public DiscordRoleImpl(Role role) {
this.id = role.getId();
this.id = role.getIdLong();
this.name = role.getName();
}
@Override
public @NotNull String getId() {
public long getId() {
return id;
}

View File

@ -60,8 +60,8 @@ public class ReceivedDiscordMessageImpl extends SendableDiscordMessageImpl imple
discordSRV,
textChannel,
user,
message.getChannel().getId(),
message.getId(),
message.getChannel().getIdLong(),
message.getIdLong(),
message.getContentRaw(),
mappedEmbeds,
webhookUsername,
@ -109,15 +109,15 @@ public class ReceivedDiscordMessageImpl extends SendableDiscordMessageImpl imple
: String.format(User.DEFAULT_AVATAR_URL, Integer.parseInt(author.getDiscriminator()) % 5);
DiscordTextChannel textChannel = discordSRV.discordAPI().getTextChannelById(
Long.toUnsignedString(webhookMessage.getChannelId())).orElse(null);
webhookMessage.getChannelId()).orElse(null);
DiscordUser user = discordSRV.discordAPI().getUserById(
Long.toUnsignedString(webhookMessage.getAuthor().getId())).orElse(null);
webhookMessage.getAuthor().getId()).orElse(null);
return new ReceivedDiscordMessageImpl(
discordSRV,
textChannel,
user,
Long.toUnsignedString(webhookMessage.getChannelId()),
Long.toUnsignedString(webhookMessage.getId()),
webhookMessage.getChannelId(),
webhookMessage.getId(),
webhookMessage.getContent(),
mappedEmbeds,
author.getName(),
@ -128,15 +128,15 @@ public class ReceivedDiscordMessageImpl extends SendableDiscordMessageImpl imple
private final DiscordSRV discordSRV;
private final DiscordTextChannel textChannel;
private final DiscordUser author;
private final String channelId;
private final String id;
private final long channelId;
private final long id;
private ReceivedDiscordMessageImpl(
DiscordSRV discordSRV,
DiscordTextChannel textChannel,
DiscordUser author,
String channelId,
String id,
long channelId,
long id,
String content,
List<DiscordMessageEmbed> embeds,
String webhookUsername,
@ -151,7 +151,7 @@ public class ReceivedDiscordMessageImpl extends SendableDiscordMessageImpl imple
}
@Override
public @NotNull String getId() {
public long getId() {
return id;
}

View File

@ -38,14 +38,14 @@ public final class SendableDiscordMessageUtil {
public static Message toJDA(@NotNull SendableDiscordMessage message) {
List<Message.MentionType> allowedTypes = new ArrayList<>();
List<String> allowedUsers = new ArrayList<>();
List<String> allowedRoles = new ArrayList<>();
List<Long> allowedUsers = new ArrayList<>();
List<Long> allowedRoles = new ArrayList<>();
Set<AllowedMention> allowedMentions = message.getAllowedMentions();
if (allowedMentions != null) {
for (AllowedMention allowedMention : allowedMentions) {
if (allowedMention instanceof AllowedMention.Snowflake) {
String id = ((AllowedMention.Snowflake) allowedMention).getId();
long id = ((AllowedMention.Snowflake) allowedMention).getId();
if (((AllowedMention.Snowflake) allowedMention).isUser()) {
allowedUsers.add(id);
} else {
@ -66,8 +66,8 @@ public final class SendableDiscordMessageUtil {
.setContent(message.getContent())
.setEmbeds(embeds)
.setAllowedMentions(allowedTypes)
.mentionUsers(allowedUsers.toArray(new String[0]))
.mentionRoles(allowedRoles.toArray(new String[0]))
.mentionUsers(allowedUsers.stream().mapToLong(l -> l).toArray())
.mentionRoles(allowedRoles.stream().mapToLong(l -> l).toArray())
.build();
}

View File

@ -24,18 +24,18 @@ import org.jetbrains.annotations.NotNull;
public class DiscordUserImpl implements DiscordUser {
private final String id;
private final long id;
private final String username;
private final String discriminator;
public DiscordUserImpl(User user) {
this.id = user.getId();
this.id = user.getIdLong();
this.username = user.getName();
this.discriminator = user.getDiscriminator();
}
@Override
public @NotNull String getId() {
public long getId() {
return id;
}

View File

@ -67,13 +67,13 @@ public class GameChatListener extends AbstractListener {
.addReplacement("%message%", DiscordSerializer.INSTANCE.serialize(message))
.build();
List<String> channelIds = channelConfig.get(cfg -> cfg instanceof ChannelConfig ? ((ChannelConfig) cfg).channelIds : null);
List<Long> channelIds = channelConfig.get(cfg -> cfg instanceof ChannelConfig ? ((ChannelConfig) cfg).channelIds : null);
if (channelIds == null || channelIds.isEmpty()) {
return;
}
List<CompletableFuture<ReceivedDiscordMessage>> futures = new ArrayList<>();
for (String channelId : channelIds) {
for (Long channelId : channelIds) {
discordSRV.discordAPI().getTextChannelById(channelId).ifPresent(textChannel ->
futures.add(textChannel.sendMessage(discordMessage)));
}

View File

@ -32,7 +32,7 @@ import org.spongepowered.api.event.lifecycle.RefreshGameEvent;
import org.spongepowered.api.event.lifecycle.StartedEngineEvent;
import org.spongepowered.api.event.lifecycle.StoppingEngineEvent;
import org.spongepowered.plugin.PluginContainer;
import org.spongepowered.plugin.jvm.Plugin;
import org.spongepowered.plugin.builtin.jvm.Plugin;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;