Skip to content

Commit db8d4c0

Browse files
committed
Change channel lookups to use numeric IDs, allows channels in categories to be used
Closes #112, #113, #115
1 parent cd8e469 commit db8d4c0

File tree

4 files changed

+22
-23
lines changed

4 files changed

+22
-23
lines changed

core/src/main/java/net/shadowfacts/discordchat/api/IConfig.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public interface IConfig {
1616

1717
String getToken();
1818

19-
String getServerID();
19+
Long getServerID();
2020

21-
List<String> getChannels();
21+
List<Long> getChannelIDs();
2222

2323
String getCommandPrefix();
2424

core/src/main/java/net/shadowfacts/discordchat/core/Config.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package net.shadowfacts.discordchat.core;
22

3-
import com.typesafe.config.ConfigFactory;
4-
import com.typesafe.config.ConfigObject;
5-
import com.typesafe.config.ConfigRenderOptions;
6-
import com.typesafe.config.ConfigValueFactory;
3+
import com.typesafe.config.*;
74
import net.shadowfacts.discordchat.api.IConfig;
85
import net.shadowfacts.discordchat.api.permission.Permission;
96
import net.shadowfacts.shadowlib.util.IOUtils;
@@ -66,13 +63,17 @@ public String getToken() {
6663
}
6764

6865
@Override
69-
public String getServerID() {
70-
return config.getString("discordchat.discord.server");
66+
public Long getServerID() {
67+
return config.getLong("discordchat.discord.server");
7168
}
7269

7370
@Override
74-
public List<String> getChannels() {
75-
return config.getStringList("discordchat.discord.channels");
71+
public List<Long> getChannelIDs() {
72+
try {
73+
return config.getLongList("discordchat.discord.channels");
74+
} catch (ConfigException.WrongType e) {
75+
throw new RuntimeException("Unable to get channel IDs, update the config to use numeric channel IDs.", e);
76+
}
7677
}
7778

7879
@Override

core/src/main/java/net/shadowfacts/discordchat/core/DiscordChat.java

+9-11
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,13 @@ public void connect() {
102102
if (guild == null) {
103103
throw new RuntimeException("Invalid server ID");
104104
}
105-
for (String channel : config.getChannels()) {
106-
List<TextChannel> channels = guild.getTextChannelsByName(channel, true);
107-
if (channels.isEmpty()) {
108-
throw new RuntimeException("Invalid channel ID: " + channel);
105+
if (config.getChannelIDs().isEmpty()) {
106+
throw new RuntimeException("No channel IDs");
107+
} else {
108+
for (long channelID : config.getChannelIDs()) {
109+
if (guild.getTextChannelById(channelID) == null) {
110+
throw new RuntimeException("Invalid channel ID: " + channelID);
111+
}
109112
}
110113
}
111114
}, "DiscordChat-initializer").start();
@@ -237,13 +240,8 @@ public void sendMessage(String message) {
237240
}
238241
Guild guild = jda.getGuildById(config.getServerID());
239242
channels = new ArrayList<>();
240-
for (String channel : config.getChannels()) {
241-
List<TextChannel> channels = guild.getTextChannelsByName(channel, false);
242-
if (channels.size() < 1) {
243-
throw new RuntimeException("No such channel: " + channel);
244-
} else {
245-
this.channels.addAll(channels);
246-
}
243+
for (long channelID : config.getChannelIDs()) {
244+
channels.add(guild.getTextChannelById(channelID));
247245
}
248246
}
249247
sendMessage(message, channels);

core/src/main/java/net/shadowfacts/discordchat/core/Listener.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public void onPrivateMessageReceived(PrivateMessageReceivedEvent event) {
4141
@Override
4242
public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
4343
if (event.getAuthor().equals(event.getJDA().getSelfUser())) return;
44-
if (!event.getGuild().getId().equals(config.getServerID())) return;
45-
if (!config.getChannels().contains(event.getChannel().getName())) return;
44+
if (event.getGuild().getIdLong() != config.getServerID()) return;
45+
if (!config.getChannelIDs().contains(event.getChannel().getIdLong())) return;
4646

4747
String raw = event.getMessage().getRawContent();
4848
if (raw.startsWith(config.getCommandPrefix())) {

0 commit comments

Comments
 (0)