Skip to content
Open
27 changes: 15 additions & 12 deletions src/main/java/oakbot/bot/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -892,20 +892,10 @@ private ChatActions handleJoinRoomAction(JoinRoom action) {
return action.onSuccess().get();
}

try {
leave(action.roomId());
} catch (Exception e) {
logger.atError().setCause(e).log(() -> "Problem leaving room " + action.roomId() + " after it was found that the bot can't post messages to it.");
}

leaveRoomSafely(action.roomId(), "the bot can't post messages to it");
return action.ifLackingPermissionToPost().get();
} catch (PrivateRoomException | RoomPermissionException e) {
try {
leave(action.roomId());
} catch (Exception e2) {
logger.atError().setCause(e2).log(() -> "Problem leaving room " + action.roomId() + " after it was found that the bot can't join or post messages to it.");
}

leaveRoomSafely(action.roomId(), "the bot can't join or post messages to it");
return action.ifLackingPermissionToPost().get();
} catch (RoomNotFoundException e) {
return action.ifRoomDoesNotExist().get();
Expand All @@ -914,6 +904,19 @@ private ChatActions handleJoinRoomAction(JoinRoom action) {
}
}

/**
* Attempts to leave a room and logs any errors that occur.
* @param roomId the room ID to leave
* @param reason the reason for leaving (used in error message)
*/
private void leaveRoomSafely(int roomId, String reason) {
try {
leave(roomId);
} catch (Exception e) {
logger.atError().setCause(e).log(() -> "Problem leaving room " + roomId + " after it was found that " + reason + ".");
}
}

private void handleLeaveRoomAction(LeaveRoom action) {
try {
leave(action.roomId());
Expand Down