Skip to content

Commit

Permalink
Fixes two issues: vanished roles are no longer mentioned & test is no…
Browse files Browse the repository at this point in the history
…w fixed.

Signed-off-by: Mihou <kokocrunchsh@gmail.com>
  • Loading branch information
ShindouMihou committed Jun 21, 2021
1 parent 5ad2964 commit 0dd2b54
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 39 deletions.
16 changes: 9 additions & 7 deletions src/main/java/pw/mihou/amelia/Amelia.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Optional;
import java.util.stream.Collectors;

public class Amelia {
Expand Down Expand Up @@ -74,8 +75,12 @@ public static void main(String[] args) {
UserDB.load();
new DiscordApiBuilder()
.setToken(token)
.setAllIntentsExcept(Intent.GUILD_MESSAGE_TYPING, Intent.DIRECT_MESSAGE_TYPING, Intent.GUILD_INTEGRATIONS, Intent.GUILD_WEBHOOKS,
Intent.GUILD_BANS, Intent.GUILD_EMOJIS, Intent.GUILD_INVITES, Intent.GUILD_VOICE_STATES)
.setAllIntentsExcept
(Intent.GUILD_MESSAGE_TYPING, Intent.DIRECT_MESSAGE_TYPING,
Intent.GUILD_INTEGRATIONS, Intent.GUILD_WEBHOOKS,
Intent.GUILD_BANS, Intent.GUILD_EMOJIS,
Intent.GUILD_INVITES, Intent.GUILD_VOICE_STATES,
Intent.GUILD_PRESENCES)
.setTotalShards(1)
.loginAllShards()
.forEach(shard -> shard.
Expand Down Expand Up @@ -113,11 +118,8 @@ public static String format(ItemWrapper item, FeedModel feedModel, Server server
}

public static String getMentions(ArrayList<Long> roles, Server server) {
return roles.stream()
.map(aLong -> server.getRoleById(aLong)
.map(Role::getMentionTag)
.orElse("[Vanished Role]"))
.collect(Collectors.joining());
return roles.stream().map(aLong -> server.getRoleById(aLong).map(Role::getMentionTag))
.filter(Optional::isPresent).map(Optional::get).collect(Collectors.joining());
}

private static void registerAllCommands(DiscordApi api) {
Expand Down
47 changes: 16 additions & 31 deletions src/main/java/pw/mihou/amelia/commands/test/TestCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package pw.mihou.amelia.commands.test;

import org.javacord.api.entity.permission.Role;
import org.javacord.api.entity.server.Server;
import org.javacord.api.entity.user.User;
import org.javacord.api.event.message.MessageCreateEvent;
Expand All @@ -10,8 +9,6 @@
import pw.mihou.amelia.io.rome.ReadRSS;
import pw.mihou.amelia.templates.Message;

import java.util.ArrayList;

public class TestCommand extends Command {

public TestCommand() {
Expand All @@ -27,28 +24,22 @@ protected void runCommand(MessageCreateEvent event, User user, Server server, St
long id = Long.parseLong(args[1]);
if (FeedDB.validate(id)) {
event.getMessage().reply("Attempting to perform test fetch...")
.thenAccept(message -> {
FeedDB.getServer(server.getId())
.getFeedModel(id)
.ifPresentOrElse(feedModel -> {
server.getTextChannelById(feedModel.getChannel()).ifPresentOrElse(tc -> {
ReadRSS.getLatest(feedModel.getFeedURL()).ifPresentOrElse(item ->
item.getPubDate().ifPresentOrElse(date -> {
if (date.after(feedModel.getDate())) {
Message.msg(Amelia.format(item, feedModel, tc.getServer())).send(tc)
.whenComplete((msg, throwable) -> {
if(throwable != null){
message.edit("An exception was thrown, is it possible that the bot cannot write on the channel?");
} else {
message.edit("Amelia was successfully able to deliver the feed!");
}
});
}
}, () -> message.edit("We were unable to fetch the date of the feeds...")),
() -> message.edit("Amelia was not able to retrieve the RSS feed from ScribbleHub..."));
}, () -> message.edit("We were unable to find the text channel ("+feedModel.getChannel()+"), please verify it exists and the bot can see and write on it!"));
}, () -> message.edit("We were unable to find the feed, are you sure you are using the feed's unique id?\nPlease verify using `feeds` command."));
});
.thenAccept(message -> FeedDB.getServer(server.getId())
.getFeedModel(id).ifPresent(feedModel -> server.getTextChannelById(feedModel.getChannel())
.ifPresentOrElse(textChannel -> ReadRSS.getLatest(feedModel.getFeedURL())
.ifPresentOrElse(itemWrapper -> itemWrapper.getPubDate()
.ifPresentOrElse(date ->
Message.msg(Amelia.format(itemWrapper, feedModel, textChannel.getServer())).send(textChannel)
.whenComplete((msg, throwable) -> {
if(throwable != null) {
message.edit("An exception was thrown, is it possible that the bot cannot write on the channel?");
message.reply("Exception: \n```"+throwable.getMessage()+"```");
} else {
message.edit("Amelia was able to deliver the feed!");
}
}), () -> message.edit("We were unable to fetch the date of the feeds...")), () -> message.edit("Amelia was not able to retrieve the RSS feed from ScribbleHub...")),
() -> message.edit("We were unable to find the text channel ("+feedModel.getChannel()+"), " +
"please verify it exists and the bot can see and write on it!"))));
} else {
event.getMessage().reply("Error: We couldn't find the feed, are you sure you are using the feed's unique id." +
"\nPlease verify using `feeds`");
Expand All @@ -61,10 +52,4 @@ protected void runCommand(MessageCreateEvent event, User user, Server server, St
}
}
}

private String getMentions(ArrayList<Long> roles, Server server) {
StringBuilder builder = new StringBuilder();
roles.forEach(aLong -> builder.append(server.getRoleById(aLong).map(Role::getMentionTag).orElse("[Vanished Role]")));
return builder.toString();
}
}
3 changes: 2 additions & 1 deletion src/main/java/pw/mihou/amelia/io/rome/ReadRSS.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
public class ReadRSS {

private static final LoadingCache<String, ItemWrapper> feeds = Caffeine.newBuilder()
.expireAfterWrite(2, TimeUnit.MINUTES).refreshAfterWrite(9, TimeUnit.MINUTES)
.expireAfterWrite(2, TimeUnit.MINUTES)
.refreshAfterWrite(9, TimeUnit.MINUTES)
.build(key -> request(key).orElse(null));
private static final RssReader reader = new RssReader().setUserAgent("Amelia/1.0r1 (Language=Java/1.8, Developer=Shindou Mihou)");

Expand Down

0 comments on commit 0dd2b54

Please sign in to comment.