Fix of getInfo and role adding/removing

Fixed getInfo if the player isn't on the DC server
Fixed message after adding or removing a game role
This commit is contained in:
Norbi Peti 2019-08-08 13:45:40 +02:00
parent 7db3b17090
commit 480032a3d6
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
3 changed files with 22 additions and 14 deletions

View file

@ -12,6 +12,7 @@ import lombok.val;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerCommandEvent;
import reactor.core.publisher.Mono;
public class MCListener implements Listener {
@EventHandler
@ -33,13 +34,16 @@ public class MCListener implements Listener {
DiscordPlayer dp = e.getPlayer().getAs(DiscordPlayer.class);
if (dp == null || dp.getDiscordID() == null || dp.getDiscordID().equals(""))
return;
User user = DiscordPlugin.dc.getUserById(Snowflake.of(dp.getDiscordID())).block();
if (user == null) return;
val userOpt = DiscordPlugin.dc.getUserById(Snowflake.of(dp.getDiscordID())).onErrorResume(t -> Mono.empty()).blockOptional();
if (!userOpt.isPresent()) return;
User user = userOpt.get();
e.addInfo("Discord tag: " + user.getUsername() + "#" + user.getDiscriminator());
Member member = user.asMember(DiscordPlugin.mainServer.getId()).block();
if (member == null) return;
val pr = member.getPresence().block();
if (pr == null) return;
val memberOpt = user.asMember(DiscordPlugin.mainServer.getId()).blockOptional();
if (!memberOpt.isPresent()) return;
Member member = memberOpt.get();
val prOpt = member.getPresence().blockOptional();
if (!prOpt.isPresent()) return;
val pr = prOpt.get();
e.addInfo(pr.getStatus().toString());
if (pr.getActivity().isPresent()) {
val activity = pr.getActivity().get();

View file

@ -27,7 +27,6 @@ import org.bukkit.event.server.TabCompleteEvent;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.Objects;
import java.util.Optional;
@RequiredArgsConstructor
@ -53,11 +52,13 @@ class MCListener implements Listener {
final Player p = e.getPlayer();
DiscordPlayer dp = e.GetPlayer().getAs(DiscordPlayer.class);
if (dp != null) {
val user = DiscordPlugin.dc.getUserById(Snowflake.of(dp.getDiscordID())).block();
DiscordPlugin.dc.getUserById(Snowflake.of(dp.getDiscordID())).flatMap(user -> user.getPrivateChannel().flatMap(chan -> module.chatChannelMono().flatMap(cc -> {
MCChatUtils.addSender(MCChatUtils.OnlineSenders, dp.getDiscordID(),
new DiscordPlayerSender(user, Objects.requireNonNull(user).getPrivateChannel().block(), p)); //TODO: Don't block
new DiscordPlayerSender(user, chan, p));
MCChatUtils.addSender(MCChatUtils.OnlineSenders, dp.getDiscordID(),
new DiscordPlayerSender(user, module.chatChannelMono().block(), p)); //Stored per-channel
new DiscordPlayerSender(user, cc, p)); //Stored per-channel
return Mono.empty();
}))).subscribe();
}
final String message = e.GetPlayer().PlayerName().get() + " joined the game";
MCChatUtils.forAllowedCustomAndAllMCChat(MCChatUtils.send(message), e.getPlayer(), ChannelconBroadcast.JOINLEAVE, true);

View file

@ -8,6 +8,7 @@ import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass;
import discord4j.core.object.entity.Role;
import lombok.val;
import reactor.core.publisher.Mono;
import java.util.List;
import java.util.stream.Collectors;
@ -31,7 +32,8 @@ public class RoleCommand extends ICommand2DC {
return true;
try {
sender.getMessage().getAuthorAsMember()
.subscribe(m -> m.addRole(role.getId()).subscribe(r -> sender.sendMessage("added role.")));
.flatMap(m -> m.addRole(role.getId()).switchIfEmpty(Mono.fromRunnable(() -> sender.sendMessage("added role."))))
.subscribe();
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while adding role!", e);
sender.sendMessage("an error occured while adding the role.");
@ -49,7 +51,8 @@ public class RoleCommand extends ICommand2DC {
return true;
try {
sender.getMessage().getAuthorAsMember()
.subscribe(m -> m.removeRole(role.getId()).subscribe(r -> sender.sendMessage("removed role.")));
.flatMap(m -> m.removeRole(role.getId()).switchIfEmpty(Mono.fromRunnable(() -> sender.sendMessage("removed role."))))
.subscribe();
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while removing role!", e);
sender.sendMessage("an error occured while removing the role.");